}
void loop() {}
while
The while statement is another branch command that does continuous looping. If the condition
following the while is true, the commands within the braces are executed continuously. Here is
an example that continuously reads a switch on pin 3, and then when the switch is pressed, the
condition is no longer true so the code escapes the while command and prints.
void setup()
{
Serial.begin(9600);
while(digitalRead(3) == HIGH) {
}
Serial.println("Switch was pressed");
}
void loop() {}
goto
The goto statement commands the computer to jump immediately to another part of the program
marked by an address label. The goto should be used sparingly because it makes the program
hard to follow, but is handy for breaking out of nested loops or other complex control structures.
Here is an example
void setup()
{
Serial.begin(9600);
while(true) {
if (digitalRead(3) == LOW) {
goto wrapup;
}
}
wrapup:
Serial.println("Switch was pressed");
}
void loop() {}
The while(true) statement runs continuously, checking the state of pin 3 each time. When pin 3 is
low (pressed), the if condition is true and the goto statement executed, breaking out of the while
loop.
functions
Functions are a powerful programming feature that are used when you want to set up an action
that can be called from several places in the program. For example, let's say you wanted an LED
connected to pin 2 to flash 3 times as an alert, but that you needed to execute the alert at three
different places in the program. One solution would be to type in the flashing code at the three
separate program locations. This uses up precious code space and also means that if you change
the flash function, for example changing from 3 flashes to 4, you have to change the code in
three places. A better solution is to write the flash function as a subroutine and to call it from the
main body of the code. Here is an example
24
Need help?
Do you have a question about the uno and is the answer not in the manual?
Questions and answers