@@ -24,6 +24,11 @@ int count = 0;
2424// holds the boolean (true/false) state of the light
2525bool is_on = false ;
2626
27+ // track time of last published messages and limit feed->save events to once
28+ // every IO_LOOP_DELAY milliseconds
29+ #define IO_LOOP_DELAY 15000
30+ unsigned long lastUpdate;
31+
2732// set up the 'counter' feed
2833AdafruitIO_Feed *counter = io.feed(" counter" );
2934
@@ -65,47 +70,54 @@ void setup() {
6570 Serial.println ();
6671 Serial.println (io.statusText ());
6772
73+ // make sure all feeds get their current values right away
74+ counter->get ();
75+ counter_two->get ();
76+ light->get ();
77+
6878}
6979
7080void loop () {
7181
7282 // process messages and keep connection alive
7383 io.run ();
7484
75- Serial.println ();
76-
77- // save current count to 'counter'
78- Serial.print (" sending -> counter " );
79- Serial.println (count);
80- counter->save (count);
81-
82- // increment the count by 1 and save the value to 'counter-two'
83- Serial.print (" sending -> counter-two " );
84- Serial.println (count + 1 );
85- counter_two->save (count + 1 );
86-
87- // print out the light value we are sending to Adafruit IO
88- Serial.print (" sending -> light " );
89- if (is_on)
90- Serial.println (" is on.\n " );
91- else
92- Serial.println (" is off.\n " );
93-
94- // save state of light to 'light' feed
95- light->save (is_on);
96-
97- // increment count value
98- count++;
99-
100- // for the purpose of this demo, toggle the
101- // light state based on the count value
102- if ((count % 2 ) == 0 )
103- is_on = true ;
104- else
105- is_on = false ;
106-
107- // wait two seconds (2000 milliseconds == 2 seconds)
108- delay (2000 );
85+ if (millis () > (lastUpdate + IO_LOOP_DELAY)) {
86+ Serial.println ();
87+
88+ // save current count to 'counter'
89+ Serial.print (" sending -> counter " );
90+ Serial.println (count);
91+ counter->save (count);
92+
93+ // increment the count by 1 and save the value to 'counter-two'
94+ Serial.print (" sending -> counter-two " );
95+ Serial.println (count + 1 );
96+ counter_two->save (count + 1 );
97+
98+ // print out the light value we are sending to Adafruit IO
99+ Serial.print (" sending -> light " );
100+ if (is_on)
101+ Serial.println (" is on.\n " );
102+ else
103+ Serial.println (" is off.\n " );
104+
105+ // save state of light to 'light' feed
106+ light->save (is_on);
107+
108+ // increment count value
109+ count++;
110+
111+ // for the purpose of this demo, toggle the
112+ // light state based on the count value
113+ if ((count % 2 ) == 0 )
114+ is_on = true ;
115+ else
116+ is_on = false ;
117+
118+ // update timer
119+ lastUpdate = millis ();
120+ }
109121
110122}
111123
0 commit comments