Introduction: Aruidno: LED Knock Puzzle

Hello everybody.

In this instructable, I'll show you what I made for my school project If This Then That (ITTT), i will also show you why I made this and how.

What I have made:
Well, i made a Multiplexed LED Puzzle Game, That also has a Temperature mode. The puzzle game works with a knock sensor (aka a piezo that has been programmed to work as a knock sensor) and it detects if you knock ones, twice or three times (the amount of times is easily modified), the game is a bit like Mastermind, LEDs will turn on if you do the correct knock pattern at the correct time, but if you hit the wrong one the level resets and the LEDs turn off. For instance the level would be made out of 5 stages, every stage is one knock pattern and after completing all stages the level will be completed and you will move on to the next one.

TL;DR: a Puzzle Game that works with knocks.

Why I made this:

For this project we had a kit that contained the following:

  • 1 Arduino Uno
  • 1 breadboard
  • TMP36GZ Temperature Sensor
  • Rainbow LED pack
  • Panel Mount Rotary Potentiometer Breadboard Friendly
  • Piezo Buzzer
  • 5 PCB Pushbutton. 6x6mm size
  • 1 photoresistors
  • 1 Force Sensitive Sensor
  • 1 Set of breadboard jumper wires
  • perf / strip board
  • Micro Servo

WARNING: Before you buy these items i did not use all the items listed above.

I just looked at the list that i listed above and I was like, "what can i make with this". I experimented with all the items, and i wanted to make something original, so not some build, and I also tried to use almost only things that came in the kit. I also wanted to show my passion as a game designer in training, so that why I made a a game. I used a knock sensor because this is a unusual way of input for a game, and doesn't hinder this game in any way. First of I made a thermometer, that shows by turning on more LEDs and with the color of the leds (It can be found here). And when I was done with this I started on the game.

TL;DR: wanted to use the items in the kit and also wanted to show my passion as game designer in training.

How:
Before I start about how i made it i want to mention that I am pretty sure that how i did it is not the best way. And that you need basic sodering skills. The most work in this project is on the coding and its pretty easy to modify, so should be doable in a short timespan.

I made three pieces of code and we will go through them in the following order: Mode select, Knock Sensor & Game and last Temp.

Step 1: Needed Items and Tools

Items Used:

  • 1x - Arduino Uno
  • 1x - Rainbow LED pack (see picture for LED colors)
  • 1x - TMP36GZ Temperature Sensor
  • 1x- Piezo Buzzer3 PCB Pushbutton. 6x6mm size
  • 6x - 1K Ohm Resistors
  • 1x - 960K Ohm Resistor (use a 1M Ohm Resistor if you can, for a better read of the knock sensor)
  • 1x - 1.5K Ohm Resistor (I used a 1K and two 220 Ohm Resistors)
  • 6x - NPN Transistors
  • 1x - Strip Board
  • Cables, I used normale wires and breadboard jumper wires (I would advise to only use normale wires because of $$$)

Needed Tools:

  • Soldering iron
  • Cutters
  • Stripping Pliers

Step 2: Programming - Mode Select

There are three buttons button1 is the Left button, button2 Is the Confirm button (it's in the middle) and button3 is Right button.

The Code will check if the mode is confirmed, if it is confirmed it will run the following code:

    button2State = digitalRead(button2Pin);
    if (button2State == HIGH)
      modeConfirmed = false;
      
    if (mode == 0)  {
      Serial.println("Normal");
    }
    else if (mode == 1)  {
      Serial.println("Random");
    }
    else if (mode == 2)  {
      Serial.println("Die Hard");
    }
    else if (mode == 3)  {
      Serial.println("TEMP MODE");
    }
    else {
      Serial.println("TEST MODE");
      //All LEDs blink
    }

It will check if button2 got pressed again to deselect the mode. And it will run the selected mode, this will be combined with the other code later on.

If the mode is not confirmed it will check if you are changing the mode, and if so to what. It will also give output to the user what the mode is, by turning on more LEDs (this will also be added later on).

button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin); button3State = digitalRead(button3Pin); if (button2State == HIGH) { modeConfirmed = true; //Yellow LED On (LED 5) delay(500); //Yellow LED Off (LED 5) } else if (button3State == HIGH) { if (mode <= 3) mode++; else mode = 0; } else if (button1State == HIGH) { if (mode > 0) mode--; else mode = 3; } if (mode == 0) { //LED 1 and 2 On } else if (mode == 1) { //LED 3 and 4 On } else if (mode == 2) { //LED 6 and 7 On } else if (mode == 3) { //Led 8 and 9 On }

NOTE: The Combined code will be at the and of the Intructible.

Step 3: Programming - Knock Sensor & Game Mode

This Code will check if there is a knock (it will register a knock when the variable "threshold" <= input), if there is a knock it will start a timer, and it will wait for another knock. If there is no knock within "timeout" (variable) time it will register the ammount of knock that have been done till it timed out.

while ((timeNow - timeStart < timeout) && knock < 3) { // check for knock till timed out or knock = 3
sensorReading = analogRead(knockSensor); timeNow = millis(); if (sensorReading >= threshold) { knock ++; Serial.println(knock); timeStart = millis(); delay(100); Serial.println("Knock"); }

After that it will check if it timed out or reached 3 knocks, if that is the case it will run the following code:

if (knock == answers[correctCounter]) {
correctCounter ++; //turn on leds of the correct awnsers (leds / total awnsers * correct awnsers (correctCounter))

Serial.println("Correct"); if (correctCounter == ammountAnswers) { level ++; //leds off correctCounter = 0; Serial.println("Level Cleared");

Serial.println(level); if (level == 2) { ammountAnswers = 4; //Change answers[] (array) to answers of the level Serial.println("Level 2"); } else if (level == 3) { ammountAnswers = 5 //Change answers[] (array) to answers of the level Serial.println("Level 3"); } } }

It will check if the answers was correct, if it is correct it will go to the next stage within the level and it will check if you finished the level. If you finished the level it will go to the next level

If you answer was not correct it will run the following code:

//leds off
correctCounter = 0;
Serial.println("False");

This will reset the stages and give feedback.

at the end of the code it will reset the knocks and timer, so that the code can run again.

knock = 0;
timeStart = 0; timeNow = 0;

NOTE: There are allot of notes within the downloadable version that explain what the code does.
and the code for the LEDs will be added later in the instructable.

Step 4: Programming - Thermometer

The code will check how many volts the Temperature Sensor returns, and converts this to °C.

temp = analogRead(tempSensPin);
float voltage = temp * 5.0; voltage /= 1024.0; Serial.print(voltage); Serial.println(" volts"); float temperatureC = (voltage - 0.5) * 100 ; Serial.print(temperatureC); Serial.println(" degrees C");

After this the code will indicate the temperature by turning on more leds as the temperature increases, it will always run for about 1 second.

for (int x = 0; x < 333; x++) {
LED1.pulse(); if (temperatureC > 0) { LED2.pulse(); x ++; } if (temperatureC > 5) { LED3.pulse(); x ++; } if (temperatureC > 10) { LED4.pulse(); x ++; } if (temperatureC > 15) { LED5.pulse(); x ++; } if (temperatureC > 20) { LED6.pulse(); x ++; } if (temperatureC > 25) { LED7.pulse(); x ++; } if (temperatureC > 30) { LED8.pulse(); x ++; } if (temperatureC > 35) { LED9.pulse(); x ++; } }

The LEDS do not just turn on they blink really fast, because they only use the power of the arduino on USB, and the arduino only supplies enough amps to turn on 3 LEDs on the same time.

The only thing you need to know about the classes are that the the first number is what pin supplies power for the transistor thats connected to the +, the second number is the pin that supplies power to the transistor that connected to the - ,and the last number is for a delay before the LED gets turned off again.

DigitalPin LED1(12, 13, 3); // PinA1 = 12, PinC1 = 12, delay = 3ms.

You can see the code in action right here:

NOTE: The code uses classes that can be found in the downloadable version.

Step 5: Soldering

The first picture shows how I mapped it out, the black things are Transistors, and the Transistors with a R are Reversed. Blue wires go to Digital pins, the Red wires at the LEDs go to the anode of the LED, The Black ones at the LEDs go to the cathode. The top Red wire go's to the 5V on Arduino and the black one to the Ground on the Arduino. All white wires are resistors, before the Transistors are the 1K Ohm Resistors and on the 5V Red wire there is a 1.5K Ohm wire.

The second picture showed how I sodered it (and yes I did not cut all the wires).

If you want to map it out yourself, and want to use my setup you can, here is the schematic I made:


Step 6: Casing

I made no casing because time ran out, but this was my fast concept for a casing. There could be markings for Temperature on one side.

IMPORTANT: Make sure that the Buzzer / Knock Sensor is touching the casing, otherwise it will not pickup the vibrations.

Step 7: The End

I planned to also make a Random mode and a Die Hard mode, but I did not have enough time.
The random mode speaks for itself, the levels would be random instead of made by a person.
The Die Hard mode would be a mode with a timer, with no other output for time then the yellow light that starts blinking faster and faster as time goes by.

The Combined Code can be found at this Step.

Arduino Contest 2016

Participated in the
Arduino Contest 2016