In this project we are going to build a keyboard to control how an LED operates!
What you will need
Seven long wires (One red and one black)
Five small wires
One LED
Four buttons
One 220-ohm resistor (red – red – brown – gold)
Four 10k-ohm resistors (brown – black – orange – gold)
Wire up the circuit as shown below:
If it is difficult to see, aside from 5V power and GND we are going to be using ports 2,3,4,5, and 12.
Once you have your circuit wired we are going to write the program for one button. You will be responsible for coding the second, third, and fourth buttons.
Open up your Arduino software and type in the following:
Let us go through the code line by line starting with the setup function.
These two lines of code say, “Make pin 12 output electricity and turn the electricity off”. This will ensure our light is turned off at the start of our program.
This line of code says, “Make pin 2 take in electricity so we can gather information from it”. When the button is not pressed pin 2 will read LOW, as in there is no electricity going to it. When you press the button down it completes the circuit and it will read HIGH.
Next we should look at our looping function.
This is an integer variable that will store the state of the button wired in to pin 2. It will either store HIGH or LOW depending on if the button is pressed.
This is our very first conditional statement! Please note that there are two equal signs next to each other. This is so the Arduino knows we are trying to compare buttonOne and HIGH to see if they are the same. If you only have one equal sign it will try to overwrite buttonOne’s data.
This function is saying, “If buttonOne has electricity then turn the light on, wait one second, and then turn the light off again.”
If buttonOne is not pressed, then this segment of the code will not run. The light only turns on and off if you press the button.
Upload the program and try pressing buttonOne!
You are responsible for coding the three other buttons. Before you do we will start you off with some additional code to get you started. Please note the differences.
Summative Assessment
A working circuit with one button: 50%
A working circuit with 4 buttons: 65%
A working circuit with 4 buttons where 80%
One button blinks slowly (greater than 1000 ms intervals)
One button blinks quickly (less than 100 ms intervals)
One button turns light on
One button turns light off
A working circuit with 4 buttons where 100%
One button blinks slowly (greater than 1000 ms intervals)
One button blinks in intervals controlled by a potentiometer
One button turns light on
One button turns light off