Skip to main content

IR Remote Control

You will use this existing program to collect the signal from your IR remote and make coloured lights show when you press the corresponding button on your remote control.

Find the Worksheet here: (PDF Document) IR Remote Control

The Worksheet requires you to start with a basic template program. Click and drag to select the program below, then copy and paste it into the Arduino Software.

Tutorial program

/*
* IR receiver program
* Connect LEDs to pin 13, 12, 11
* Connect the receiver input to digital pin 5
* Use Controller code '0270'
* 06_07_2014 Stephen Pithouse
*/

long pattern[20];
long patternStart = 0;
long time = 0;
int a = 0;
int redTog = 0;
int yellowTog = 0;
int greenTog = 0;
long red[] = {};
long yellow[] = {};
long green[] = {};
void setup() {
pinMode(5, INPUT); //IR receiver input
pinMode(11, OUTPUT); //LED
pinMode(12, OUTPUT); //LED
pinMode(13, OUTPUT); //LED
Serial.begin(115200);
}
void loop() {
getPattern();
compare();
delay(250);
}
void getPattern() {
while (digitalRead(5) == HIGH) { //wait for the pattern to start
}
time = millis(); //Reset loop varibles
patternStart = micros(); // "
a = 0; // "
while (millis() - time < 200) { //record first 200mS
if (digitalRead(5) == LOW) { //Trough in signal
while (digitalRead(5) == LOW) {
pattern[a] = micros() - patternStart; //Record time until trough ends
}
a++;
}
if (digitalRead(5) == HIGH) { //Peak in signal
while (digitalRead(5) == HIGH) {
pattern[a] = micros() - patternStart; //Record time until peak ends
}
a++;
}
}
debug();
}
void debug() { //Prints the pulse code in two forms
// for (int b = 0; b < 20; ++b) { //Display first 20 times
// Serial.println(pattern[b]); //Column Form
// }
for (int b = 0; b < 19; ++b) { //Display first 20 times
Serial.print(pattern[b]); //Single line form - suitable to be copied into program
Serial.print(", ");
}
Serial.println(pattern[19]);
}
void compare() {
int redDif = 0;
int yellowDif = 0;
int greenDif = 0;
for (int c = 10; c < 19; c++) {
redDif += abs(pattern[c] - red[c])/100;
yellowDif += abs(pattern[c] - yellow[c])/100;
greenDif += abs(pattern[c] - green[c])/100;
}
if (redDif < yellowDif && redDif < greenDif) {
Serial.println("RED!");
redToggle(11);
}
else if (yellowDif < redDif && yellowDif < greenDif) {
Serial.println("YELLOW!");
yellowToggle(12);
}
else if (greenDif < yellowDif && greenDif < redDif) {
Serial.println("GREEN!");
greenToggle(13);
}
else
{
Serial.println("No match!");
}
//Serial.println(redDif);
//Serial.println(yellowDif);
//Serial.println(greenDif);
}
void redToggle(int colour) {
if (redTog == 1) { //Currently on .: turn off
digitalWrite(colour, LOW); //Turn off
}
else { //Currently off .: turn on
digitalWrite(colour, HIGH);
}
redTog = abs(redTog - 1);
}
void yellowToggle(int colour) {
if (yellowTog == 1) { //Currently on .: turn off
digitalWrite(colour, LOW); //Turn off
}
else { //Currently off .: turn on
digitalWrite(colour, HIGH);
}
yellowTog = abs(yellowTog - 1);
}
void greenToggle(int colour) {
if (greenTog == 1) { //Currently on .: turn off
digitalWrite(colour, LOW); //Turn off
}
else { //Currently off .: turn on
digitalWrite(colour, HIGH);
}
greenTog = abs(greenTog - 1);
}