Skip to content
Browse files

Added LED "sleep throb," more pin changes

  • Loading branch information...
1 parent 1d54dcf commit ed7ddd597d9f82d00ed6414c3ee5e2629eb56064 @PaintYourDragon PaintYourDragon committed Feb 10, 2012
Showing with 60 additions and 16 deletions.
  1. +58 −14 Gutenbird.ino
  2. +2 −2 README.txt
View
72 Gutenbird.ino
@@ -7,12 +7,12 @@ REQUIRES ARDUINO IDE 1.0 OR LATER -- Back-porting is not likely to
occur, as the code is deeply dependent on the Stream class, etc.
Required hardware includes an Ethernet-connected Arduino board such
-as the Arduino Uno Ethernet or other Arduino-compatible board with an
-Arduino Ethernet Shield, plus an Adafruit Mini Thermal Receipt printer
-and all related power supplies and cabling.
+as the Arduino Ethernet or other Arduino-compatible board with an
+Arduino Ethernet Shield, plus an Adafruit Mini Thermal Receipt
+printer and all related power supplies and cabling.
Resources:
-http://www.adafruit.com/products/418 Arduino Ethernet Uno
+http://www.adafruit.com/products/418 Arduino Ethernet
http://www.adafruit.com/products/284 FTDI Friend
http://www.adafruit.com/products/201 Arduino Uno
http://www.adafruit.com/products/201 Ethernet Shield
@@ -28,10 +28,11 @@ http://www.adafruit.com/products/600 Printer starter pack
// Global stuff --------------------------------------------------------------
const int
- printer_RX_Pin = 2, // Printer connection: green wire
- printer_TX_Pin = 3, // Printer connection: yellow wire
- printer_Ground = 4, // Printer connection: black wire
- led_pin = 5, // To status LED (hardware PWM pin)
+ led_pin = 3, // To status LED (hardware PWM pin)
+ // Pin 4 is skipped -- this is the Card Select line for Arduino Ethernet!
+ printer_RX_Pin = 5, // Printer connection: green wire
+ printer_TX_Pin = 6, // Printer connection: yellow wire
+ printer_Ground = 7, // Printer connection: black wire
maxTweets = 5; // Limit tweets printed; avoid runaway output
const unsigned long // Time limits, expressed in milliseconds:
pollingInterval = 60L * 1000L, // Note: Twitter server will allow 150/hr max
@@ -40,6 +41,7 @@ const unsigned long // Time limits, expressed in milliseconds:
Thermal
printer(printer_RX_Pin, printer_TX_Pin);
byte
+ sleepPos = 0, // Current "sleep throb" table position
resultsDepth, // Used in JSON parsing
// Ethernet MAC address is found on sticker on Ethernet shield or board:
mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x76, 0x09 };
@@ -60,6 +62,14 @@ char
msgText[141], // Max tweet length (140) + \0
name[11], // Temp space for name:value parsing
value[141]; // Temp space for name:value parsing
+PROGMEM byte
+ sleepTab[] = { // "Sleep throb" brightness table (reverse for second half)
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 1, 1, 2, 3, 4, 5, 6, 8, 10, 13,
+ 15, 19, 22, 26, 31, 36, 41, 47, 54, 61,
+ 68, 76, 84, 92, 101, 110, 120, 129, 139, 148,
+ 158, 167, 177, 186, 194, 203, 211, 218, 225, 232,
+ 237, 242, 246, 250, 252, 254, 255 };
// Function prototypes -------------------------------------------------------
@@ -73,6 +83,14 @@ int
// ---------------------------------------------------------------------------
void setup() {
+
+ // Set up LED "sleep throb" ASAP, using Timer1 interrupt:
+ TCCR1A = _BV(WGM11); // Mode 14 (fast PWM), 64:1 prescale, OC1A off
+ TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS11) | _BV(CS10);
+ ICR1 = 8333; // ~30 Hz between sleep throb updates
+ TIMSK1 |= _BV(TOIE1); // Enable Timer1 interrupt
+ sei(); // Enable global interrupts
+
Serial.begin(57600);
pinMode(printer_Ground, OUTPUT);
digitalWrite(printer_Ground, LOW); // Just a reference ground, not power
@@ -105,8 +123,14 @@ void loop() {
int i;
char c;
- // Attempt server connection, with timeout...
startTime = millis();
+
+ // Disable Timer1 interrupt during network access, else there's trouble.
+ // Just show LED at steady 100% while working. :T
+ TIMSK1 &= ~_BV(TOIE1);
+ analogWrite(led_pin, 255);
+
+ // Attempt server connection, with timeout...
Serial.print("Connecting to server...");
while((client.connect(serverName, 80) == false) &&
((millis() - startTime) < connectTimeout));
@@ -157,11 +181,19 @@ void loop() {
Serial.println("failed");
}
- // Pause between queries, factoring in time already spent
- // on server connection and printing.
+ // Sometimes network access & printing occurrs so quickly, the steady-on
+ // LED wouldn't even be apparent, instead resembling a discontinuity in
+ // the otherwise smooth sleep throb. Keep it on at least 4 seconds.
+ t = millis() - startTime;
+ if(t < 4000L) delay(4000L - t);
+
+ // Pause between queries, factoring in time already spent on network
+ // access, parsing, printing and LED pause above.
t = millis() - startTime;
if(t < pollingInterval) {
Serial.print("Pausing...");
+ sleepPos = sizeof(sleepTab); // Resume following brightest position
+ TIMSK1 |= _BV(TOIE1); // Re-enable Timer1 interrupt for sleep throb
delay(pollingInterval - t);
Serial.println("done");
}
@@ -190,11 +222,11 @@ boolean jsonParse(int depth, byte endChar) {
printer.print(fromUser);
for(i=strlen(fromUser); i<31; i++) printer.write(' ');
printer.inverseOff();
- printer.println(msgText);
printer.underlineOn();
printer.print(timeStamp);
for(i=strlen(timeStamp); i<32; i++) printer.write(' ');
printer.underlineOff();
+ printer.println(msgText);
printer.feed(3);
printer.sleep();
@@ -237,8 +269,8 @@ boolean jsonParse(int depth, byte endChar) {
readName = true; // Now in name-reading mode
name[0] = 0; // Clear existing name data
} // Else true/false/null or a number follows. These values aren't
- // used or expected by this program, so just ignore...either a comma
- // or endChar will come along eventually, these are handled above.
+ // used or expected by this program, so just ignore...either a comma
+ // or endChar will come along eventually, these are handled above.
}
}
@@ -321,3 +353,15 @@ int timedRead(void) {
return -1; // Timed out
}
+// ---------------------------------------------------------------------------
+
+// Timer1 interrupt handler for sleep throb
+ISR(TIMER1_OVF_vect, ISR_NOBLOCK) {
+ // Sine table contains only first half...reflect for second half...
+ analogWrite(led_pin, pgm_read_byte(&sleepTab[
+ (sleepPos >= sizeof(sleepTab)) ?
+ ((sizeof(sleepTab) - 1) * 2 - sleepPos) : sleepPos]));
+ if(++sleepPos >= ((sizeof(sleepTab) - 1) * 2)) sleepPos = 0; // Roll over
+ TIFR1 |= TOV1; // Clear Timer1 interrupt flag
+}
+
View
4 README.txt
@@ -9,12 +9,12 @@ Also requires Adafruit Thermal Printer Library:
http://github.com/adafruit/Thermal-Printer-Library
Required hardware includes an Ethernet-connected Arduino board such
-as the Arduino Uno Ethernet or other Arduino-compatible board with an
+as the Arduino Ethernet or other Arduino-compatible board with an
Arduino Ethernet Shield, plus an Adafruit Mini Thermal Receipt printer
and all related power supplies and cabling.
Resources:
-http://www.adafruit.com/products/418 Arduino Ethernet Uno
+http://www.adafruit.com/products/418 Arduino Ethernet
http://www.adafruit.com/products/284 FTDI Friend
http://www.adafruit.com/products/201 Arduino Uno
http://www.adafruit.com/products/201 Ethernet Shield

0 comments on commit ed7ddd5

Please sign in to comment.
Something went wrong with that request. Please try again.