Step 4: Seeed and Twitter

box_1.png
P1150813.JPG
P1150814.JPG

1. Cut out holes for the USB and Ethernet and maybe a hole for the power jack if you want to power the arduino that way.

2.  Create a 2.5in by 2.5in by 2.5in box of velum with the 2.5in by 5in bottom open

3. Tape the vellum to the top of your box over the BlinkM and secure all the parts

4. Download the TrueRandom http://code.google.com/p/tinkerit/wiki/TrueRandom,
NewSoftSerial http://arduiniana.org/libraries/newsoftserial/,
and Twitter http://www.arduino.cc/playground/Code/TwitterLibrary libraries

5. Upload this code to your Arduino

#include <TrueRandom.h>

#include <SPI.h>

#include <avr/pgmspace.h>

#include <Wire.h>

#include <BlinkM_funcs.h>

#include <Client.h>

#include <Ethernet.h>

#include <Server.h>

#include <Udp.h>

#include <EthernetDNS.h>

#include <Twitter.h>


/*
 based on Kittty Twitty Cat Toy v1.0
 by Marc de Vinck
 Jan 6, 2010
 KittyTwitty cat toy project found in MAKE, Volume 22

 String (formerly TextString) Library by Tom Igoe
 http://www.arduino.cc/en/Tutorial/TextString
 */


// the push button
#define pushBtn 6


boolean printed=true;

int color;

volatile long reader1 = 0;
volatile int reader1Count = 0;

#define blue 57337134
#define red 23773468
#define green 23805695

long prevTime;
int card1;
int card2;
int count=0;

long dec;

int r = 0;
int g = 0;
int b=0;

#define addr 0x09

// used to store the status of pin (6)
int var; 

long randNum1;
long randNum2;
long randNum3;
long randNum4;

String dataString;
long prevtime = -30000;

// defining the network setting for the Ethernet Shield

// this can be made up
byte mac[] = {
0x1A, 0x6F, 0x99, 0xCD, 0xFF, 0xFF }; 

//get this from Settings, then change last byte
byte ip[] = {
10,0,1,197 };

//to get token: http://arduino-tweet.appspot.com/
Twitter twitter("[your_token_here]");


prog_char words1_0[] PROGMEM = "Red ";
prog_char words1_1[] PROGMEM = "Green ";
prog_char words1_2[] PROGMEM = "Blue ";
prog_char words1_3[] PROGMEM = "Yellow ";
prog_char words1_4[] PROGMEM = "Purple ";
prog_char words1_5[] PROGMEM = "Cyan ";
prog_char words1_6[] PROGMEM = "White ";

prog_char words2_0[] PROGMEM = "lights ";
prog_char words2_1[] PROGMEM = "arts ";
prog_char words2_2[] PROGMEM = "gases ";
prog_char words2_3[] PROGMEM = "circles ";
prog_char words2_4[] PROGMEM = "needles ";
prog_char words2_5[] PROGMEM = "doorknobs ";
prog_char words2_6[] PROGMEM = "sirens ";

prog_char words3_0[] PROGMEM = "sleep. ";
prog_char words3_1[] PROGMEM = "walk. ";
prog_char words3_2[] PROGMEM = "live. ";
prog_char words3_3[] PROGMEM = "exist. ";
prog_char words3_4[] PROGMEM = "perceive. ";
prog_char words3_5[] PROGMEM = "think. ";
prog_char words3_6[] PROGMEM = "see. ";

prog_char words4_0[] PROGMEM = "Cats are awesome.";
prog_char words4_1[] PROGMEM = "Quilts taste like gingerbread.";
prog_char words4_2[] PROGMEM = "Metal is highly interactive.";
prog_char words4_3[] PROGMEM = "Light is radiation.";
prog_char words4_4[] PROGMEM = "Sound is the wind.";
prog_char words4_5[] PROGMEM = "Clocks make a good bed.";
prog_char words4_6[] PROGMEM = "Glass is opaque.";

PROGMEM const char *words1_table[] ={
words1_0,
words1_1,
words1_2,
words1_3,
words1_4,
words1_5,
words1_6};

PROGMEM const char *words2_table[]={
words2_0,words2_1,words2_2,words2_3,words2_4,words2_5,words2_6};

PROGMEM const char *words3_table[]={
words3_0,words3_1,words3_2,words3_3,words3_4,words3_5,words3_6};

PROGMEM const char *words4_table[]={
words4_0,words4_1,words4_2,words4_3,words4_4,words4_5,words4_6};

char words1_buffer[7];
char words2_buffer[10];
char words3_buffer[10];
char words4_buffer[30];


void reader1One(void) {
     reader1Count++;
     reader1 = reader1 << 1;
     reader1 |= 1;
}


void reader1Zero(void) {
     reader1Count++;
     reader1 = reader1 << 1;
}



void setup(){
     // run this code once
     BlinkM_beginWithPower();

     //defining the btn as an input so we can read it
     pinMode(pushBtn, INPUT);

     // starts serial communications so you can debug easier
     Serial.begin(9600); 
    
     //DATA0 to pin 2
     attachInterrupt(0, reader1Zero, RISING);

     //DATA1 to pin 3
     attachInterrupt(1, reader1One, RISING); 

     for(int i = 2; i<4; i++){
         pinMode(i, OUTPUT);
         digitalWrite(i, HIGH); // enable internal pull up causing a one
         digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
         pinMode(i, INPUT);
          digitalWrite(i, HIGH); // enable internal pull up
     }

    reader1 = 0;
    reader1Count = 0;

     //begins the Ethernet connection from the stored information
     Ethernet.begin(mac, ip); 

     connectToTwitter();

     Serial.println("Waiting"); // print, used for debugging
}


void connectToTwitter(){
     // print a blank line, used for debugging
     Serial.println("Connecting to Twitter..."); // print, used for debugging

     char testFN1[4]={int(random(4000,5000)) };

     if (twitter.post(testFN1)) { // Twitter that we are up and running
         int status = twitter.wait(&Serial); // wait for a response from twitter
          if (status == 200) { // if Twitter responds 200
               Serial.println("Tweet OK!"); // print success // print a blank line, used for debugging

          }else {
              Serial.print("Tweet failed : code ");
              Serial.println(status); // print error code
              connectToTwitter(); // print a blank line, used for debugging
          }
     }else {
           Serial.println("connection failed.");
           connectToTwitter();
     }
}

void loop(){
     // run over and over, never stop
     readCard();
    displayColor();

    // check status of wire sensor
    checkState(); 
}

void getString(){
     //concatenates poem
    dataString= strcpy_P(words1_buffer, (char*)pgm_read_word(&(words1_table[color])));
    dataString=dataString+ strcpy_P(words2_buffer,   (char*)pgm_read_word(&(words2_table[int(randNum2)])));

     dataString=dataString+ strcpy_P(words3_buffer, (char*)pgm_read_word(&(words3_table[int(randNum3)])));

     dataString=dataString+ strcpy_P(words4_buffer, (char*)pgm_read_word(&(words4_table[int(randNum4)])));

     dataString=dataString+ " - 1000000000";
}



void tweet(){
     // function tweet, this is called if status = 1
     char filename[88];

     dataString.toCharArray(filename, 88);

      Serial.println("Connecting to Twitter..."); // print, used for debugging
      Serial.println(); // print a blank line, used for debugging
      if (twitter.post(filename)) {
           // tweet the completed datastring of words
          Serial.print("Tweeting -- "); // print, used for debugging
          Serial.println(dataString); // print, used for debugging
          Serial.print(" -- Status: "); // print, used for debugging
         int status = twitter.wait();
         if (status == 200) {
               Serial.println("Successful!");
               Serial.println();
         }else {
              Serial.print("Tweet failed : code "); // print error code
              Serial.println(status); // print error code
         }
    }else {
          Serial.println("Connection to Twitter failed."); // print error code
    }
}





void readCard(){
     if(count<2){
          if(reader1Count >=26){
              int serialNumber=(reader1 >> 1) & 0x3fff;
              int siteCode= (reader1 >> 17) & 0x3ff;
             dec = (reader1& 0xfffffff);
             if(count==0){
                r=0;
                b=0;
                g=0;
             }
          
           Serial.print("dec=");
           Serial.println(dec);


         switch(dec){
         case red:
             r=255;
             break;
      
         case green:
               g=255;
               break;

          case blue:
                b=255;
                break;

          default:
              color=0;

              Serial.print("new tag");
              Serial.print(reader1& 0xfffffff);
           }

           reader1 = 0;
           reader1Count = 0;
           printed = false;
        }else if(reader1Count == 0){

     }
    if(millis()-prevTime>2000){
          count++;
          prevTime=millis();
     }

   }else{
         count=0;
   }

}//readCard()


void checkState(){ // check status of wire function
     if(!printed){
          doStuff();
          printed=true;
       }
}



void displayColor(){
    if(r==255 && g==0 && b==0){
         color=0;
     } else if(r==0 && g==255 && b==0){
         color=1;
     } else if(r==0 && g==0 && b==255){
         color=2;
     }else if(r==255 && g==255 && b==0){
          color=3;
     } else if(r==255 && g==0 && b==255){
         color=4;
     } else if(r==0 && g==255 && b==255){
        color=5;
     } else if(r==255 && g==255 && b==255){
        color=6;
    }
    BlinkM_fadeToRGB(addr,r,g,b);
}


void doStuff(){
     randNum2 = random(7);
     randNum3 = random(7);
     randNum4 = random(7);
     getString();
     tweet();
}





5. Connect your Arduino to the ethernet 

6. Test.
 
Remove these adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!