Created
September 17, 2012 14:21
-
-
Save paramaggarwal/3737606 to your computer and use it in GitHub Desktop.
Scrolling Tweets using multiple LED matrix displays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <WiFly.h> | |
#include <avr/pgmspace.h> | |
#define DELAY 100 | |
#define SPEED 20 | |
char passphrase[] = "digital6"; | |
char ssid[] = "APE-1349"; | |
const int clockPin = 3; //13 | |
const int latchPin = 2; //10 | |
const int dataPin1 = 6; | |
const int dataPin2 = 5; | |
const int dataPin3 = 4; | |
String tweetBuffer; | |
//byte arrow[8]={0x18,0x3C,0x66,0xDB,0x18,0x18,0x18,0x18}; | |
byte displayBuffer[24]= {0}; | |
prog_uchar fontDefinitions[475] PROGMEM = { | |
0x00,0x00,0x00,0x00,0x00,/*space*/ // is 32 in ASCII | |
0x00,0xF6,0xF6,0x00,0x00,/*!*/ | |
0x00,0xE0,0x00,0xE0,0x00,/*"*/ | |
0x28,0xFE,0x28,0xFE,0x28,/*#*/ | |
0x00,0x64,0xD6,0x54,0x08,/*$*/ | |
0xC2,0xCC,0x10,0x26,0xC6,/*%*/ | |
0x4C,0xB2,0x92,0x6C,0x0A,/*&*/ | |
0x00,0x00,0xE0,0x00,0x00,/*'*/ | |
0x00,0x38,0x44,0x82,0x00,/*(*/ | |
0x00,0x82,0x44,0x38,0x00,/*)*/ | |
0x88,0x50,0xF8,0x50,0x88,/***/ | |
0x08,0x08,0x3E,0x08,0x08,/*+*/ | |
0x00,0x00,0x05,0x06,0x00,/*,*/ | |
0x08,0x08,0x08,0x08,0x08,/*-*/ | |
0x00,0x00,0x06,0x06,0x00,/*.*/ | |
0x02,0x0C,0x10,0x60,0x80,/*/*/ | |
0x7C,0x8A,0x92,0xA2,0x7C,/*0*/ | |
0x00,0x42,0xFE,0x02,0x00,/*1*/ | |
0x42,0x86,0x8A,0x92,0x62,/*2*/ | |
0x44,0x82,0x92,0x92,0x6C,/*3*/ | |
0x10,0x30,0x50,0xFE,0x10,/*4*/ | |
0xE4,0xA2,0xA2,0xA2,0x9C,/*5*/ | |
0x3C,0x52,0x92,0x92,0x0C,/*6*/ | |
0x80,0x86,0x98,0xE0,0x80,/*7*/ | |
0x6C,0x92,0x92,0x92,0x6C,/*8*/ | |
0x60,0x92,0x92,0x94,0x78,/*9*/ | |
0x00,0x00,0x36,0x36,0x00,/*:*/ | |
0x00,0x00,0x35,0x36,0x00,/*;*/ | |
0x10,0x28,0x44,0x82,0x00,/*<*/ | |
0x28,0x28,0x28,0x28,0x28,/*=*/ | |
0x00,0x82,0x44,0x28,0x10,/*>*/ | |
0x40,0x80,0x8A,0x90,0x60,/*?*/ | |
0x7C,0x82,0xBA,0xBA,0x62,/*@*/ | |
0x3E,0x48,0x88,0x48,0x3E,/*A*/ | |
0xFE,0x92,0x92,0x92,0x6C,/*B*/ | |
0x7C,0x82,0x82,0x82,0x44,/*C*/ | |
0xFE,0x82,0x82,0x82,0x7C,/*D*/ | |
0xFE,0x92,0x92,0x92,0x82,/*E*/ | |
0xFE,0x90,0x90,0x90,0x80,/*F*/ | |
0x7C,0x82,0x82,0x8A,0x4E,/*G*/ | |
0xFE,0x10,0x10,0x10,0xFE,/*H*/ | |
0x82,0x82,0xFE,0x82,0x82,/*I*/ | |
0x84,0x82,0xFC,0x80,0x80,/*J*/ | |
0xFE,0x10,0x28,0x44,0x82,/*K*/ | |
0xFE,0x02,0x02,0x02,0x02,/*L*/ | |
0xFE,0x40,0x20,0x40,0xFE,/*M*/ | |
0xFE,0x60,0x10,0x0C,0xFE,/*N*/ | |
0x7C,0x82,0x82,0x82,0x7C,/*O*/ | |
0xFE,0x90,0x90,0x90,0x60,/*P*/ | |
0x7C,0x82,0x82,0x86,0x7E,/*Q*/ | |
0xFE,0x90,0x98,0x94,0x62,/*R*/ | |
0x64,0x92,0x92,0x92,0x4C,/*S*/ | |
0x80,0x80,0xFE,0x80,0x80,/*T*/ | |
0xFC,0x02,0x02,0x02,0xFC,/*U*/ | |
0xF8,0x04,0x02,0x04,0xF8,/*V*/ | |
0xFC,0x02,0x0C,0x02,0xFC,/*W*/ | |
0xC6,0x28,0x10,0x28,0xC6,/*X*/ | |
0xC0,0x20,0x1E,0x20,0xC0,/*Y*/ | |
0x86,0x8A,0x92,0xA2,0xC2,/*Z*/ | |
0x00,0x00,0xFE,0x82,0x00,/*[*/ | |
0x00,0x00,0x00,0x00,0x00,/*this should be / */ | |
0x80,0x60,0x10,0x0C,0x02,/*]*/ | |
0x20,0x40,0x80,0x40,0x20,/*^*/ | |
0x01,0x01,0x01,0x01,0x01,/*_*/ | |
0x80,0x40,0x20,0x00,0x00,/*`*/ | |
0x04,0x2A,0x2A,0x2A,0x1E,/*a*/ | |
0xFE,0x12,0x22,0x22,0x1C,/*b*/ | |
0x1C,0x22,0x22,0x22,0x14,/*c*/ | |
0x1C,0x22,0x22,0x12,0xFE,/*d*/ | |
0x1C,0x2A,0x2A,0x2A,0x18,/*e*/ | |
0x10,0x7E,0x90,0x80,0x40,/*f*/ | |
0x18,0x25,0x25,0x25,0x1E,/*g*/ | |
0xFE,0x10,0x10,0x10,0x0E,/*h*/ | |
0x00,0x12,0x5E,0x02,0x00,/*i*/ | |
0x02,0x01,0x01,0x11,0x5E,/*j*/ | |
0xFE,0x08,0x08,0x14,0x22,/*k*/ | |
0x00,0x82,0xFE,0x02,0x00,/*l*/ | |
0x3E,0x20,0x1C,0x20,0x1E,/*m*/ | |
0x3E,0x20,0x20,0x20,0x1E,/*n*/ | |
0x1C,0x22,0x22,0x22,0x1C,/*o*/ | |
0x3F,0x24,0x24,0x24,0x18,/*p*/ | |
0x18,0x24,0x24,0x3F,0x01,/*q*/ | |
0x3E,0x10,0x20,0x20,0x10,/*r*/ | |
0x12,0x2A,0x2A,0x2A,0x04,/*s*/ | |
0x00,0x10,0x3C,0x12,0x04,/*t*/ | |
0x3C,0x02,0x02,0x02,0x3E,/*u*/ | |
0x30,0x0C,0x02,0x0C,0x30,/*v*/ | |
0x38,0x06,0x18,0x06,0x38,/*w*/ | |
0x22,0x14,0x08,0x14,0x22,/*x*/ | |
0x38,0x05,0x05,0x05,0x3E,/*y*/ | |
0x22,0x26,0x2A,0x32,0x22,/*z*/ | |
0x00,0x10,0x6C,0x82,0x82,/*{*/ | |
//0x00,0x00,0xFF,0x00,0x00,/*|*/ | |
0x04,0x02,0xFF,0x02,0x04,/*|, arrow*/ | |
0x82,0x82,0x6C,0x10,0x00,/*}*/ | |
0x08,0x10,0x18,0x08,0x10/*~*/ | |
}; | |
WiFlyClient client("simpletwitter.herokuapp.com", 80); | |
void setup() { | |
pinMode(clockPin, OUTPUT); | |
pinMode(latchPin, OUTPUT); | |
pinMode(dataPin1, OUTPUT); | |
pinMode(dataPin2, OUTPUT); | |
pinMode(dataPin3, OUTPUT); | |
Serial.begin(115200); | |
WiFly.begin(); | |
if (!WiFly.join(ssid, passphrase)) { | |
Serial.println("Association failed."); | |
while (1); // Hang on failure. | |
} | |
} | |
void loop() { | |
getTweetsToBuffer(); | |
//Serial.println(sBuffer); | |
for(int i=0; i<5; i++) { | |
newTweetTone(); | |
delay(1000); | |
//Serial.println(tweet); | |
//delay(1000); | |
//displayTweet(); | |
displayTweet(getTweetfromBody(i)); | |
addSpace(); | |
} | |
//displayString("This is a scrolling LED matrix display. You can easily make your own! ", 70); | |
} | |
void scrollBufferLeft() { | |
for(int i=23; i>=0; i--) { | |
displayBuffer[i+1] = displayBuffer[i]; | |
} | |
} | |
void addCharSliceToBuffer(byte c, byte pos) { | |
displayBuffer[0] = pgm_read_byte_near(fontDefinitions + ((c - 32) * 5) + pos); | |
} | |
void displayTweet(String displayString) { | |
for(int k = 0; k < displayString.length(); k++) { | |
for(int j = 0; j < 5; j++) { | |
addCharSliceToBuffer(displayString[k], j); | |
for(int i=0; i < SPEED; i++) drawImage(displayBuffer); | |
scrollBufferLeft(); | |
} | |
addCharSliceToBuffer(' ', 0); | |
for(int i=0; i < SPEED; i++) drawImage(displayBuffer); | |
scrollBufferLeft(); | |
} | |
} | |
void addSpace() { | |
for(int j=0; j<16; j++) { | |
addCharSliceToBuffer(' ', 0); | |
for(int i=0; i < SPEED; i++) drawImage(displayBuffer); | |
scrollBufferLeft(); | |
} | |
} | |
void drawImage(byte * matrix) { | |
// Draw each column of the buffer, one by one. | |
for(int i=0; i<8; i++) { | |
// Shift out the position of the column, active high values. | |
for(int j=0; j<8; j++) { | |
digitalWrite(clockPin, LOW); | |
digitalWrite(dataPin1, (1<<i) & (1<<j) ); | |
digitalWrite(dataPin2, (1<<i) & (1<<j) ); | |
digitalWrite(dataPin3, (1<<i) & (1<<j) ); | |
digitalWrite(clockPin, HIGH); | |
} | |
// Shift out the position of the column, active low values. | |
for(int j=0; j<8; j++) { | |
digitalWrite(clockPin, LOW); | |
digitalWrite(dataPin1, (~(matrix[i+16])) & (1<<j) ); | |
digitalWrite(dataPin2, (~(matrix[i+8])) & (1<<j) ); | |
digitalWrite(dataPin3, (~(matrix[i])) & (1<<j) ); | |
digitalWrite(clockPin, HIGH); | |
} | |
// Toggle latch pin to display sent data. | |
digitalWrite(latchPin,LOW); | |
digitalWrite(latchPin,HIGH); | |
} | |
} | |
void getTweetsToBuffer() { | |
Serial.println("Getting tweets..."); | |
if (client.connect()) { | |
Serial.println("Connected."); | |
client.println("GET / HTTP/1.0"); | |
client.println("Host: simpletwitter.herokuapp.com"); | |
client.println(); | |
} else { | |
Serial.println("Error."); | |
} | |
String sBuffer; | |
while (client.connected()) { | |
if(client.available()) { | |
sBuffer += (char) client.read(); | |
//Serial.print(c); | |
} | |
} | |
//Serial.println(buffer); | |
Serial.println(); | |
Serial.println("Disconnecting."); | |
client.stop(); | |
tweetBuffer = sBuffer.substring(sBuffer.indexOf("\r\n\r\n") + 4); | |
} | |
String getTweetfromBody(byte num) { | |
int startIndex = 0; | |
while(num > 0) { | |
startIndex = tweetBuffer.indexOf("\n", startIndex) + 1; | |
//Serial.println(startIndex); | |
num--; | |
} | |
int startSearch = tweetBuffer.indexOf(" ", startIndex) + 1; | |
int endSearch = tweetBuffer.indexOf("\n", startIndex); | |
String tweet = tweetBuffer.substring(startSearch, endSearch); | |
//Serial.println(tweet); | |
return tweet; | |
} | |
void newTweetTone() { | |
tone(8, 2000, DELAY); | |
delay(DELAY); | |
tone(8, 2200, DELAY); | |
delay(DELAY); | |
tone(8, 2400, DELAY); | |
delay(DELAY); | |
delay(DELAY); | |
tone(8, 2400, DELAY); | |
delay(DELAY); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment