Add a pushbutton switch and resistor to the project to use it as an input control.
The simplest input component is a switch which internally connects ("close") and disconnects ("open") the 2 wires connected to the switch.
The Arduino UNO includes several input pins that can connected to components to obtain information, status, and other data.
With a switch connected to the input pin, an Arduino program can determine if the switch is opened or closes.
In digital electronics and programming we refer these input values as TRUE/FALSE, HIGH/LOW, ON/OFF, 1/0. etc. These all mean the same thing!
In this exercise, we'll connect a pushbutton switch between an Arduino input pin and GND. When the switch is closed, the input pin is connected to GND. An Arduino program can read this digital value. In this case, pressing the button will be "LOW."
When the switch is open (unpressed), the input pin would not be connected to a circuit and would have a noisy, unpredictable value.
We use a "pull up" resistor between the input pin and VCC. This insures that the input pin value is always "HIGH" when the button isn't pressed. When the button is pressed, the connection to GND "pulls" the input pin to "LOW."
Optional reading:
Refer to the diagram, below.
Insert the pushbutton switch into the breadboard, straddling the channel between the "e" and "f" columns. (The row doesn't matter - your choice. ) Orient the switch so that the terminals sticking out from the case are on the left and right (not up and down.)
Insert one lead (wire) of the 10K resistor in a hole (h, i, or j) on the same row as the upper terminal of the switch.
Insert the other lead of the resistor into one of the "+" holes on the right.
(The orientation of the resistor doesn't matter -- resistors do not have "polarity." But, LEDs do!)
Insert one end of a short length of hookup wire, with the insulation stripped off, in a hole i on the same row as the lower of the switch. Insert the other lead of the wire into one of the "-" holes on the right. (You can also use a jumper wire instead of the hookup wire.)
Note that the 2 switch terminals on the left side of the breadboard channel (a, b, c,d, e) are unconnected. We're not using them.
Insert one end of a jumper wire in a hole h on the same row as the upper terminal of the switch.
Insert the other end of the jumper wire into pin "5" on the UNO.
Copy the program below into Codecraft.
The "If" block is in the "Control" menu.
The comparison block is in the "Operators" menu. (See example -->)
The "DigitalRead" block is in the "Input" menu.
Be sure to change the input to "D5" to read the button.
You've already used the "LED pin" block in the first exercise.
Upload the program.
When you press the button, the small LED on the UNO board should turn on. When you release the button, it should turn off.
Not working? Check your wiring and code carefully!
Ask for help if you're stuck.
Use your new knowledge of buttons and If/DigitalRead and modify the RGB LED program you previously created. Some ideas:
Pressing the button displays one sequence of the LEDs blinking
Each button press lights up another LED so you can count to 8 with the button. (The 9th press resets back to all LEDs off)
While the button is released, the LEDs are blinking in random colors. When the button is pressed, the LED color remains the same for 5 seconds, then continues with random colors.
Your ideas?
Use a "count with" block to change the RGB values to fade the brightness up over time.
Assign random colors to each LED.
The "random" block is in the "Operators" menu.
Wait for the button to be released before turning off the LEDs.
If you click the "</>" button on the upper right of the Codecraft screen, you'll show the Arduino C/C++ code that you've been writing with the grapical block coding tool.
Here's the Arduino C/C++ code for the first program in this workshop that blinks the LED. Can you recognize your blocks in the text code?
void setup(){
pinMode(5, INPUT);
pinMode(13, OUTPUT);
}
void loop(){
if ((digitalRead(5) == 0)) {
digitalWrite(13,HIGH);
} else {
digitalWrite(13,LOW);
}
}
You can use your Arduino kit to explore Arduino text programming with the Arduino IDE [https://www.arduino.cc/en/software]