
06
If you press the button, the LED will light on, and it will light off when you release the button. If it
doesn’t, make sure the LED and button are properly connected to the corresponding Crowtail-Base
Shield interface.
What will you see
1. Declare the variables for LED and button and assign values to them.
2. Define whether the module is output or input.
3. Read the value of the button.
4. If the button value read is HIGH(pressed), turn on the LED.
5. If the button value read is LOW(not pressed), turn off the LED.
Code overview
Code usage
A variable is a placeholder for a value that may change in your code.
Variables must be introduced or "declared" before using variables.
Here, we declare two variables that define which ports of the base shield
the module should connect to and a variable called ‘buttonState’ of type
int(integer) and assign it a value of 0 to record the status of the button.
Don't forget that variable names are case-sensitive!
Integer Variables
Input or Output
Before using one of the digital pins, you need to tell Arduino
whether it is an input (INPUT) or an output (OUTPUT).
We use a built-in "function" called pinMode() to make the pin
corresponding to the led a digital output.
Digital Input
We use the digitalRead() function to read the value on a digital pin.
Check to see if an input pin is reading HIGH(5V) or LOW(0V).
Returns TRUE(1) or FALSE(0) depending on the reading.
If/else Statements
The if / else statement allows your code to make corresponding choices
for different results, running a set of code when the logical statement in
parentheses is true, and another set of code when the logical statement
is false. For example, if the button is pressed, the LED will light on
and when the button is released, the LED will light off.
This is another logical operator. The "equal" symbol (==) can be confusing.
The two equal signs are equal to ask: “The two values are equal
to each other? “On the other hand, if you want to compare two values,
don't forget to add a second equal sign, because if it's just a "=",
it's an assignment method.
Is equal to
When you're using a pin as an OUTPUT, you can command it to be
HIGH (output 5 volts) or LOW (output 0 volts). When you set it to HIGH,
for digital output modules, it means work. When you set it to LOW,
for digital output modules, it means don't work. For example, the led will
light on(work) when it is set to HIGH and it will light off(don't work)
when it is set to LOW.
Digital Output