Arduino uno User Manual page 22

Hide thumbs Also See for uno:
Table of Contents

Advertisement

{
Serial.begin(9600);
}
void loop()
{
if (digitalRead(3) == LOW) {
Serial.println("Somebody closed the switch!");
}
}
The if line reads the state of pin 3. If it is high, which it will be for this circuit when the switch is
open, the code jumps over the Serial.println command and will repeat the loop. When you close
the switch, 0V is applied to pin 3 and its state is now LOW. This means the if condition is true so
this time around the code between the braces is executed and the message is printed
The syntax for the if statement is
if (condition) {
//commands
}
If the condition is true, the program will execute the commands between the braces. If the
condition is not true, the program will skip to the statement following the braces.
The condition compares one thing to another. In the example above, the state of pin 1 was
compared to LOW with ==, the equality condition. Other conditional operators are != (not equal
to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).
You can have the program branch depending on the value of a variable. For example, this
program will print the value of i only when it is less than 30.
int i;
void setup()
{
Serial.begin(9600);
i=0;
}
void loop()
{
i=i+1;
if (i<30) {
Serial.println(i);
}
}
for
22

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the uno and is the answer not in the manual?

Questions and answers

Table of Contents

Save PDF