/* Kittty Twitty Cat Toy v1.0 by Marc de Vinck Jan 6, 2010 For KittyTwitty cat toy project found in MAKE, Volume 22 This project continues to evolve online at: wwwmakezine.com/kittytwitty Twitter Library for Arduino V1.0.1 created by NeoCat http://www.arduino.cc/playground/Code/TwitterLibrary String (formerly TextString) Library by Tom Igoe http://www.arduino.cc/en/Tutorial/TextString */ //these are libraries that need to be included for the program to work, see links above #include // this allows us to use the Ethernet shield easily #include // this allows us to easily talk to Twitter #include // this allows us to easily chain a string of words together // these constants will not change const int powerPin = 9; // power LED pin const int statusPin = 8; // status LED pin const int wirePin = 6; // the pin that the guitar wire is connected to // These are variables that will change int var; // used to store the status of pin (6) long randNum1; // variable that will store random number 1 long randNum2; // variable that will store random number 2 long randNum3; // variable that will store random number 3 // define a a new string of data String dataString; // defining the network setting for the Ethernet Shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // this can be made up byte ip[] = { 192, 168, 2, 7 }; // a free IP address on your network byte gateway[] = { 192, 168, 2, 1 }; // the gateway address of your network byte subnet[] = { 255, 255, 255, 0 }; // the subnet mask of your network Twitter twitter("user:password"); // defines your username:followed by your password char* words1[] = { "Chester ","The cat ", "Your little buddy ", "Monster cat ", "Bad cat ", "Furball "}; // array of words for the first section of the sentence char* words2[] = { "loves", "pwned", "destroyed", "pownced on", "attacked", "stalked"}; // array of words for the second section of the sentence char* words3[] = { "Meow!", "Prrrrrr", ">^,,^<", "Yowzers!", "Mwahahaha!" }; // array of words for the final section of the sentence void setup(){ // run this code once pinMode(wirePin, INPUT); //defining the wire pin as an input so we can read it pinMode(powerPin, OUTPUT); // sets the pin as output pinMode(statusPin, OUTPUT); // sets the pin as output digitalWrite(powerPin, HIGH); // sets the power LED (green one) on Serial.begin(9600); // starts serial communications so we can debug easier Ethernet.begin(mac, ip, gateway, subnet); //begins the Ethernet connection from the stored information delay(2000); // a 1 second delay to let everything settle down! // let's tweet that we are up and running Serial.println("Kitty Twitty is up and running!"); // print, used for debugging Serial.println(); // print a blank line, used for debugging Serial.println("Connecting to Twitter..."); // print, used for debugging digitalWrite(statusPin, HIGH); // sets the status LED off if (twitter.post("Kitty Twitty is up and running!")) { // Twitter that we are up and running int status = twitter.wait(); // wait for a response from twitter if (status == 200) { // if Twitter responds 200 Serial.println("Tweet OK!"); // print success Serial.println(); // print a blank line, used for debugging } else { Serial.print("Tweet failed : code "); Serial.println(status); // print error code Serial.println(); // print a blank line, used for debugging } } else { Serial.println("Connection to Twitter failed."); } delay (1000); // delay 1 second digitalWrite(statusPin, LOW); // sets the status LED off Serial.println("Waiting for someone to play with me!"); // print, used for debugging } void loop(){ // run over and over, never stop checkState(); // check status of wire sensor } void tweet(){ // function tweet, this is called if status = 1 randNum1 = random(6); //generate a random number from 0-6 randNum2 = random(6); //generate a random number from 0-6 randNum3 = random(5); //generate a random number from 0-5 dataString = words1[randNum1]; // datastring is a word from the array word1, selects based on random nuber generated dataString.append(words2[randNum2]); // add to the datastring a word from the array word2, selects based on random nuber generated dataString.append(" Kitty Twitty! "); // add to the datastring "Kitty Twitty!" dataString.append(words3[randNum3]); // add to the datastring a word from the array word1, selects based on random nuber generated Serial.println("Connecting to Twitter..."); // print, used for debugging Serial.println(); // print a blank line, used for debugging if (twitter.post(dataString)) { // tweet the completed datastring of words Serial.print("Tweeting -- "); // print, used for debugging Serial.print(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 } Serial.println("30 Second timeout started."); // print, used for debugging Serial.println(); // print a blank line, used for debugging delay (30000); // this delay of 30 seconds after a tweet ensures we don't over-tweet (150/per hour) digitalWrite(statusPin, LOW); // sets the status LED off Serial.println("Waiting for someone to play with me!"); // print, used for debugging Serial.println(); // print a blank line, used for debugging } void checkState(){ // check status of wire function var = digitalRead(wirePin); // read the status of the wire pin if (var==1){ // if the pin is HIGH (+ 5v) digitalWrite(statusPin, HIGH); // turn on the status LED tweet(); // go to the Tweet function to send out a tweet delay (2000); // wait for 2 seconds digitalWrite(statusPin, LOW); // turn off the status LED } else{ digitalWrite(statusPin, LOW); // turn off the status LED } }