RuiViana

Fade_LED

Jul 18th, 2016
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#include <IRremote.h>
  2. //int RECV_PIN = 4;
  3. //IRrecv irrecv(RECV_PIN);
  4. //decode_results results;
  5. //rgb led
  6. int redPin = 12;
  7. int greenPin = 8;
  8. int bluePin = 7;
  9. //motor pot
  10. int const potenciometro = 0;
  11. int const transTIP120 = 10;
  12. int valPotenciometro = 0;
  13. //leds
  14.  
  15. int led = 11;
  16. int brightness = 0;
  17. int fadeAmount = 5;
  18. //------------------------------
  19. void setup()
  20. {
  21.   Serial.begin(9600);
  22.   //pot motor
  23.   pinMode(transTIP120, OUTPUT);
  24.  
  25. //  irrecv.enableIRIn();
  26.   //leds
  27.   pinMode(led, OUTPUT);
  28.   //pinMode(11, OUTPUT);
  29.   //rgb
  30.   pinMode(redPin, OUTPUT);
  31.   pinMode(greenPin, OUTPUT);
  32.   pinMode(bluePin, OUTPUT);
  33.   //RELAY
  34.   pinMode(2, OUTPUT);
  35.   pinMode(3, OUTPUT);
  36.   pinMode(5, OUTPUT);
  37.   pinMode(6, OUTPUT);
  38.  
  39. }
  40. int i1 = 0;
  41. int i2 = 0;
  42. int i3 = 0;
  43. int i4 = 0;
  44. int i5 = 0;
  45. int i6 = 0;
  46. int i7 = 0;
  47. int i8 = 0;
  48. int i9 = 0;
  49. //-------------------------
  50. void loop()
  51. {
  52.   fade();
  53. /*  if (irrecv.decode(&results))
  54.   {
  55.     //button 1
  56.     if (results.value == 0xFF6897)  // Red Button
  57.       {
  58.       setColor(255, 0, 0);  // Sets Red Color to the RGB LED
  59.       delay(100);
  60.     }
  61.     //button 2
  62.     if (results.value == 0xFF9867)  // Yellow Button
  63.      {
  64.       setColor(255, 255, 0);  // Yellow Color
  65.       delay(100);
  66.     }
  67.     //button OK
  68.     if (results.value == 0xFF02FD)  // Stop Button
  69.     {
  70.       setColor(0, 0, 0);  // OFF
  71.       delay(100);
  72.     }
  73.     //button 3
  74.     if (i1 == 0 && results.value == 0xFFB04F)
  75.     {
  76.       digitalWrite(6, HIGH);
  77.       i1 = 1;
  78.     }
  79.     else if (i1 == 1 && results.value == 0xFFB04F)
  80.     {
  81.       digitalWrite(6, LOW);
  82.       i1 = 0;
  83.     }
  84.     //button 4
  85.     else if (i2 == 0 && results.value == 0xFF30CF)
  86.     {
  87.       digitalWrite(5, HIGH);
  88.       i2 = 1;
  89.     }
  90.     else if (i2 == 1 && results.value == 0xFF30CF)
  91.     {
  92.       digitalWrite(5, LOW);
  93.       i2 = 0;
  94.     }
  95.     //button 5
  96.     else if (i3 == 0 && results.value == 0xFF18E7)
  97.     {
  98.       digitalWrite(3, HIGH);
  99.       i3 = 1;
  100.     }
  101.     else if (i3 == 1 && results.value == 0xFF18E7)
  102.     {
  103.       digitalWrite(3, LOW);
  104.       i3 = 0;
  105.     }
  106.     //button 6
  107.     else if (i4 == 0 && results.value == 0xFF7A85)
  108.     {
  109.       digitalWrite(2, HIGH);
  110.       i4 = 1;
  111.     }
  112.     else if (i4 == 1 && results.value == 0xFF7A85)
  113.     {
  114.       digitalWrite(2, LOW);
  115.       i4 = 0;
  116.     }
  117.     //button 7
  118.     else if (i5 == 0 && results.value == 0xFF10EF)
  119.     {
  120.       digitalWrite(10, HIGH);
  121.       i5 = 1;
  122.     }
  123.     else if (i5 == 1 && results.value == 0xFF10EF)
  124.     {
  125.       digitalWrite(10, LOW);
  126.       i5 = 0;
  127.     }
  128.     //button 8
  129.     else if (i6 == 0 && results.value == 0xFF38C7)
  130.     {
  131.       digitalWrite(9, HIGH);
  132.       i6 = 1;
  133.       fade();
  134.     }
  135.     else if (i6 == 1 && results.value == 0xFF38C7)
  136.     {
  137.       digitalWrite(9, LOW);
  138.       i6 = 0;
  139.     }
  140.     irrecv.resume();
  141.   }
  142.   delay(100);*/
  143. }
  144. //-------------------------------------
  145. //rgb led
  146. void setColor(int red, int green, int blue)
  147. {
  148.   analogWrite(redPin, red);
  149.   analogWrite(greenPin, green);
  150.   analogWrite(bluePin, blue);
  151.  
  152.   //potmotor
  153.   valPotenciometro = analogRead(potenciometro) / 4;
  154.   analogWrite(transTIP120, valPotenciometro);
  155. }
  156. //-------------------------
  157. void fade()
  158. {
  159.  
  160.   //fade led
  161.   Serial.println(brightness);
  162.   analogWrite(led, brightness);
  163.   brightness = brightness + fadeAmount;
  164.  
  165.   if (brightness == 0 || brightness == 255)
  166.   {
  167.     fadeAmount = -fadeAmount ;
  168.   }
  169.   delay(30);
  170. }
RAW Paste Data