Advertisement
Guest User

davideallume

a guest
Jan 11th, 2010
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.14 KB | None | 0 0
  1. /*
  2. StateLamp by Fabio Carnino
  3. code inspired by arkadian.eu
  4. http://www.arkadian.eu/pages/219/arduino-controlled-ikea-lamp
  5. Grono lamp changing behaviour by changing its face
  6.  
  7. Hackea Workshop held by Tinker in Torino for Torino Design Week
  8. http://tinker.it/
  9. http://www.torinodesignweek.org/
  10.  
  11. */
  12.  
  13. //tilts
  14. int buttonPin1 = 3;    
  15. int buttonPin2 = 2;
  16. int buttonPin3 = 4;
  17.  
  18. //value to store tilts
  19. int buttonPin1Val = 0;
  20. int buttonPin2Val = 0;
  21. int buttonPin3Val = 0;
  22.  
  23. int fadeval = 0;
  24. int fadedIn = 0;
  25. int fadedOut = 0;
  26.  
  27. int ledPin4 = 11; // (Yellow)
  28.  
  29. int ledRGB[] = {9,10,11}; // rgb led
  30. int states[13];
  31. long debounces[13];
  32.  
  33. long debounceDelay = 50;
  34.  
  35. unsigned long lastAction = millis();
  36.  
  37. //Defined Colors (different RGB (red, green, blue) values for colors
  38. const byte RED[] =     {255, 0, 0};
  39. const byte ORANGE[] =  {83, 4, 0};
  40. const byte YELLOW[] =  {255, 255, 0};
  41. const byte GREEN[] =   {0, 255, 0};
  42. const byte BLUE[] =    {0, 0, 255};
  43. const byte WHITE[] =   {255, 255, 255};
  44. const byte BLACK[] =   {0, 0, 0};
  45. const byte PINK[] =    {158, 4, 79};
  46. const byte B2[] =      {0, 128, 255};
  47. const byte G2[] =      {128, 255, 0};
  48. const byte R2[] =      {255, 128, 0};
  49. const byte B3[] =      {128, 0, 255};
  50. const byte G3[] =      {0, 255, 128};
  51. const byte R3[] =      {229, 215, 180};
  52.  
  53. byte coloreAttuale[2];
  54.  
  55. void setup() {
  56.   pinMode(buttonPin1, INPUT);
  57.   pinMode(buttonPin2, INPUT);
  58.   pinMode(buttonPin3, INPUT);
  59.  
  60.   for(int i = 0; i < 3; i++)
  61.   {
  62.     pinMode(ledRGB[i],OUTPUT);
  63.   }
  64.  
  65.   pinMode(ledPin4, OUTPUT);
  66.   Serial.begin(9600);
  67.   digitalWrite(buttonPin1, LOW); // pull-down
  68.   digitalWrite(buttonPin2, LOW);
  69.   digitalWrite(buttonPin3, LOW);
  70.  
  71.   debounces[buttonPin1] = 0;
  72.   debounces[buttonPin2] = 0;
  73.   debounces[buttonPin3] = 0;
  74.  
  75.   states[buttonPin1] = 0;
  76.   states[buttonPin2] = 0;
  77.   states[buttonPin3] = 0;
  78. }
  79.  
  80. void loop() {
  81.   readState(buttonPin1);
  82.   readState(buttonPin2);
  83.   readState(buttonPin3);
  84.  
  85.   buttonPin1Val = states[buttonPin1];
  86.   buttonPin2Val = states[buttonPin2];
  87.   buttonPin3Val = states[buttonPin3];
  88.  
  89.  /* uncomment for debugging
  90.   Serial.print("buttonPin1Val = ");
  91.   Serial.print(buttonPin1Val);
  92.   Serial.print("___");
  93.  
  94.   Serial.print("buttonPin2Val = ");
  95.   Serial.print(buttonPin2Val);
  96.   Serial.print("___");
  97.  
  98.   Serial.print("buttonPin3Val = ");
  99.   Serial.println(buttonPin3Val);
  100. */  
  101. // quattro possibilità  / four states
  102.   if (buttonPin2Val == 0 && buttonPin3Val ==0) { // extra
  103.      fadeToColor(ledRGB, coloreAttuale, GREEN,10);
  104.       color(GREEN);
  105.      }
  106.   if (buttonPin2Val == 1 && buttonPin3Val ==0) { // fade
  107.     fadeToColor(ledRGB, coloreAttuale, WHITE,10);
  108.      }
  109.   if (buttonPin2Val == 0 && buttonPin3Val ==1) { // off
  110. //  digitalWrite(ledRGB,LOW);
  111.    digitalWrite(9, LOW);
  112.    digitalWrite(10, LOW);
  113.    digitalWrite(11, LOW);
  114.       }
  115.   if (buttonPin2Val == 1 && buttonPin3Val ==1) { // fade
  116.     fadeToColor(ledRGB, coloreAttuale, PINK, 1);
  117.   }  
  118. }
  119.  /* else if (buttonPin2Val == 1) {
  120.     if (fadedOut == 0) {
  121.       fadeout();
  122.     }
  123.   }
  124.  
  125.   */
  126. //}
  127.  
  128. void fadeToColor(int* led, byte* startColor, const byte* endColor, int fadeSpeed){
  129.   int changeRed = endColor[0] - startColor[0];                            //the difference in the two colors for the red channel
  130.   int changeGreen = endColor[1] - startColor[1];                          //the difference in the two colors for the green channel
  131.   int changeBlue = endColor[2] - startColor[2];                           //the difference in the two colors for the blue channel
  132.   int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); //make the number of change steps the maximum channel change
  133.  
  134.     for(int i = 0 ; i < steps; i++){                                       //iterate for the channel with the maximum change
  135.     byte newRed = startColor[0] + (i * changeRed / steps);                 //the newRed intensity dependant on the start intensity and the change determined above
  136.     byte newGreen = startColor[1] + (i * changeGreen / steps);             //the newGreen intensity
  137.     byte newBlue = startColor[2] + (i * changeBlue / steps);               //the newBlue intensity
  138.     byte newColor[] = {newRed, newGreen, newBlue};                         //Define an RGB color array for the new color
  139.     color(newColor);    //Set the LED to the calculated value
  140.     //readButton();  
  141.     //if(myOption==0){delay(fadeSpeed);}  
  142.     //delay(fadeSpeed);
  143.   }
  144.   color(endColor);                 //The LED should be at the endColor but set to endColor to avoid rounding errors
  145. }
  146.  
  147. void readState(int tiltPin) {
  148.   int reading = digitalRead(tiltPin);
  149.   //Serial.println(reading);
  150.   //Serial.println(reading);
  151.   if (reading != states[tiltPin]) {
  152.     if ((millis() - debounces[tiltPin]) > debounceDelay) {
  153.       // whatever the reading is at, it's been there for longer
  154.       // than the debounce delay, so take it as the actual current state:
  155.       states[tiltPin] = reading;
  156.       debounces[tiltPin] = millis(); // reset the debouncing timer
  157.     }
  158.   }
  159. }
  160.  
  161. void color(const byte rgb[]){
  162.   for(int i = 0; i < 3; i++)
  163.   {
  164.     analogWrite(ledRGB[i],rgb[i]);
  165.     coloreAttuale[i] = rgb[i];
  166.   }
  167. }
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement