Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Kitty Twitty Arduino Code

By: Treeloy on Jul 25th, 2013  |  syntax: None  |  size: 6.17 KB  |  views: 93  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  
  3.  Kittty Twitty Cat Toy v1.0
  4.  by Marc de Vinck
  5.  Jan 6, 2010
  6.  
  7.  Updated by Eloy Salinas to work with NeoCats library
  8.  July 27, 2013
  9.  
  10.  For KittyTwitty cat toy project found in MAKE, Volume 22
  11.  
  12.  This project continues to evolve online at: wwwmakezine.com/kittytwitty
  13.  
  14.  Twitter Library for Arduino V1.0.1 created by NeoCat
  15.  http://www.arduino.cc/playground/Code/TwitterLibrary
  16.  
  17.  
  18.  */
  19.  
  20. //these are libraries that need to be included for the program to work, see links above
  21. #include <SPI.h>
  22. #include <Ethernet.h>    // this allows us to use the Ethernet shield easily
  23. #include <Twitter.h>     // this allows us to easily talk to Twitter
  24.  
  25. // defining the network setting for the Ethernet Shield
  26. //Only use ip, gateway, and subnet if not usuing auto DHCP
  27. byte mac[] = {
  28.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // this can be made up
  29. byte ip[] = {
  30.   0, 0, 0, 0 }; // a free IP address on your network
  31. byte gateway[] = {
  32.   0, 0, 0, 0 }; // the gateway address of your network
  33. byte subnet[] = {
  34.   255, 255, 255, 0 };  // the subnet mask of your network
  35.  
  36. //Twitter Authentication
  37. Twitter twitter("YourTokenHere");  // this was YourID:Password in 1.0.1
  38.  
  39. // these constants will not change
  40.  
  41. const int powerPin = 9;  //  power LED pin
  42. const int statusPin = 8;  //  status LED pin
  43. const int wirePin = 6;  //  the pin that the guitar wire is connected to
  44.  
  45. // These are variables that will change
  46.  
  47. int var;  // used to store the status of pin (6)
  48.  
  49. long randNum1;  // variable that will store random number 1
  50. long randNum2;  // variable that will store random number 2
  51. long randNum3;  // variable that will store random number 3
  52.  
  53.  
  54.   String msg;
  55.   char dataString[100];
  56.  
  57.  
  58.   char* words1[] = {
  59.   "Chester ","The cat ", "Your little buddy ", "Monster cat ", "Bad cat ", "Furball "};       // array of words for   the first section of the sentence
  60.   char* words2[] = {
  61.   "loves", "pwned", "destroyed", "pownced on", "attacked", "stalked"}; // array of words for the second         section of the sentence
  62.   char* words3[] = {
  63.   "Meow!", "Prrrrrr", ">^,,^<", "Yowzers!", "Mwahahaha!" };  
  64.  
  65. void setup(){    // run this code once
  66.  
  67.   pinMode(wirePin, INPUT);        //defining the wire pin as an input so we can read it
  68.   pinMode(powerPin, OUTPUT);      // sets the pin as output
  69.   pinMode(statusPin, OUTPUT);      // sets the pin as output
  70.  
  71.   digitalWrite(powerPin, HIGH);   // sets the power LED (green one) on
  72.  
  73.   //If not using auto DHCP use Ethernet.begin(mac, ip, gateway, subnet);
  74.   Ethernet.begin(mac);  //begins the Ethernet connection and uses automatic DHCP
  75.   Serial.begin(9600);              // starts serial communications so we can debug easier
  76.   delay(2000);        // a 1 second delay to let everything settle down!
  77.  
  78.   // let's tweet that we are up and running
  79.  
  80.   Serial.println("Kitty Twitty is up and running!");  // print, used for debugging
  81.   Serial.println();                                    // print a blank line, used for debugging
  82.   Serial.println("Connecting to Twitter...");          // print, used for debugging
  83.  
  84.   digitalWrite(statusPin, HIGH);   // sets the status LED off
  85.  
  86.   if (twitter.post("Kitty Twitty is up and running!")) {  // Twitter that we are up and running
  87.     int status = twitter.wait(&Serial);     // wait for a response from twitter                    
  88.     if (status == 200) {    // if Twitter responds 200
  89.       Serial.println("Tweet OK!");  // print success
  90.       Serial.println();  // print a blank line, used for debugging
  91.     }
  92.     else {
  93.       Serial.print("Tweet failed : code ");
  94.       Serial.println(status);  // print error code
  95.       Serial.println();  // print a blank line, used for debugging
  96.     }
  97.   }
  98.   else {
  99.     Serial.println("Connection to Twitter failed.");
  100.   }
  101.   delay (1000); // delay 1 second
  102.   digitalWrite(statusPin, LOW);   // sets the status LED off
  103.   Serial.println("Waiting for someone to play with me!"); // print, used for debugging
  104. }
  105.  
  106. void loop(){ // run over and over, never stop
  107.  
  108.     checkState();  // check status of wire sensor
  109. }
  110.  
  111.  
  112. void tweet(){  // function tweet, this is called if status = 1
  113.  
  114.   randNum1 = random(5);  //generate a random number from 0-5
  115.   randNum2 = random(5);  //generate a random number from 0-5
  116.   randNum3 = random(4);  //generate a random number from 0-4
  117.  
  118.   msg = "";
  119.   msg = words1[randNum1];
  120.   msg += words2[randNum2];
  121.   msg += " Kitty Twitty! ";
  122.   msg += words3[randNum3];
  123.  
  124.   msg.toCharArray(dataString, 100);
  125.  
  126.  
  127.  
  128.  
  129.   Serial.println("Connecting to Twitter...");  // print, used for debugging
  130.   Serial.println();     // print a blank line, used for debugging
  131.   if (twitter.post(dataString)) {  // tweet the completed datastring of words
  132.     Serial.print("Tweeting -- ");  // print, used for debugging
  133.     Serial.print(dataString);  // print, used for debugging
  134.     Serial.print(" -- Status: ");  // print, used for debugging
  135.     int status = twitter.wait(&Serial);
  136.     if (status == 200) {
  137.       Serial.println("Successful!");
  138.       Serial.println();
  139.     }
  140.     else {
  141.       Serial.print("Tweet failed : code "); // print error code
  142.       Serial.println(status);  // print error code
  143.     }
  144.   }
  145.   else {
  146.     Serial.println("Connection to Twitter failed."); // print error code
  147.   }
  148.   Serial.println("60 Second timeout started."); // print, used for debugging
  149.   Serial.println();  // print a blank line, used for debugging
  150.   delay (60000);  // this delay of 60 seconds after a tweet ensures we don't over-tweet (60/per hour)
  151.   digitalWrite(statusPin, LOW);   // sets the status LED off
  152.   Serial.println("Waiting for someone to play with me!"); // print, used for debugging
  153.   Serial.println();      // print a blank line, used for debugging
  154.  
  155. }
  156.  
  157. void checkState(){    // check status of wire function
  158.  
  159.   var = digitalRead(wirePin);  // read the status of the wire pin
  160.  
  161.     if (var==1){      // if the pin is HIGH (+ 5v)
  162.     digitalWrite(statusPin, HIGH);   // turn on the status LED
  163.     tweet();            // go to the Tweet function to send out a tweet
  164.     delay (2000);  // wait for 2 seconds
  165.     digitalWrite(statusPin, LOW);   // turn off the status LED
  166.   }
  167.   else{
  168.     digitalWrite(statusPin, LOW); // turn off the status LED
  169.   }
  170. }
clone this paste RAW Paste Data