Turn LEDs On/Off via Remote Control
I will show you how can you turn the LEDs on via the Colors button on your remote control.
Components and supplies
Resistor 221 ohm
Arduino UNO
LED (generic)
Wire Cable - By the Foot
Breadboard (generic)
IR receiver (generic)
Apps and platforms
Arduino IDE
Project description
Code
Code
arduino
copy this to Arduino and upload it.
1 /* 2 source: www.electroschematics.com 3 You'll need to change the led pins and the codes 4 accordingly to your configuration and IR remote 5 */ 6 7 #include <IRremote.h> 8 9 int RECV_PIN = 12; // the pin where you connect the output pin of TSOP4838 10 int led1 = 8; 11 int led2 = 9; 12 int led3 = 10; 13 int led4 = 11; 14 int itsONled[] = {0,0,0,0,0}; 15 /* the initial state of LEDs is OFF (zero) 16 the first zero must remain zero but you can 17 change the others to 1's if you want a certain 18 led to light when the board is powered */ 19 #define code1 33772 // code received from button A 20 #define code2 52972 // code received from button B 21 #define code3 3494 // code received from button C 22 #define code4 65160 // code received from button D 23 IRrecv irrecv(RECV_PIN); 24 25 decode_results results; 26 27 void setup() 28 { 29 Serial.begin(9600); // you can comment this line 30 irrecv.enableIRIn(); // Start the receiver 31 pinMode(led1, OUTPUT); 32 pinMode(led2, OUTPUT); 33 pinMode(led3, OUTPUT); 34 pinMode(led4, OUTPUT); 35 } 36 37 void loop() { 38 if (irrecv.decode(&results)) { 39 unsigned int value = results.value; 40 switch(value) { 41 case code1: 42 if(itsONled[1] == 1) { // if first led is on then 43 digitalWrite(led1, LOW); // turn it off when button is pressed 44 itsONled[1] = 0; // and set its state as off 45 } else { // else if first led is off 46 digitalWrite(led1, HIGH); // turn it on when the button is pressed 47 itsONled[1] = 1; // and set its state as on 48 } 49 break; 50 case code2: 51 if(itsONled[2] == 1) { 52 digitalWrite(led2, LOW); 53 itsONled[2] = 0; 54 } else { 55 digitalWrite(led2, HIGH); 56 itsONled[2] = 1; 57 } 58 break; 59 case code3: 60 if(itsONled[3] == 1) { 61 digitalWrite(led3, LOW); 62 itsONled[3] = 0; 63 } else { 64 digitalWrite(led3, HIGH); 65 itsONled[3] = 1; 66 } 67 break; 68 case code4: 69 if(itsONled[4] == 1) { 70 digitalWrite(led4, LOW); 71 itsONled[4] = 0; 72 } else { 73 digitalWrite(led4, HIGH); 74 itsONled[4] = 1; 75 } 76 break; 77 } 78 Serial.println(value); // you can comment this line 79 irrecv.resume(); // Receive the next value 80 } 81 }
Code
arduino
copy this to Arduino and upload it.
1 /* 2 source: www.electroschematics.com 3 You'll need to 4 change the led pins and the codes 5 accordingly to your configuration and 6 IR remote 7 */ 8 9 #include <IRremote.h> 10 11 int RECV_PIN 12 = 12; // the pin where you connect the output pin of TSOP4838 13 int led1 = 14 8; 15 int led2 = 9; 16 int led3 = 10; 17 int led4 = 11; 18 int itsONled[] 19 = {0,0,0,0,0}; 20 /* the initial state of LEDs is OFF (zero) 21 the first 22 zero must remain zero but you can 23 change the others to 1's if you want a 24 certain 25 led to light when the board is powered */ 26 #define code1 33772 27 // code received from button A 28 #define code2 52972 // code received from 29 button B 30 #define code3 3494 // code received from button C 31 #define 32 code4 65160 // code received from button D 33 IRrecv irrecv(RECV_PIN); 34 35 36 decode_results results; 37 38 void setup() 39 { 40 Serial.begin(9600); 41 // you can comment this line 42 irrecv.enableIRIn(); // Start the receiver 43 44 pinMode(led1, OUTPUT); 45 pinMode(led2, OUTPUT); 46 pinMode(led3, 47 OUTPUT); 48 pinMode(led4, OUTPUT); 49 } 50 51 void loop() { 52 53 if (irrecv.decode(&results)) { 54 unsigned int value = results.value; 55 56 switch(value) { 57 case code1: 58 if(itsONled[1] 59 == 1) { // if first led is on then 60 digitalWrite(led1, 61 LOW); // turn it off when button is pressed 62 itsONled[1] = 0; 63 // and set its state as off 64 } else { // 65 else if first led is off 66 digitalWrite(led1, HIGH); // turn it 67 on when the button is pressed 68 itsONled[1] = 1; // and 69 set its state as on 70 } 71 break; 72 case 73 code2: 74 if(itsONled[2] == 1) { 75 digitalWrite(led2, 76 LOW); 77 itsONled[2] = 0; 78 } else { 79 digitalWrite(led2, 80 HIGH); 81 itsONled[2] = 1; 82 } 83 break; 84 85 case code3: 86 if(itsONled[3] == 1) { 87 digitalWrite(led3, 88 LOW); 89 itsONled[3] = 0; 90 } else { 91 digitalWrite(led3, 92 HIGH); 93 itsONled[3] = 1; 94 } 95 break; 96 97 case code4: 98 if(itsONled[4] == 1) { 99 digitalWrite(led4, 100 LOW); 101 itsONled[4] = 0; 102 } else { 103 digitalWrite(led4, 104 HIGH); 105 itsONled[4] = 1; 106 } 107 break; 108 109 } 110 Serial.println(value); // you can comment this line 111 112 irrecv.resume(); // Receive the next value 113 } 114 }
Downloadable files
circuit diagrams
circuit diagrams
circuit diagrams
circuit diagrams
Comments