1
+ /*
2
+ Twitter Lamp demo sketch: monitors one or more Twitter accounts
3
+ for changes, changing the RGB led colors
4
+ Based on the orginal code of Gutenbird by Adafruit Industries
5
+ Modified by Federico Vanzati (Officine Arduino)
6
+ Released with MIT licesne.
7
+
8
+ REQUIRES ARDUINO IDE 1.0 OR LATER -- Back-porting is not likely to
9
+ occur, as the code is deeply dependent on the Stream class, etc.
10
+
11
+ Required hardware includes a WiFi Shield connected to the Arduino UNO board
12
+ and power RGB leds.
13
+ */
14
+
1
15
#include < SPI.h>
2
16
#include < WiFi.h>
3
17
#include " structRgb.h"
4
18
#include < avr/pgmspace.h>
5
19
6
- char *queryString1 = " #news" ;
7
- char *queryString2 = " #arduino" ;
20
+ // Enter your hashtag that you want to monitoring
21
+ char *queryString1 = " #yourhashtag" ; // first hashtag
22
+ char *queryString2 = " #arduino" ; // second hashtag
8
23
9
- char ssid[] = " OFFICINE ARDUINO" ; // your network SSID (name)
24
+ // Enter your WiFi network settings
25
+ char ssid[] = " yourSSID" ; // your network SSID (name)
10
26
char pass[] = " password" ; // your network password (use for WPA, or use as key for WEP)
11
27
int keyIndex = 0 ; // your network key Index number (needed only for WEP)
12
28
int status = WL_IDLE_STATUS; // status of the wifi connection
13
29
14
30
15
- const int pin_r = 5 ;
16
- const int pin_g = 6 ;
17
- const int pin_b = 3 ;
31
+ const int pin_r = 5 ; // pwm pin for the red led
32
+ const int pin_g = 6 ; // pwm pin for the green led
33
+ const int pin_b = 3 ; // pwm pin for the blue led
18
34
19
- const uint8_t mode1_switch = 2 ;
20
- const uint8_t mode2_switch = 8 ;
35
+ const uint8_t mode1_switch = 2 ; // pin where connect the switch for mode 1
36
+ const uint8_t mode2_switch = 8 ; // pin where connect the switch for mode 2
21
37
uint8_t prev_mode1Switch = true ;
22
38
uint8_t prev_mode2Switch = true ;
23
39
24
- const int maxTweets = 5 ; // Limit tweets printed; avoid runaway output
40
+ const int maxTweets = 5 ; // Limit tweets printed; avoid runaway output
25
41
26
42
const unsigned long // Time limits, expressed in milliseconds:
27
43
pollingInterval = 10L * 1000L , // Note: Twitter server will allow 150/hr max
28
44
connectTimeout = 15L * 1000L , // Max time to retry server link
29
- responseTimeout = 15L * 1000L , // Max time to wait for data from server
30
- fadeInterval = 1000 ;
31
-
32
- unsigned long prevMillisFade = 0 ;
45
+ responseTimeout = 15L * 1000L ; // Max time to wait for data from server
33
46
34
- byte sleepPos = 0 ; // Current "sleep throb" table position
35
47
byte resultsDepth; // Used in JSON parsing
36
48
37
49
uint8_t colorCount = 0 ;
@@ -71,10 +83,10 @@ fadeTo(rgb, rgb);
71
83
// ---------------------------------------------------------------------------
72
84
73
85
void setup () {
74
- pinMode (mode1_switch, INPUT_PULLUP);
75
- pinMode (mode2_switch, INPUT_PULLUP);
76
-
86
+ pinMode (mode1_switch, INPUT_PULLUP); // connecting the first pin directly to the input pin w/o external hardware
87
+ pinMode (mode2_switch, INPUT_PULLUP); // connecting the second pin directly to the input pin w/o external hardware
77
88
89
+ // Little test just to check if the the leds are working and saying hello
78
90
analogWrite (pin_r, 255 );
79
91
analogWrite (pin_g, 0 );
80
92
analogWrite (pin_b, 0 );
@@ -94,6 +106,7 @@ void setup() {
94
106
analogWrite (pin_g, 0 );
95
107
analogWrite (pin_b, 0 );
96
108
109
+ // Initialize the serial communication, used only for debuggging
97
110
Serial.begin (9600 );
98
111
99
112
// check for the presence of the shield:
@@ -111,7 +124,7 @@ void setup() {
111
124
status = WiFi.begin (ssid, pass);
112
125
113
126
// wait 10 seconds for connection:
114
- delay (5000 );
127
+ delay (10000 );
115
128
}
116
129
// you're connected now, so print out the status:
117
130
printWifiStatus ();
@@ -134,11 +147,12 @@ void loop() {
134
147
135
148
startTime = millis ();
136
149
150
+ // check the switch state
137
151
uint8_t mode1_switchStatus = digitalRead (mode1_switch);
138
152
uint8_t mode2_switchStatus = digitalRead (mode2_switch);
139
153
154
+ // if just switched to mode 1, change the color sequence
140
155
if (mode1_switchStatus == false && prev_mode1Switch == true ) {
141
- Serial.println (" ARDUINO" );
142
156
colorCount = 0 ;
143
157
strcpy (queryString, queryString1);
144
158
for (uint8_t i=0 ; i<8 ; i++) {
@@ -153,15 +167,15 @@ void loop() {
153
167
Serial.print (" " );
154
168
Serial.print (lampShades[i].green );
155
169
Serial.print (" " );
156
- Serial.println (lampShades[i].blue );
157
-
170
+ Serial.println (lampShades[i].blue );
158
171
}
159
172
analogWrite (pin_r, lampShades[0 ].red );
160
173
analogWrite (pin_g, lampShades[0 ].green );
161
174
analogWrite (pin_b, lampShades[0 ].blue );
162
175
}
176
+
177
+ // if just switched to mode 2, change the color sequence
163
178
else if (mode2_switchStatus == false && prev_mode2Switch == true ) {
164
- Serial.println (" CIAO" );
165
179
colorCount = 0 ;
166
180
strcpy (queryString, queryString2);
167
181
for (uint8_t i=0 ; i<8 ; i++) {
@@ -177,7 +191,6 @@ void loop() {
177
191
Serial.print (lampShades[i].green );
178
192
Serial.print (" " );
179
193
Serial.println (lampShades[i].blue );
180
-
181
194
}
182
195
183
196
analogWrite (pin_r, lampShades[0 ].red );
@@ -187,12 +200,15 @@ void loop() {
187
200
prev_mode1Switch = mode1_switchStatus;
188
201
prev_mode2Switch = mode2_switchStatus;
189
202
203
+ // Use for debugging the the switch state
204
+ /*
190
205
Serial.print(prev_mode1Switch);
191
206
Serial.print(" ");
192
207
Serial.println(prev_mode2Switch);
193
208
194
209
Serial.println(queryString);
195
210
Serial.println();
211
+ */
196
212
197
213
// Attempt server connection, with timeout...
198
214
Serial.print (F (" Connecting to server..." ));
@@ -250,7 +266,6 @@ void loop() {
250
266
Serial.println (F (" failed" ));
251
267
}
252
268
253
-
254
269
// Sometimes network access & printing occurrs so quickly, the steady-on
255
270
// LED wouldn't even be apparent, instead resembling a discontinuity in
256
271
// the otherwise smooth sleep throb. Keep it on at least 4 seconds.
@@ -315,13 +330,8 @@ boolean jsonParse(int depth, byte endChar) {
315
330
}
316
331
else if (!strcasecmp (name, " from_user" )) {
317
332
strncpy (fromUser, value, sizeof (fromUser)-1 );
318
- /*
319
- colorCount++;
320
- if(colorCount == maxColorCount)
321
- colorCount = 0;
322
- */
323
- Serial.println (colorCount);
324
333
334
+ // every time a new user post a tweet lest's fade up the LED
325
335
uint8_t cc = (colorCount+1 )%8 ;
326
336
327
337
fadeTo (lampShades[colorCount], lampShades[cc]);
0 commit comments