@@ -20,10 +20,35 @@ unsigned long nextCommunication;
20
20
unsigned long nextConfirmation;
21
21
int err_count;
22
22
23
- void setColor (int r,int g,int b) {
24
- digitalWrite (LBLUE,b);
25
- digitalWrite (LGREEN,g);
26
- digitalWrite (LRED,r);
23
+ #define COLOR_BLACK 0x000000
24
+ #define COLOR_BLUE 0x0000FF
25
+ #define COLOR_RED 0xFF0000
26
+ #define COLOR_GREEN 0x00FF00
27
+
28
+
29
+ uint32_t previousLedValue;
30
+ void setColor (uint32_t color, boolean save) {
31
+ int r = (color >> 16 ) & 0xff ;
32
+ int g = (color >> 8 ) & 0xff ;
33
+ int b = (color ) & 0xff ;
34
+
35
+ if ( r == 0xff ) digitalWrite (LRED,LOW);
36
+ else if ( r == 0x00 ) digitalWrite (LRED,HIGH);
37
+ else analogWrite (LRED,256 -r);
38
+
39
+ if ( g == 0xff ) digitalWrite (LGREEN,LOW);
40
+ else if ( g == 0x00 ) digitalWrite (LGREEN,HIGH);
41
+ else analogWrite (LGREEN,256 -g);
42
+
43
+ if ( b == 0xff ) digitalWrite (LBLUE,LOW);
44
+ else if ( b == 0x00 ) digitalWrite (LBLUE,HIGH);
45
+ else analogWrite (LBLUE,256 -b);
46
+
47
+ if ( save ) previousLedValue=color;
48
+ }
49
+
50
+ void restoreColor () {
51
+ setColor (previousLedValue,false );
27
52
}
28
53
29
54
void setup () {
@@ -47,7 +72,7 @@ void setup() {
47
72
pinMode (LGREEN,OUTPUT);
48
73
pinMode (LRED,OUTPUT);
49
74
pinMode (BOARDLED,OUTPUT);
50
- setColor (HIGH,HIGH,LOW );
75
+ setColor (COLOR_BLUE, true );
51
76
Serial.println (" Go on!" );
52
77
}
53
78
@@ -81,19 +106,20 @@ void loop() {
81
106
err_count=0 ;
82
107
nextCommunication=millis ()+10000 ; // + 10s
83
108
nextConfirmation=millis ()+10000 ;
109
+ setColor (COLOR_GREEN,true );
84
110
} else {
85
- setColor (LOW,HIGH,HIGH );
111
+ setColor (COLOR_RED, true );
86
112
}
87
113
}
88
114
if ( ! gps.location .isValid () || !gps.altitude .isValid () ) {
89
115
// Wait for GPS to be positionned
90
- setColor (HIGH,HIGH,HIGH );
116
+ setColor (COLOR_BLACK, false );
91
117
delayWithGps (1000 );
92
118
for ( int i = 0 ; i < gps.satellites .value ()+1 ; i++ ) {
93
- setColor (HIGH,HIGH,HIGH );
119
+ setColor (COLOR_BLACK, false );
94
120
delayWithGps (150 );
95
- setColor (HIGH,(connected)?LOW:HIGH,(connected)?HIGH:LOW );
96
- delayWithGps (150 );
121
+ restoreColor ( );
122
+ delayWithGps (100 );
97
123
}
98
124
} else {
99
125
// GPS Position OK
@@ -110,7 +136,6 @@ void loop() {
110
136
} else {
111
137
Serial.print (" lat: " );Serial.print (lat);
112
138
Serial.print (" , lng: " );Serial.print (lng);
113
- Serial.print (" , alt_src: " );Serial.print (gps.altitude .value ());
114
139
Serial.print (" , alt: " );Serial.print (altitude);
115
140
Serial.print (" , hdop:" );Serial.print (hdop);
116
141
Serial.print (" , sat:" );Serial.println (gps.satellites .value ());
@@ -120,27 +145,32 @@ void loop() {
120
145
msg[4 ]=(lat>>16 ) & 0xFF ; msg[5 ]=(lat>>8 ) & 0xFF ;msg[6 ]=(lat) & 0xFF ;
121
146
msg[7 ]=(lng>>16 ) & 0xFF ; msg[8 ]=(lng>>8 ) & 0xFF ;msg[9 ]=(lng) & 0xFF ;
122
147
}
123
- setColor (HIGH,HIGH,HIGH );
148
+ setColor (COLOR_BLACK, false );
124
149
modem.beginPacket ();
125
150
modem.write (msg,10 );
126
- int err = modem.endPacket ((nextConfirmation < millis ()));
151
+ boolean toBeConfirmed = (nextConfirmation < millis ());
152
+ int err = modem.endPacket (toBeConfirmed);
127
153
if ( err <= 0 ) {
128
- setColor (LOW,HIGH,HIGH);
154
+ // Should only be here when in confirmation mode
155
+ // with an error
156
+ setColor (COLOR_RED,true );
129
157
err_count++;
130
158
if ( err_count > 50 ) {
131
159
connected = false ;
132
- setColor (HIGH,HIGH,LOW );
160
+ setColor (COLOR_BLUE, true );
133
161
}
134
162
nextCommunication=millis ()+20000L ; // wait for 20 seconds
135
- nextConfirmation=millis ()+180000L ; // wait for 8 minutes - do not want to spam in SF12
163
+ nextConfirmation=millis ()+300000L ; // wait for 5 minutes - do not want to spam in SF12
136
164
} else {
137
- delay (50 );
138
- setColor (HIGH,LOW,HIGH);
139
- err_count = 0 ;
140
- nextCommunication=millis ()+10000L ; // wait for 10 seconds.
141
- if ( nextConfirmation < millis () ) { // confirmation every 30 seconds
142
- nextConfirmation = millis ()+30000L ;
165
+ delayWithGps (200 );
166
+ if (toBeConfirmed) {
167
+ err_count = 0 ;
168
+ setColor (COLOR_GREEN,true );
169
+ nextConfirmation = millis ()+30000L ; // Next confirmation in 30 seconds
170
+ } else {
171
+ restoreColor ();
143
172
}
173
+ nextCommunication=millis ()+10000L ; // wait for 10 seconds.
144
174
}
145
175
}
146
176
}
0 commit comments