Advertisement
candale

RGB Lamp

Jun 18th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.46 KB | None | 0 0
  1. /*
  2. RGB Led Lamp 1.0a
  3. by Luca Soltoggio
  4. 15/03/2012
  5.  
  6. http://arduinoelettronica.wordpress.com/
  7.  
  8. */
  9.  
  10. int rpin = 3;   // pin RED
  11. int gpin = 5;   // pin GREEN
  12. int bpin = 6;   // pin BLU
  13. unsigned int r=0, g=0, b=0,bm=0;   // valori rgb e coefficiente divisore del blu
  14. unsigned int valh=0, vals=0, valv=0;   // hue, saturation, value
  15. unsigned int sensorValue;  // variabile che memorizza il valore dei potenziometri
  16.  
  17. const int analogInPin = A0;  // pin dei potenziometri
  18. const int analogInPin2 = A1;
  19. const int digitalInPin = 10;  // pin del deviatore
  20. const int digitalInPin2= 11;
  21.  
  22. int red[]={255,255,135};  // array con i valori RGB della funzione "Bianco"
  23. int green[]={157,255,158};
  24. int blue[]={51,255,255};
  25.  
  26. boolean DIG1, DIG2;
  27.  
  28. long previousMillis = 0;
  29.  
  30. int i=0,j=0;
  31. int dl=0;  // delay
  32.  
  33. void setup()
  34. {
  35.   pinMode(digitalInPin,INPUT);
  36.   pinMode(digitalInPin2,INPUT);
  37. }
  38.  
  39. void loop()
  40. {
  41.   DIG1=digitalRead(digitalInPin);
  42.   DIG2=digitalRead(digitalInPin2);
  43.  
  44.   if (DIG1) {
  45.     bm=1.1;
  46.     HSV_Game();
  47.   }
  48.   else if (DIG2) {
  49.     bm=1.9;
  50.     RAINBOW_Game();
  51.   }
  52.   else {
  53.     bm=2.4;
  54.     LIGHT_Game();
  55.   }
  56.  
  57.   analogWrite(rpin, r/1.09);  // ROSSO
  58.   analogWrite(gpin, g/1);  // VERDE
  59.   analogWrite(bpin, b/bm);  // BLUE
  60. }
  61.  
  62. // funzione di armonizzazione dei valori analogici tramite media aritmetica
  63. int SensorSmooth (int pin) {
  64.   sensorValue=0;
  65.   for (int i = 0; i<10; i++) {     sensorValue+= analogRead(pin);   }   return int ((float)sensorValue/10+0.5); } // prima funzione: selezione del colore e della luminosità tramite potenziometri void HSV_Game() {   valh = SensorSmooth(analogInPin);   vals = 255;   valv = SensorSmooth(analogInPin2);   valh = map(valh,0,1023,0,359);   valv = map(valv,0,1023,32,255);   hsv2rgb(valh,vals,valv,r,g,b); } // seconda funzione: selezione di bianco caldo, bianco neutro e bianco freddo, oltre alla luminosità void LIGHT_Game() {   valv = SensorSmooth(analogInPin2);   valv = map(valv,0,1023,16,255);   valh = SensorSmooth(analogInPin);   if (valh=742) valh=2;   if (valh>2) valh=1;
  66.   r=red[valh]*valv/255;
  67.   g=green[valh]*valv/255;
  68.   b=blue[valh]*valv/255;
  69. }
  70.  
  71. // terza funzione: selezione della velocità di rotazione tra tutti i colori e della luminosità
  72. void RAINBOW_Game() {
  73.   dl = SensorSmooth(analogInPin); // tempo di pausa
  74.   dl = map(dl,0,1023,1,100);
  75.   valv = SensorSmooth(analogInPin2);
  76.   valv = map(valv,0,1023,64,255);
  77.   unsigned long currentMillis = millis();
  78.   switch (j) {
  79.     case 0:
  80.       r=255*valv/255;
  81.       g=i*valv/255;
  82.       b=0*valv/255;
  83.       break;
  84.     case 1:
  85.       r=(255-i)*valv/255;
  86.       g=255*valv/255;
  87.       b=0*valv/255;
  88.       break;
  89.     case 2:
  90.       r=0*valv/255;
  91.       g=255*valv/255;
  92.       b=i*valv/255;
  93.       break;
  94.     case 3:
  95.       r=0*valv/255;
  96.       g=(255-i)*valv/255;
  97.       b=255*valv/255;
  98.       break;
  99.     case 4:
  100.       r=i*valv/255;
  101.       g=0*valv/255;
  102.       b=255*valv/255;
  103.       break;
  104.     case 5:
  105.       r=255*valv/255;
  106.       g=0*valv/255;
  107.       b=(255-i)*valv/255;
  108.   }
  109.   if (currentMillis-previousMillis > (long)(100-dl)) {  // usa millis invece che delay
  110.     previousMillis=currentMillis;
  111.     i=i+1;
  112.     if (i==256) {
  113.       i=1;
  114.       j=j+1;
  115.       if (j==6) j=0;
  116.     }
  117.   }
  118. }
  119.  
  120. /* Funzione che trasforma HSV in RGB
  121. (c) Elco Jacobs, E-atelier Industrial Design TU/e, July 2011
  122.  
  123. http://code.google.com/p/shiftpwm/source/browse/trunk/examples/ShiftPWM_Example1/hsv2rgb.cpp?r=3
  124.  
  125. */
  126. void hsv2rgb(int hue, int sat, int val, unsigned int& r, unsigned int& g, unsigned int& b)
  127. {
  128.     int H_accent = hue/60;
  129.     int bottom = ((255 - sat) * val)>>8;
  130.     int top = val;
  131.     int rising  = ((top-bottom)  *(hue%60   )  )  /  60  +  bottom;
  132.     int falling = ((top-bottom)  *(60-hue%60)  )  /  60  +  bottom;
  133.  
  134.     switch(H_accent) {
  135.         case 0:
  136.                 r = top;
  137.                 g = rising;
  138.                 b = bottom;
  139.         break;
  140.  
  141.         case 1:
  142.                 r = falling;
  143.                 g = top;
  144.                 b = bottom;
  145.         break;
  146.  
  147.         case 2:
  148.                 r = bottom;
  149.                 g = top;
  150.                 b = rising;
  151.         break;
  152.  
  153.         case 3:
  154.                 r = bottom;
  155.                 g = falling;
  156.                 b = top;
  157.         break;
  158.  
  159.         case 4:
  160.                 r = rising;
  161.                 g = bottom;
  162.                 b = top;
  163.         break;
  164.  
  165.         case 5:
  166.                 r = top;
  167.                 g = bottom;
  168.                 b = falling;
  169.         break;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement