Advertisement
bld

D.I.Y internet controlled living room ambient light

bld
Jan 14th, 2012
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.95 KB | None | 0 0
  1. #define fadeTime 1000   //How many milliseconds fading should take
  2. #define tempSetSecs 60  //How many seconds we want the temp setting to stay
  3.  
  4. #define pinR 3
  5. #define pinG 5
  6. #define pinB 6
  7.  
  8. #define EthernetResetPin 2
  9.  
  10. //Used for ethernet
  11. #include <Ethernet.h>
  12. #include <SPI.h>
  13. byte mac[] = {
  14.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  15. byte ip[] = {
  16.   192, 168, 1, 50 };
  17. byte gw[] = {
  18.   192, 168, 1, 1 };
  19. byte subnet[] = {
  20.   255, 255, 255, 0 };
  21. Server server(80);
  22.  
  23.  
  24. //Color values
  25. unsigned int stickR = 0;
  26. unsigned int stickG = 0;
  27. unsigned int stickB = 0;
  28.  
  29. unsigned int tempR = 0;
  30. unsigned int tempG = 0;
  31. unsigned int tempB = 0;
  32. boolean tempActive = false;
  33.  
  34. //Boolean to define if the pwm should be changed
  35. boolean updatePWM = true;
  36.  
  37. //When the temp color was set
  38. unsigned long tempSet = 0;
  39.  
  40. //Smoothing of colors
  41. #define numReadings 100
  42.  
  43. unsigned int S_index = 0;
  44.  
  45. unsigned int R_readings[numReadings];
  46. unsigned int R_total = 0;
  47. unsigned int R_average = 0;
  48.  
  49. unsigned int G_readings[numReadings];
  50. unsigned int G_total = 0;
  51. unsigned int G_average = 0;
  52.  
  53. unsigned int B_readings[numReadings];
  54. unsigned int B_total = 0;
  55. unsigned int B_average = 0;
  56.  
  57. unsigned long lastSmooth = 0;
  58. int smoothSteps = 1;
  59. #define smoothDelay (fadeTime-500)/numReadings
  60.  
  61. //#define debug 1
  62.  
  63. void setup()
  64. {  
  65.   for (int thisReading = 0; thisReading < numReadings; thisReading++)
  66.   {
  67.     R_readings[thisReading] = 0;
  68.     G_readings[thisReading] = 0;
  69.     B_readings[thisReading] = 0;
  70.   }
  71.  
  72.   pinMode(pinR, OUTPUT);
  73.   pinMode(pinG, OUTPUT);
  74.   pinMode(pinB, OUTPUT);
  75.  
  76.   pinMode(EthernetResetPin, OUTPUT);
  77.   digitalWrite(EthernetResetPin, LOW);  
  78.   digitalWrite(EthernetResetPin, HIGH);
  79.  
  80.   Ethernet.begin(mac, ip, gw, subnet);
  81.   server.begin();
  82.  
  83. #ifdef debug
  84.   Serial.begin(9600);
  85. #endif
  86. }
  87.  
  88. void loop()
  89. {
  90.   if ((millis() - tempSet) >= (tempSetSecs*1000UL) && tempActive)
  91.  {
  92.    smoothSteps = numReadings;
  93.    tempActive = false;
  94.  }  
  95.  
  96.   if ((millis()-lastSmooth) >= smoothDelay && smoothSteps > 0)
  97.   {
  98.     R_total -= R_readings[S_index];
  99.     G_total -= G_readings[S_index];
  100.     B_total -= B_readings[S_index];
  101.  
  102.     if (tempSet == 0 || (millis() - tempSet) >= (tempSetSecs*1000UL))
  103.     {
  104.       R_readings[S_index] = stickR;
  105.       G_readings[S_index] = stickG;
  106.       B_readings[S_index] = stickB;
  107.     }
  108.     else
  109.     {
  110.       R_readings[S_index] = tempR;
  111.       G_readings[S_index] = tempG;
  112.       B_readings[S_index] = tempB;
  113.     }
  114.  
  115.     R_total += R_readings[S_index];
  116.     G_total += G_readings[S_index];
  117.     B_total += B_readings[S_index];
  118.  
  119.     S_index++;
  120.     if (S_index >= numReadings) S_index = 0;
  121.  
  122.     R_average = R_total / numReadings;
  123.     G_average = G_total / numReadings;
  124.     B_average = B_total / numReadings;
  125.  
  126.     analogWrite(pinR, R_average);
  127.     analogWrite(pinG, G_average);
  128.     analogWrite(pinB, B_average);
  129.  
  130. #ifdef debug
  131.     Serial.print("R ");
  132.     Serial.print(R_average, DEC);
  133.     Serial.print(" G ");
  134.     Serial.print(G_average, DEC);
  135.     Serial.print(" B ");
  136.     Serial.println(B_average, DEC);
  137. #endif
  138.  
  139.     lastSmooth = millis();
  140.     smoothSteps--;
  141.   }
  142.  
  143.   Client client = server.available();
  144.   if (client)
  145.   {
  146.     // an http request ends with a blank line
  147.     boolean currentLineIsBlank = true;
  148.  
  149.     String inString = String(35); //Used to hold the string from the ethernet shield
  150.  
  151.     while (client.connected()) //We got a client online
  152.     {
  153.       if (client.available())
  154.       {
  155.         char c = client.read();        
  156.         inString += c;
  157.  
  158.         if (c == '\n' && currentLineIsBlank)
  159.         {
  160.           // send a standard http response header
  161.           client.println("HTTP/1.1 200 OK");
  162.           client.println("Content-Type: text/html");
  163.           client.println();
  164.  
  165.           break;
  166.         }
  167.  
  168.         if (c == '\n')
  169.         {
  170.           // you're starting a new line
  171.           currentLineIsBlank = true;
  172.         }
  173.         else if (c != '\r')
  174.         {
  175.           // you've gotten a character on the current line
  176.           currentLineIsBlank = false;
  177.         }
  178.       }
  179.     }
  180.  
  181.     if (inString.indexOf("favicon.ico") > 0) //Do nothing about the favicon.ico request
  182.     {
  183.       client.stop();
  184.       inString = "";
  185.       return;
  186.     }
  187.  
  188.     // give the web browser time to receive the data
  189.     delay(1);
  190.  
  191.     int startCommand = inString.indexOf('_');
  192.     int stopCommand = inString.indexOf('_', startCommand + 1);
  193.     int startR = startCommand;
  194.     int startG = inString.indexOf('-', startR + 1);
  195.     int startB = inString.indexOf('-', startG + 1);
  196.  
  197.     if (startCommand > 0 && stopCommand > 0) // If we got a valid command...
  198.     {
  199.       client.print("R > ");
  200.       client.println(inString.substring(startR+1, startG));
  201.       client.print("<br/>G > ");
  202.       client.println(inString.substring(startG+1, startB));
  203.       client.print("<br/>B > ");
  204.       client.println(inString.substring(startB+1, stopCommand));
  205.  
  206.       if (inString.indexOf("STICK") > 0) //Check if this is a value that will stick
  207.       {
  208.         client.print("<br><br>Sticky");
  209.  
  210.         stickR = string2Num(inString.substring(startR+1, startG));
  211.         stickG = string2Num(inString.substring(startG+1, startB));
  212.         stickB = string2Num(inString.substring(startB+1, stopCommand));
  213.         tempSet = 0;
  214.       }
  215.       else
  216.       {  
  217.         tempR = string2Num(inString.substring(startR+1, startG));
  218.         tempG = string2Num(inString.substring(startG+1, startB));
  219.         tempB = string2Num(inString.substring(startB+1, stopCommand));
  220.         tempSet = millis();
  221.         tempActive = true;
  222.       }
  223.      
  224.       //Set smoothSteps to the numbers of readings we use for fading
  225.       smoothSteps = numReadings;
  226.     }
  227.  
  228.     // close the connection:
  229.     client.stop();
  230.  
  231.     inString = "";
  232.   }
  233. }
  234.  
  235. int string2Num(String input)
  236. {
  237.   char tempNum[10]; //Char to hold the message before converting it to an integer
  238.   input.toCharArray(tempNum, 10);
  239.   return atoi(tempNum); // Convert char array to an integer
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement