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

arduino washing machine

By: a guest on Aug 5th, 2011  |  syntax: C  |  size: 2.46 KB  |  views: 3,162  |  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. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <Udp.h>
  4.  
  5.  
  6. byte mac[] = {
  7.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  8. byte ip[] = {
  9.   192, 168, 1, 177 };
  10. byte gateway[] = {
  11.   192, 168, 1, 1 };
  12. byte subnet[] = {
  13.   255, 255, 255, 0 };
  14. const int LED = A2;
  15. long laundryIsDoneSince = -1;
  16. boolean LEDon;
  17. Server server(80);
  18.  
  19.  
  20. void setup()
  21. {
  22.   Ethernet.begin(mac, ip, gateway, subnet);
  23.   server.begin();
  24.   Serial.begin(9600);
  25.   pinMode(LED, INPUT);
  26. }
  27.  
  28.  
  29. void loop()
  30. {
  31.   // listen for incoming clients
  32.   Client client = server.available();
  33.  
  34.   // Signal from washing machine (LED) is unstable
  35.   // so we have to watch it for some time
  36.   LEDon = false;
  37.   long start = millis();
  38.   while ((start+100)>millis()){
  39.     if(!digitalRead(LED)) {
  40.       LEDon = true;
  41.       break;
  42.     }
  43.   }
  44.  
  45.   if (LEDon)
  46.   {
  47.     if (laundryIsDoneSince==-1){
  48.       laundryIsDoneSince = millis();
  49.     }
  50.     Serial.println("LED is on");
  51.   }
  52.   else {
  53.     laundryIsDoneSince=-1;
  54.     Serial.println("LED is off");
  55.   }
  56.  
  57.   if (client) {
  58.     // Some code is taken frpm the official HTTPServer example
  59.     boolean currentLineIsBlank = true;
  60.     while (client.connected()) {
  61.       if (client.available()) {
  62.         char c = client.read();
  63.         if (c == '\n' && currentLineIsBlank) {
  64.           client.println("HTTP/1.1 200 OK");
  65.           client.println("Content-Type: text/html");
  66.           client.println();
  67.           client.println("<br /><br /><br /><br /><br /><br /><br /><br />");
  68.           client.println("<table align=center><tr><td align=center>");
  69.           if (LEDon) {
  70.             long minutesDone = ((millis()-laundryIsDoneSince)/1000/60)+1;
  71.             client.print("<font color='#00C000' size='13'><b>&#9786;</b></font>"); // Smiley :)
  72.             client.print("<br /><br />");
  73.             Serial.println(minutesDone);
  74.             if (minutesDone > 1) {
  75.               client.print("...since ");
  76.               client.print(minutesDone);
  77.               client.print(" minutes!");            
  78.             }
  79.           }
  80.           else {
  81.             client.print("<font color='#Co0000' size='6'><b>Nope, not yet...</b></font>");
  82.           }
  83.           client.println("</td></tr></table>");
  84.           break;
  85.         }
  86.         if (c == '\n') {
  87.           currentLineIsBlank = true;
  88.         }
  89.         else if (c != '\r') {
  90.           currentLineIsBlank = false;
  91.         }
  92.       }
  93.     }
  94.     // close the connection:
  95.     client.stop();
  96.   }
  97.   delay(2000);
  98. }