Last active
August 29, 2015 14:18
-
-
Save ryanamaral/34726743c4adbf0002d5 to your computer and use it in GitHub Desktop.
[Arduino] Control LEDs with IR Remote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Control LEDs ON/OFF with IR Remote and Arduino | |
//source: http://www.electroschematics.com/9522/arduino-control-leds-ir-remote/ | |
/* | |
source: www.electroschematics.com | |
You'll need to change the led pins and the codes | |
accordingly to your configuration and IR remote | |
*/ | |
#include <IRremote.h> | |
int RECV_PIN = 3; // the pin where you connect the output pin of TSOP4838 | |
int led1 = 2; | |
int led2 = 4; | |
int led3 = 7; | |
int itsONled[] = {0,0,0,0}; | |
/* the initial state of LEDs is OFF (zero) | |
the first zero must remain zero but you can | |
change the others to 1's if you want a certain | |
led to light when the board is powered */ | |
#define code1 63495 // code received from button A | |
#define code2 30855 // code received from button B | |
#define code3 22695 // code received from button C | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
void setup() | |
{ | |
Serial.begin(9600); // you can comment this line | |
irrecv.enableIRIn(); // Start the receiver | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
pinMode(led3, OUTPUT); | |
} | |
void loop() { | |
if (irrecv.decode(&results)) { | |
unsigned int value = results.value; | |
switch(value) { | |
case code1: | |
if(itsONled[1] == 1) { // if first led is on then | |
digitalWrite(led1, LOW); // turn it off when button is pressed | |
itsONled[1] = 0; // and set its state as off | |
} else { // else if first led is off | |
digitalWrite(led1, HIGH); // turn it on when the button is pressed | |
itsONled[1] = 1; // and set its state as on | |
} | |
break; | |
case code2: | |
if(itsONled[2] == 1) { | |
digitalWrite(led2, LOW); | |
itsONled[2] = 0; | |
} else { | |
digitalWrite(led2, HIGH); | |
itsONled[2] = 1; | |
} | |
break; | |
case code3: | |
if(itsONled[3] == 1) { | |
digitalWrite(led3, LOW); | |
itsONled[3] = 0; | |
} else { | |
digitalWrite(led3, HIGH); | |
itsONled[3] = 1; | |
} | |
break; | |
} | |
Serial.println(value); // you can comment this line | |
irrecv.resume(); // Receive the next value | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Control RBG LED with IR Remote (HX1838) | |
//source: http://nicuflorica.blogspot.ro/2014/01/telecomanda-infrarosu-hx1838.html | |
/* adapted sketch by niq_ro from http://www.tehnic.go.ro | |
and http://nicuflorica.blogspot.com/ | |
/* YourDuino.com Example Software Sketch | |
IR Remote Kit Test | |
Uses YourDuino.com IR Infrared Remote Control Kit 2 | |
http://arduino-direct.com/sunshop/index.php?l=product_detail&p=153 | |
based on code by Ken Shirriff - http://arcfn.com | |
Get Library at: https://github.com/shirriff/Arduino-IRremote | |
Unzip folder into Libraries. RENAME folder IRremote | |
terry@yourduino.com */ | |
/*-----( Import needed libraries )-----*/ | |
#include "IRremote.h" | |
/*-----( Declare Constants )-----*/ | |
int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11 | |
/*-----( Declare objects )-----*/ | |
IRrecv irrecv(receiver); // create instance of 'irrecv' | |
decode_results results; // create instance of 'decode_results' | |
/*-----( Declare Variables )-----*/ | |
// other | |
int ledr = 6; //pin for red LED | |
int leda = 5; //pin for blue LED | |
int ledv = 9; //pin for green LED | |
void setup() /*----( SETUP: RUNS ONCE )----*/ | |
{ | |
Serial.begin(9600); | |
Serial.println("IR Receiver Raw Data + Button Decode Test"); | |
Serial.println("0 = all off"); | |
Serial.println("1 = blue LED is 100%"); | |
Serial.println("2 = green LED is 100%"); | |
Serial.println("3 = red LED is 100%"); | |
Serial.println("4 = blue LED is 50%"); | |
Serial.println("5 = green LED is 50%"); | |
Serial.println("6 = red LED is 50%"); | |
Serial.println("7 = blue LED is off"); | |
Serial.println("8 = green LED is off"); | |
Serial.println("9 = red LED is off"); | |
Serial.println("---------------------------------------------"); | |
irrecv.enableIRIn(); // Start the receiver | |
// initial state for LEds | |
analogWrite(ledr, 0); | |
analogWrite(leda, 0); | |
analogWrite(ledv, 0); | |
}/*--(end setup )---*/ | |
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/ | |
{ | |
if (irrecv.decode(&results)) // have we received an IR signal? | |
{ | |
// Serial.println(results.value, HEX); UN Comment to see raw values | |
translateIR(); | |
irrecv.resume(); // receive the next value | |
} | |
}/* --(end main loop )-- */ | |
/*-----( Declare User-written Functions )-----*/ | |
void translateIR() // takes action based on IR code received | |
// describing Car MP3 IR codes | |
{ | |
switch(results.value) | |
{ | |
case 0xFFA25D: | |
Serial.println(" CH- "); | |
break; | |
case 0xFF629D: | |
Serial.println(" CH "); | |
break; | |
case 0xFFE21D: | |
Serial.println(" CH+ "); | |
break; | |
case 0xFF22DD: | |
Serial.println(" PREV "); | |
break; | |
case 0xFF02FD: | |
Serial.println(" NEXT "); | |
break; | |
case 0xFFC23D: | |
Serial.println(" PLAY/PAUSE "); | |
break; | |
case 0xFFE01F: | |
Serial.println(" VOL- "); | |
break; | |
case 0xFFA857: | |
Serial.println(" VOL+ "); | |
break; | |
case 0xFF906F: | |
Serial.println(" EQ "); | |
break; | |
case 0xFF6897: | |
{ | |
Serial.println(" 0 "); | |
analogWrite(ledr, 0); | |
analogWrite(leda, 0); | |
analogWrite(ledv, 0); | |
} | |
break; | |
case 0xFF9867: | |
Serial.println(" 100+ "); | |
break; | |
case 0xFFB04F: | |
Serial.println(" 200+ "); | |
break; | |
case 0xFF30CF: | |
{ | |
Serial.println(" 1 "); | |
analogWrite(leda, 255); | |
} | |
break; | |
case 0xFF18E7: | |
{ | |
Serial.println(" 2 "); | |
analogWrite(ledv, 255); | |
} | |
break; | |
case 0xFF7A85: | |
{ | |
Serial.println(" 3 "); | |
analogWrite(ledr, 255); | |
} | |
break; | |
case 0xFF10EF: | |
{ | |
Serial.println(" 4 "); | |
analogWrite(leda, 122); | |
} | |
break; | |
case 0xFF38C7: | |
{ | |
Serial.println(" 5 "); | |
analogWrite(ledv, 122); | |
} | |
break; | |
case 0xFF5AA5: | |
{ | |
Serial.println(" 6 "); | |
analogWrite(ledr, 122); | |
} | |
break; | |
case 0xFF42BD: | |
{ | |
Serial.println(" 7 "); | |
analogWrite(leda, 0); | |
} | |
break; | |
case 0xFF4AB5: | |
{ | |
Serial.println(" 8 "); | |
analogWrite(ledv, 0); | |
} | |
break; | |
case 0xFF52AD: | |
{ | |
Serial.println(" 9 "); | |
analogWrite(ledr, 0); | |
} | |
break; | |
default: | |
Serial.println(" other button "); | |
} | |
delay(500); | |
} //END translateIR | |
/* ( THE END ) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment