Project tutorial

# Infrared Controlled Logic Puzzle -- Lights On! © GPL3+

Blow the cobwebs out your ears with this fun yet challenging logic puzzle with an IR control. Try and get all the lights on!

• 3,123 views
• 12 respects

## Components and supplies

 Arduino UNO
×1
×1
 IR Remote - Generic
×1
 LED (generic) a range of colors will work the best!
×5
 Resistor 221 ohm
×5

## Apps and online services

 Arduino IDE

### What does it DO?

A fun and easy Logic Puzzle! Choose 6 Buttons on your IR remote. 5 of the buttons toggle different pairs/trios of LED's on and off, and one resets all the LED's. If the buttons are hit in the correct order...all the LED's will be on! Once you've solved it, simply change the values in your code to make a new test, or pass it onto to a friend and laugh piteously at their feeble attempts to solve your puzzle.

### Putting it together

Assembly of the circuit is very straight forward. Connect 5 LED's to the breadboard, with their negative leads directly into the Negative/ground power rail.

Connect a 220 ohm resistor (making sure their leads do not touch) to the positive lead of each LED, and then connect each Resistor to the Arduino's digital pins 3 to 7 inclusive. The order is not super important, but for debugging purposes it is handy to have them in the same order on the breadboard as they are connected to the Arduino, at least when you are testing the circuit.

### The Infrared Remote/Reciever

I bought my IR remote/receiver as part of a kit online, so yours may be slightly different. This circuit should work with any IR remote you have in your house (like the TV/DVD remote).

First, connect the IR receiver to a part of the breadboard where it will have an unobstructed line of sight to aim at it with the remote. A jumper wire hanging in front of the receiver will block the signal, so make sure its clear. The Receiver should have 3 pins, which are positive, negative and data. Generally the negative pin is in the middle, but depending on which receiver you have the side of the data/positive may change. If your circuit does not pick up any IR signals, try moving the wires around. Connect the data lead to digital pin 11 on the Arduino:

To use the remote to control the LED's we are going to have to pick some buttons on the remote to be our controllers. For my project I chose the buttons 1,2,3,4,5 and power (top left). In your Arduino IDE, add the the 'IRremote Library' by Shirriff:

We now need to use the library to test our IR remote, as as far as Im aware, they can all be a little bit different. Open a new Sketch with the following code.

``````#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);  //Setup the IR remote for initilisation
decode_results results;   // set a decode variable, called results
unsigned int val = 0; // A variable to store the IR reading
void setup() {
Serial.begin(9600) //Turn on the serial port
irrecv.enableIRIn(); // Start the IR receiver
}
void loop() {
if (irrecv.decode(&results)) {  //If the IR receiver gets a signal
val = results.value;          //Save that signal as a value to 'val'
Serial.println(val);          //To check the values from your remote
irrecv.resume();              // Recieve the Next value
}
}
``````

Now open the serial monitor, grab a pen and paper, and start hitting buttons. Have a close look at the numbers that are coming up on the monitor. If you hold a button down it should show a value, and then repeat a different number over and over. That repeated number will be the same for any button you press, its the receiver registering that a button is being held down. What we are interested in is the number that pops up first, or if you just tap the button without holding it. See below, the number I got for repeated values was 65535. IGNORE THAT NUMBER!

Pick the 6 buttons you'd like to use to toggle the LED's on and off and as a reset button. Its probably a good idea to pick 5 that are close together, and one separate from the rest. Jot down what number comes up on the serial monitor when you hit your chosen buttons, as we will hard code those numbers into our sketch.

### Almost there!

Now, open a new Sketch and copy or type in the full code from below. We should be ready to try it out!

The Sketch uses a switch function to assess the value recorded from the IR remote as 'val', and then if it matches a value that you set as one of your global variables it will run one of two functions. toggle2() toggles on/off 2 LED's. toggle3() does the same for 3 LED's. We then pass the toggle functions the arguments of which LED's we want toggled if that particular button is hit. For example, in the case of code1 being received, LED's 1 and 4 will turn on if they are off, or off if they are on. If code3 is received, LED's 3, 4 and 5 will toggle on/off. You can change these values to mix up the puzzle if you know the solution, or to make it harder or easier. Case6 just resets all the LED's to off.

Each LED is assigned an LED_State which is either 1 or 0 and we change it whenever we turn it on or off. This is the easiest way for an 'if' statement to test whether the LED is currently on or off.

All you have to do now, is replace the values for code1-code6 in the global variables in the top of the sketch, with the values you recorded from your own remote and start playing.

If anyone comes up with a particularly difficult rendition, please share the values you used in the toggle functions as I would love to give them a go!

Thanks and enjoy!

## Code

##### IR BrainteaserC/C++
```/* Arduino Project - Infrared Logic Puzzle
* This Sketch Allows you to control the Digital Settings
* of 5 LED's. The challenge is to turn them all on
*
*  See Fritzing Diagram attached in Github
*
* Created on December 12 2017 by FIELDING
*/

#include <IRremote.h>  //Infrared Remote Library

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);  //Setup the IR remote for initilisation
decode_results results;   // set a decode variable, called results

const int LED [] = {0, 7, 6, 5, 4, 3}; // Set LED's as Digital Pins 7 thru 3

int LED_State[] = {0, 0, 0, 0, 0, 0, 0}; // Inlcude one more than the number of LED's
// you have, first val must be zero. Sets all
// LED's to low/off

unsigned int val = 0; // A variable to store the IR reading

const int code1 = 12495;   //Set the values recieved
const int code2 = 6375;  //from the IR remote
const int code3 = 31365;  //for your chosen buttons
const int code4 = 4335;
const int code5 = 14535;
const int code6 = 41565;

void setup() {

Serial.begin(9600); // Turn on to allow you to check your IR remote values

irrecv.enableIRIn(); // Start the IR receiver

for (int i = 1; i < 6; i++) { // Set all LED's as OUTPUTS
pinMode(LED[i], OUTPUT);
}
}

void loop() {

if (irrecv.decode(&results)) {  //If the IR receiver gets a signal
val = results.value;          //Save that signal as a value to 'val'
Serial.println(val);          //To check the values from your remote

switch (val) {
case code1:                   //If the IR reciever picks up code1
toggle2(1, 4);              //Toggle these LED's on/off
break;
case code2:                   //If the IR reciever picks up code2
toggle2(2, 3);              //Toggle these LED's on/off
break;
case code3:                   //If the IR reciever picks up code3
toggle3(3, 4, 5);              //Toggle these LED's on/off
break;
case code4:                   //If the IR reciever picks up code4
toggle3(1, 4, 5);              //Toggle these LED's on/off
break;
case code5:                   //If the IR reciever picks up code5
toggle3(1, 3, 4);              //Toggle these LED's on/off
break ;
case code6 :
for (int i = 1; i < 6; i++) { // If Code 6 is received, all LED's are
digitalWrite(LED[i], LOW);   // turned off
LED_State[i] = 0;
}
break ;
}
irrecv.resume(); // Recieve the Next value
}
}

//Create a function to toggle two LED's on/off
void toggle2(int x, int y) {    //Set x and y as two of the LED's (mix it up!)
if (LED_State[x] == 0) {       //check to see if LED is currently on low/off
digitalWrite(LED[x], HIGH);   // if its off, turn it on
LED_State[x] = 1;
} else {
digitalWrite(LED[x], LOW);    // if its on, turn it off
LED_State[x] = 0;
}
if (LED_State[y] == 0) {       //check to see if LED is currently on low/off
digitalWrite(LED[y], HIGH);   // if its off, turn it on
LED_State[y] = 1;
} else {
digitalWrite(LED[y], LOW);    // if its on, turn it off
LED_State[y] = 0;
}
}

//Create a function to toggle three LED's on/off
void toggle3(int x, int y, int z) {     //Set x and y as two of the LED's (mix it up!)
if (LED_State[x] == 0) {              //check to see if LED is currently on low/off
digitalWrite(LED[x], HIGH);         // if its off, turn it on
LED_State[x] = 1;
} else {
digitalWrite(LED[x], LOW);    // if its on, turn it off
LED_State[x] = 0;
}
if (LED_State[y] == 0) {       //check to see if LED is currently on low/off
digitalWrite(LED[y], HIGH);   // if its off, turn it on
LED_State[y] = 1;
} else {
digitalWrite(LED[y], LOW);    // if its on, turn it off
LED_State[y] = 0;
}
if (LED_State[z] == 0) {       //check to see if LED is currently on low/off
digitalWrite(LED[z], HIGH);   // if its off, turn it on
LED_State[z] = 1;
} else {
digitalWrite(LED[z], LOW);    // if its on, turn it off
LED_State[z] = 0;
}
}
```

## Schematics

• 3 projects
• 27 followers

#### Published on

December 12, 2017

#### Members who respect this project

and 9 others

See similar projects
you might like

#### Remote Controlled Lights

Project showcase by jsheng

• 2,088 views
• 10 respects

#### Traffic Lights Controlled by Arduino

Project showcase by lmsousa

• 10,179 views
• 21 respects

#### Create a People Counter Controlled by an Android App

Project tutorial by Kutluhan Aktar

• 7,369 views
• 8 respects

• 3,731 views
• 14 respects

#### Remote Controlled Bulb With Arduino

Project tutorial by muhammed shameel k.v

• 3,225 views