Paso 6: Todo el programa
El código fuente todo, es open source, utilizando la licencia de Creative Commons Attribution y otro heredado de licencias desde el IDE de Arduino y bibliotecas.
/* Tweeting pet dishes ver 0.5b June 25, 2011 This code uses NeoCat's Twitter library and the Ethernet Shield. See the Instructable for more information by Daniel Gentleman for the Adafruit/Instructables Make It Tweet Challenge. */ #if defined(ARDUINO) && ARDUINO > 18 // Arduino 0019 or later #include #endif #include #include #include // The MAC address is on a sticker on the Ethernet shield. byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x3E, 0x28 }; byte ip[] = { 192,168,1, 222 }; // Your Token to Tweet (get it from http://arduino-tweet.appspot.com/) Twitter twitter("YOUR TOKEN HERE"); // Variables variables variables const int foodPin = A2; // Analog 0 for measuring the food weight const int ledPin = 3; // LED const int waterPin = 2; // digital 9 for water sensor int foodWeight; // Variable for storing food weight //const int ledPin = 13; // LED pin for water level int waterState = 0; // Variable for setting water state int lastWaterState = 0; // Variable for last water state const int lowFood = 25; // if the food level is below this, it will tweet int lastFoodWeight; // Last Food State placeholder int foodTweet = 0; // int foodValue = 0; // char msg[125]; // make a nice fat buffer (125 characters) for tweets int waterAlarmState = 0; // Have we reported water yet? int foodAlarmState = 0; // Have we reported an empty bowl? String tweet = "Starting Up" ; void setup() { delay(1000); Ethernet.begin(mac, ip); Serial.begin(9600); pinMode(waterPin, INPUT); pinMode(foodPin, INPUT); pinMode(ledPin, OUTPUT); delay(1000); foodWeight = getFood(); waterState = getWater(); } const int analogInPin = A2; // Analog input pin that the potentiometer is attached to const int analogOutPin = 13; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) void loop(){ lastFoodWeight = foodWeight; foodWeight = getFood(); if (foodWeight > (lastFoodWeight + 20)){ //If we fed the pets at least 20 String gotfed = "Thanks for feeding me! My bowl is now "; String tweet = gotfed + foodWeight + "% full!"; Serial.println(tweet); postTweet(tweet); foodAlarmState = 0; // Clears food alarm if it was below critical } if (foodWeight < (lastFoodWeight - 10)){ // If the pets ate at least 10 int ateweight = lastFoodWeight - foodWeight; if (ateweight > 100){ ateweight = 0; } String atefood = "I just ate "; String tweet = atefood + ateweight +"% of my food!"; Serial.println(tweet); postTweet(tweet); } if (foodWeight < lowFood){ // If the food is beneath the low threshold if (foodAlarmState != 1){ String foodwarning = "I am almost out of food! I have only "; String tweet = foodwarning + foodWeight + " percent left!"; Serial.println(tweet); postTweet(tweet); foodAlarmState = 1; } } if (getWater() == 0){ if (waterAlarmState != 1){ String waterPre = "My food is at "; String tweet = waterPre + foodWeight + "% but my water is low!"; Serial.println(tweet); postTweet(tweet); waterAlarmState = 1; // Sets water alarm state (so it won't tweet forever) } } if (waterState > lastWaterState){ String tweet = "Thanks for the water!"; Serial.println(tweet); postTweet(tweet); waterAlarmState = 0; // Clears water alarm state } delay(2000); // Regular loop delay } int getFood() { // read the analog in value: int foodValue = analogRead(foodPin); // first sample Serial.print(foodValue); if (foodValue >= 600) { foodValue = 599; } if (foodValue <= 582) { foodValue = 583; } int bowlPercent = map(foodValue, 583, 599, 1, 100); Serial.print("food level = " ); Serial.println(bowlPercent); return bowlPercent; } int getWater(){ lastWaterState = waterState; waterState = digitalRead(waterPin); if (waterState == HIGH) { Serial.println("Water is LOW"); waterState = 0; digitalWrite(ledPin, HIGH); return 0; } else{ Serial.println("Water is good"); waterState = 1; digitalWrite(ledPin, LOW); return 1; } } void postTweet(String tweet){ String termTweet = tweet + "\0" ;// Terminate the tweet with a null Serial.print(termTweet); Serial.println(" - Terminated tweet"); int twtlen = (termTweet.length()+3); // count the characters, add 3 just in case Serial.print(twtlen); Serial.println(" - Tweet length"); termTweet.toCharArray(msg,twtlen); // Convert it to an array called msg Serial.print("Attempted tweet "); Serial.println(msg); // tweet.toCharArray(msg,twtlen); Serial.println("connecting ..."); if (twitter.post(msg)) { // Specify &Serial to output received response to Serial. // If no output is required, you can just omit the argument, e.g. // int status = twitter.wait(); int status = twitter.wait(&Serial); if (status == 200) { Serial.println("OK."); delay (120000); // Wait two minutes before doing anything after a tweet (don't spam!) } else { Serial.print("failed : code "); Serial.println(status); } } else { Serial.println("connection failed."); } }