page17
New Program Elements in digitalInput sketch
pinMode(inputPin, INPUT);
inputValue=digitalRead(inputPin);
if (inputValue==LOW) {
... things to do...
}
else {
... things to do....
}
Set Pin Mode: Done in the setup function. This says that we want an input on the inputPin.
Reading the input: This is done every time the sketch goes round the main loop. The sketch looks at the value of the input pin and stores either HIGH or LOW in the variable called “inputValue”. Think of the variable as a labelled box to store values. Like other variables the type of “inputValue” was declared at the start of the program
IF statement: This is used if you want to make the sketch do different things based on the environment or the results of a past activity.
IF statement: This is used if you want to make the sketch do different things based on the environment or the results of a past activity.
Condition: After the word “if” you have brackets that contain the condition that is being tested. The two equals signs (“==”) means “has the value of” or “is the same as”. Here we see if the variable inputValue contains the value “LOW”.
Other possible conditions include “!=” for “not equal to”, “>” for “greater than” and “<” for “less than”.
True actions: After the condition you have a list of statements surrounded by curly brackets. These are what the sketch does if the condition being tested turns out to be true.
False actions: After the true actions you may have the world “else” and then another list of actions in surrounded by curly brackets. These are what the sketch does if the condition being tested is false.