/*
Kittty Twitty Cat Toy v1.0
by Marc de Vinck
Jan 6, 2010
Updated by Eloy Salinas to work with NeoCats library
July 27, 2013
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
*/
//these are libraries that need to be included for the program to work, see links above
#include <SPI.h>
#include <Ethernet.h> // this allows us to use the Ethernet shield easily
#include <Twitter.h> // this allows us to easily talk to Twitter
// defining the network setting for the Ethernet Shield
//Only use ip, gateway, and subnet if not usuing auto DHCP
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // this can be made up
byte ip[] = {
0, 0, 0, 0 }; // a free IP address on your network
byte gateway[] = {
0, 0, 0, 0 }; // the gateway address of your network
byte subnet[] = {
255, 255, 255, 0 }; // the subnet mask of your network
//Twitter Authentication
Twitter twitter("YourTokenHere"); // this was YourID:Password in 1.0.1
// 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
String msg;
char dataString[100];
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!" };
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
//If not using auto DHCP use Ethernet.begin(mac, ip, gateway, subnet);
Ethernet.begin(mac); //begins the Ethernet connection and uses automatic DHCP
Serial.begin(9600); // starts serial communications so we can debug easier
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(&Serial); // 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(5); //generate a random number from 0-5
randNum2 = random(5); //generate a random number from 0-5
randNum3 = random(4); //generate a random number from 0-4
msg = "";
msg = words1[randNum1];
msg += words2[randNum2];
msg += " Kitty Twitty! ";
msg += words3[randNum3];
msg.toCharArray(dataString, 100);
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(&Serial);
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("60 Second timeout started."); // print, used for debugging
Serial.println(); // print a blank line, used for debugging
delay (60000); // this delay of 60 seconds after a tweet ensures we don't over-tweet (60/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
}
}