Hardware Reference
In-Depth Information
If switchState is high or connected to 5 volts…
…print “The switch is on!” via serial.
If switchState is low or connected to ground…
…print “The switch is off!” via serial.
Pause for half a second to slow down the output of the sketch.
When you upload this code to Galileo and open the serial monitor, you should
see The switch is off! printed repeatedly in the window. Push down on the
switch and it should change!
digitalRead()
The main takeaway from Example 4-1 is the use of digitalRead() , which
checks the value of the pin provided in the parameters. In this case, you pro-
vided the variable switchInputPin , which evaluates to 2. After checking to
see if pin 2 is connected to 5 volts or ground, digitalRead() returns either
HIGH or LOW respectively. That value is stored in a new variable called
switchState .
You may be wondering: HIGH and LOW aren't integers, so why
are we storing them as variables with the type int ? The reason
is that Galileo thinks of high and low as the integers 1 and 0
and therefore HIGH and LOW are defined to mean 1 and 0 re-
spectively in Arduino code. This makes the code a little bit
easier to read and understand.
To prove that HIGH and LOW are equivalent to 1 and 0, try using
Serial.println() to print the value of HIGH + HIGH to the serial
monitor.
You can also see that you used pinMode() to set pin 2 as an input in the setup
function. You're required to do this if you want to use digitalRead() on that
pin.
The syntax of digitalRead() is:
digitalRead(pin);
The parameter of digitalRead is:
pin : the input pin number you want to read
digitalRead() returns HIGH or LOW.
 
Search WWH ::




Custom Search