Introduction: IR-Remote Color Guessing Game

In this instructable, I will show you how to control an RGB led using an IR-remote, and make a fun game out of it!

Step 1: Parts Needed

The parts you will need for this project are:

- arduino one
- RGB Led
- IR-Sensor

(led count depends on the game, the amount of lives or )
- 2 green leds
- 1 yellow led
- 1 orange led
- 3 red leds

Step 2: Code

#include <IRremote.h>

int redPin = 10; int greenPin = 6; int bluePin = 5;

int levensPin1 = 8; int levensPin2 = 9; int levensPin3 = 12;

int levelPin1 = 2; int levelPin2 = 3; int levelPin3 = 4; int levelPin4 = 7;

int RECV_PIN = 11; IRrecv irrecv(RECV_PIN); decode_results results;

// input colors int input[] = {-1,-1,-1}; int color[] = {-1,-1,-1};

int indexNumber = 0; int indexColor = 0;

int waarde = 0;

int rood = 0; int groen = 0; int blauw = 0;

// generate colors int roodRandom = 0; int groenRandom = 0; int blauwRandom = 0;

// game variables int diff = 200;

int levelNumber = 1;

int level[] = {-1,-1,-1,-1};

int t = 0;

int level1 = 255; int level2 = 0; int level3 = 0; int level4 = 0;

int lives = 3; int levens1 = 255; int levens2 = 255; int levens3 = 255;

int roodGok = 0; int groenGok = 0; int blauwGok = 0;

void setup() { Serial.begin(9600); irrecv.enableIRIn(); // start the receiver pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT);

pinMode(levensPin1, OUTPUT); pinMode(levensPin2, OUTPUT); pinMode(levensPin3, OUTPUT);

}

void loop() { if (irrecv.decode(&results)) // receive the signals from the remote { Serial.println(results.value, DEC); // display the signals recieved from each pressed button irrecv.resume(); // receive next signal switch(results.value){ // turn the signals into the numbers on the buttons, and assign actions to buttons

case 109562864: addWaarde(1); break;

case 109562824: addWaarde(2); break;

case 109562856: addWaarde(3); break;

case 109562872: addWaarde(4); break;

case 109562820: addWaarde(5); break;

case 109562852: addWaarde(6); break;

case 109562868: addWaarde(7); break;

case 109562828: addWaarde(8); break;

case 109562860: addWaarde(9); break;

case 109562876: addWaarde(0); break;

case 109562818: //oud ding enter/R CLK handleInput(); break;

case 109562816: //oud ding power handleColor(); break; case 109562878: //oud ding cancel/L CLK resetColor(); break;

case 109562866: //lege knop midden randomColor(); break;

case 109562830: // pijltje naar rechts displayEverything(); break; case 109562838: // esc case 109562822: // AV source resetEverything(); break; }

// assign buttons on the remote for controlling the color: this way, players can see what the colors look like and how to combine them, // before starting a game. switch(results.value){ case 109562840: rood = 255; break;

case 109562844: groen = 255; break;

case 109562850: blauw = 255; break;

case 109562836: rood = 0; groen = 0; blauw = 0; break; }

}

analogWrite(redPin, abs(255 - rood)); // controlling RGB led: in reverse, because mine is wired in reverse. analogWrite(greenPin, abs(255 - groen)); analogWrite(bluePin, abs(255 - blauw));

// controlling live leds if (lives == 2){ levens1 = 0; } if (lives == 1){ levens2 = 0; } if (lives == 0){ levens3 = 0; } analogWrite(levensPin1, levens1); analogWrite(levensPin2, levens2); analogWrite(levensPin3, levens3);

// controlling level leds if (levelNumber == 1){ level1 = 255; } if (levelNumber == 2){ level1 = 255; level2 = 255; } if (levelNumber == 3){ level1 = 255; level2 = 255; level3 = 255; } if (levelNumber == 4){ level1 = 255; level2 = 255; level3 = 255; level4 = 255; } analogWrite(levelPin1, level1); analogWrite(levelPin2, level2); analogWrite(levelPin3, level3); analogWrite(levelPin4, level4);

}

void addWaarde(int value) { // add pressed value to list

if (indexNumber == 3) { return; }

if (input[indexNumber] == -1){ input[indexNumber] = value; indexNumber++; } }

void handleInput() { // make a 'waarde'(value) out of the list waarde = abs(input[0] * 100 + input[1] * 10 + input[2]); input[0] = -1; // reset de signalen input[1] = -1; input[2] = -1;

indexNumber = 0; Serial.print("waarde opgeslagen: "); Serial.println(waarde); addColor(waarde); }

void addColor(int waarde) { // add the 3-digit 'waarde' to a list

if (indexColor == 3) { return; }

if (color[indexColor] == -1){ color[indexColor] = waarde; if (color[indexColor] > 255){ color[indexColor] = 255; } indexColor++; }

}

void randomColor(){ // make the light turn a random color and save it in variables roodRandom = random(0,255);

groenRandom = random(0,255);

blauwRandom = random(0,255);

rood = roodRandom; groen = groenRandom; blauw = blauwRandom; Serial.print("roodRandom = "); Serial.println(roodRandom); Serial.print("groenRandom = "); Serial.println(groenRandom); Serial.print("blauwRandom = "); Serial.println(blauwRandom);

}

void handleColor() { // turn the 'waarde' from the list into 'Gok' (guess) variables roodGok = color[0]; groenGok = color[1]; blauwGok = color[2]; // check if the Gok variables differ too much with the real random color: if so, display the guess for three seconds, blink green and change the color again, add a level abd make the allowed difference smaller // if not, display the guess for three seconds, blink red and deduct a life // if you're out of lives, the game starts again if (abs((rood + groen + blauw) - (roodGok + groenGok + blauwGok)) <= diff){ analogWrite(redPin, abs(255 - roodGok)); analogWrite(greenPin, abs(255 - groenGok)); analogWrite(bluePin, abs(255 - blauwGok)); delay(3000); analogWrite(redPin, 255); analogWrite(greenPin, 0); analogWrite(bluePin, 255); delay(300); analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(300); analogWrite(redPin, 255); analogWrite(greenPin, 0); analogWrite(bluePin, 255); delay(300); analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(300);

Serial.print("roodGok = "); Serial.println(roodGok); Serial.print("groenGok = "); Serial.println(groenGok); Serial.print("blauwGok = "); Serial.println(blauwGok); resetColor(); randomColor(); levelNumber++; diff -= 50; } else{ analogWrite(redPin, abs(255 - roodGok)); analogWrite(greenPin, abs(255 - groenGok)); analogWrite(bluePin, abs(255 - blauwGok)); delay(3000); analogWrite(redPin, 0); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(300); analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(300); analogWrite(redPin, 0); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(300); analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(300); Serial.print("roodGok = "); Serial.println(roodGok); Serial.print("groenGok = "); Serial.println(groenGok); Serial.print("blauwGok = "); Serial.println(blauwGok);

resetColor(); lives--; } if (lives == 0){ analogWrite(redPin, 0); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(100); analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(100); analogWrite(redPin, 0); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(100); analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(100); analogWrite(redPin, 0); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(100); analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(100); resetEverything(); }

}

void resetColor(){ // use for reset values, for a wrong press or every new guess (just for certainty) color[0] = -1; color[1] = -1; color[2] = -1;

indexNumber=0; indexColor=0; }

void resetEverything(){ // restart the game color[0] = -1; color[1] = -1; color[2] = -1;

indexNumber=0; indexColor=0;

lives = 3; rood = 0; groen = 0; blauw = 0;

roodRandom = 0; groenRandom = 0; blauwRandom = 0;

levens1 = 255; levens2 = 255; levens3 = 255;

level1 = 255; level2 = 0; level3 = 0; level4 = 0;

levelNumber = 1;

t = 0; }

void displayEverything(){ // use this to display important information on the serial monitor Serial.print("roodGok = "); Serial.println(roodGok); Serial.print("groenGok = "); Serial.println(groenGok); Serial.print("blauwGok = "); Serial.println(blauwGok); Serial.print("roodRandom = "); Serial.println(roodRandom); Serial.print("groenRandom = "); Serial.println(groenRandom); Serial.print("blauwRandom = "); Serial.println(blauwRandom);

Serial.print("rood = "); Serial.println(rood); Serial.print("groen = "); Serial.println(groen); Serial.print("blauw = "); Serial.println(blauw);

Serial.print("waarde opgeslagen: "); Serial.println(waarde); }

Step 3: Build

You obviously want a nice build for this project. this part is pretty open to interpretation, but there are some things to keep in mind with this:

- keep the front panel thin, so you can still stick your leds through it and the IR-receiver still works

- keep enough space in the build for the arduino and a solder board

- make sure there is a hole in the back for the power of the arduino

I've added a rough scheme for the wiring.

Good luck, and have fun!