In this project we are going to build a keyboard to control how an LED operates!
What You Will Need:
Six wires
One LED
Four buttons
One 220-ohm resistor
Four 10k-ohm resistors
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 and 12.
Again, look for the circuit being formed with the button.
Note: If you are wondering why we need a resistor (let alone such a strong resistor), the 10k-ohm resistor forces the digital input of the microcontroller to a known state (GND) when the button isn't pressed. You don't have to worry about this, just know it has to be there. It just prevents short-circuiting, misinputs, etc.
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 power and turn the power on”. 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 line is your first variable! Variables store values so you can use them later on in your program.
Because Arduino runs off a C based language you must declare what type of variable you want. If you are storing any regular number, the declaration int will suffice. We will discuss the different variable types in the future.
Next we named the variable “buttonOne”. This is how we will access the value in the future.
This variable 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 because it is a digital input.
This is also 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.
A working circuit with one button: 50%
A working circuit with 4 buttons: 75%
A working circuit with 4 buttons where: 100%
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
When you are ready to submit your project, submit your code and pictures of your project to Google Classroom under Project 2.
YOU MUST SHOW ME YOUR PROJECT BEFORE YOU SUBMIT IT!