/* Zeitgeist-o-meter A scrolling Twitter display for Arduino For more see: http://www.instructables.com/id/Arduino-LCD-Twitter-display Copyright PKM 2010 */ /* LiquidCrystal Library Library originally added 18 Apr 2008 by David A. Mellis library modified 5 Jul 2009 by Limor Fried (http://www.ladyada.net) example added 9 Jul 2009 by Tom Igoe modified 8 Feb 2010 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/LiquidCrystal The circuit: * LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * 10K resistor: * ends to +5V and ground * wiper to LCD VO pin (pin 3) */ // include the library code: #include #include #define USART_RX_BUFFER_SIZE (150) Servo myservo; // create servo object to control a servo // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // lcd.print for text constants/strings // lcd.write for variables long loop_start; int bytei; // used in writing serial data to string int col; // used to print a row's worth of string to lcd int input_pin = 0; int new_tweet_status = 0; // only move the flag once int servo_switch_pin = 7; // pin for servo power transistor int servo_pin = 9; // pin for servo control signal int sensor_delay; // read from analog pin, transform to delay in ms char inmsg[200]; int servo_tick = 10; void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.clear(); lcd.noAutoscroll(); lcd.setCursor(0,0); lcd.print("Zeitgeistometer!"); lcd.setCursor(0,1); lcd.print(" (c) PKM 2010"); myservo.attach(servo_pin); // attaches the servo on pin 9 to the servo object pinMode(servo_switch_pin, OUTPUT); digitalWrite(servo_switch_pin, HIGH); myservo.write(0); delay(250); digitalWrite(servo_switch_pin, LOW); Serial.begin(9600); delay(5000); // time to start up host script } void loop() { loop_start = millis(); // test modularised status check Serial.print("status\n"); while (Serial.available() == 0 && (millis() < loop_start+10000)) {} if (Serial.available()) { delay(100); // wait for entire status message // read message into buffer read_to_buffer(); // clear bottom // clear_row(1); // write status message lcd.setCursor(0,1); // for (bytei = 1;bytei < 17; bytei++) { // if (inmsg[bytei] == '\0') { // lcd.write(' '); // } else { // lcd.write(inmsg[bytei]); // } // } if (inmsg[0] == 'T') { if (new_tweet_status == 0) { new_tweet_status = 1; // myservo.write(0); digitalWrite(servo_switch_pin, HIGH); for (bytei=0; bytei < 101; bytei++) { myservo.write(bytei); delay(10); } delay(50); } digitalWrite(servo_switch_pin, LOW); lcd.print(" "); for (bytei=1 ; bytei<14 ; bytei++) { if (inmsg[bytei] != '\0') { lcd.write(inmsg[bytei]); } else { lcd.print(' '); } } // next bytei } else { if (new_tweet_status == 1) { new_tweet_status = 0; // myservo.write(90); digitalWrite(servo_switch_pin, HIGH); for (bytei=100; bytei >=0; bytei--) { myservo.write(bytei); delay(15); } delay(50); digitalWrite(servo_switch_pin, LOW); } lcd.print(" #instructables "); } } else { lcd.setCursor(0,0); lcd.print(" No status mesg "); lcd.setCursor(0,1); lcd.print(" :( "); // no serial available } delay(500); // test modularised tweet check Serial.print("tweet\n"); while (Serial.available() == 0 && (millis() < loop_start+8000)) {} if (Serial.available()) { delay(150); // wait for entire message // read message into buffer read_to_buffer(); // clear top row clear_row(0); // set marker to beginning of tweet bytei=0; while (inmsg[bytei+15] != '\0') { // scrolly scrolly sensor_delay = analogRead(input_pin); sensor_delay = 1024-sensor_delay; sensor_delay = sensor_delay/2; sensor_delay += 50; // (scaled to range 100-300) // sensor_delay = 150; lcd.setCursor(0,0); for (col = 0; col < 16; col++) { lcd.write(inmsg[bytei + col]); } // sdelay(millis() + sensor_delay); bytei++; delay(sensor_delay); // } } } else { lcd.setCursor(0,1); lcd.print("No twit response"); // no serial available } } void clear_buffer() { for (bytei=0;bytei<150;bytei++) { inmsg[bytei] = '\0'; } } void read_to_buffer() { clear_buffer(); bytei = 0; while (Serial.available() > 0) { inmsg[bytei] = Serial.read(); bytei++; } if (bytei == 127 || bytei == 128) while (Serial.available() > 0) { inmsg[bytei] = Serial.read(); bytei++; } bytei = 0; } void clear_row(int r) { lcd.setCursor(0,r); lcd.print(" "); lcd.setCursor(0,r); }