WojciechMilczarek

IR controlled Arduino LED chain malfunction

Dec 11th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. #include "IRremote.h"
  2. //assigning ir receiver to digital pin 2
  3. int receiver = 2;
  4.  
  5. IRrecv irrecv(receiver); //creating an instance of irrecv
  6. decode_results results; //create instance of decode results
  7.  
  8. //LEDs initialization
  9. int lampka[] = {3, 5, 6, 9, 10, 11, 13};
  10.  
  11. //ir sensor initialization
  12. int ir_sens = 2;
  13.  
  14. //photoresistor initialization at analog pin 0
  15. int photo_res = 0;
  16.  
  17. int val = 255; //brightenss of the leds
  18.  
  19. int program = 1; //which program should be executed now?
  20. int state = 1; //the chain should be turned on or turned off?
  21. int mode = 1; //we input modes or brightness with the remote?
  22.  
  23. //function defining buttons of the remote
  24. void translateIR()
  25. {
  26.    
  27.  switch(results.value)
  28.    {
  29.     case 0xFF42BD: //* button
  30.       mode = 1;
  31.       break;
  32.      
  33.     case 0xFF52AD: //# button
  34.       mode = 2;
  35.       break;
  36.    
  37.      case 0xFF02FD: //ok button
  38.        if (state == 1)
  39.          state = 0;
  40.        else
  41.          state = 1;
  42.        break;
  43.     case 0xFF6897: //button 1
  44.       if (mode == 1)
  45.       program = 1;
  46.       if (mode == 2)
  47.       val = 25;
  48.       break;
  49.      
  50.     case 0xFF9867: //button2
  51.       if (mode == 1)
  52.       program = 2;
  53.       if (mode == 2)
  54.       val = 50;
  55.       break;
  56.    
  57.     case 0xFFB04F: //button3
  58.       program = 3;
  59.       break;
  60.    }
  61.  
  62. }
  63.  
  64.  
  65. //pierwszy program lancucha
  66. //first program of the chain
  67. void pierwszy()
  68. {
  69.  for(int wl_wyl = 0; wl_wyl < 7; wl_wyl++)
  70.  {
  71.   analogWrite(lampka[wl_wyl], val);
  72.   delay(500);
  73.   digitalWrite(lampka[wl_wyl], LOW);
  74.  }
  75. }
  76.  
  77. //drugi program programu
  78. //second program of the chain
  79. void staly()
  80. {
  81.  for(int wlacznik = 0; wlacznik < 7; wlacznik++)
  82.  {
  83.   analogWrite(lampka[wlacznik], val);
  84.  }
  85.  
  86.  delay(3000);
  87.  
  88.  for(int wylacznik = 0; wylacznik < 7; wylacznik++)
  89.  {
  90.   digitalWrite(lampka[wylacznik], LOW);
  91.  }
  92. }
  93.  
  94.  
  95. //trzeci program lancucha
  96. //third program of the chain
  97. void srodek()
  98. {
  99.  analogWrite(lampka[3], val);
  100.  delay(500);
  101.  analogWrite(lampka[0], val);
  102.  delay(500);
  103.  analogWrite(lampka[5], val);
  104.  delay(500);
  105.  analogWrite(lampka[1], val);
  106.  delay(500);
  107.  analogWrite(lampka[4], val);
  108.  delay(500);
  109.  analogWrite(lampka[2], val);
  110.  delay(3000);
  111.  
  112.  for(int wylacznik = 0; wylacznik <7; wylacznik++)
  113.  {
  114.    digitalWrite(lampka[wylacznik], LOW);
  115.    delay(100);
  116.  }
  117.  
  118. }
  119.  
  120. void setup(){
  121.  
  122.   Serial.begin(9600);
  123.  
  124.   irrecv.enableIRIn(); //initialization of ir receiver
  125.  
  126.   //ustawienie wszystkich lampek jako piny wyjscia
  127.   //setting all LED as output pins
  128.   for (int ust_lamp = 0; ust_lamp < 7; ust_lamp++)
  129.   {
  130.     pinMode(lampka[ust_lamp],OUTPUT);
  131.   }
  132. }
  133.  
  134. void loop()
  135. {
  136.   Serial.print("Aktualny tryb to: ");
  137.   Serial.println(mode);
  138.   Serial.print("Aktualna jasność to: ");
  139.   Serial.println(val);
  140.   //właczenie lampek gdy jest ciemno
  141.   //turning on LEDs when its dark
  142.   if ((analogRead(photo_res) < 500)&& (state == 1))
  143.   {
  144.     //wykonujemy aktualnie wybrany program
  145.     //we execute program which is currently set
  146.     switch(program)
  147.     {
  148.       case 1:
  149.         pierwszy();
  150.         break;
  151.        
  152.       case 2:
  153.         staly();
  154.         break;
  155.        
  156.       case 3:
  157.         srodek();
  158.         break;
  159.     }
  160.   }
  161.  
  162.   else //wylaczamy lampki gdy jest zbyt jasno; if it's too bright we turn off the lights
  163.   {
  164.     for(int wyl_ciemnosc = 0; wyl_ciemnosc <7; wyl_ciemnosc++)
  165.     {
  166.      digitalWrite(lampka[wyl_ciemnosc], LOW);
  167.     }
  168.   }
  169.   delay(1000);
  170.  
  171.   if (irrecv.decode(&results))
  172.   {
  173.    
  174.    translateIR();
  175.    irrecv.resume();
  176.    
  177.   }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment