if, for and functions
Three pieces of C++ carry most sketches: for repeats a block a counted number of times, if runs a block only when a test is true, and a function gives a block a name so it can be used again. All three, on the lamp on pin 13: L on the UNO, D13 on the Nano.
for: a block, a counted number of times
for (int i = 0; i < n; i++) {
// the block
}The three parts in the brackets are a start (int i = 0), a test (i < n)
and a step (i++, which adds one to i). The chip does the start once, then
the test; if the test is true it runs the block, does the step, and goes back
to the test. The first time the test is false, it skips past the closing
brace.
Counting from 0 with < runs the block exactly n times, and that is the
usual way to write it. The test runs one more time than the block: the
fourth test, 3 < 3, is the one that ends the loop. i exists only inside
the for; after the closing brace there is no i to read.
Functions: a name for a block
void blinkTimes(int n) makes a function. void says it hands nothing back;
int n says the caller gives it one whole number, which is called n inside.
blinkTimes(3) in loop() runs the block with n standing for 3, and the
chip carries on after the call when it reaches the closing brace.
A function earns its place when the same lines would otherwise be written
twice, or when its name says more than its lines do. blinkTimes(count)
reads as what it does. setup() and loop() are functions in the same sense;
the only difference is that main() calls them for you.
A function can also hand a value back. int twice(int x) { return 2 * x; }
returns an int, and twice(4) can stand anywhere a number could.
if: a block, only when the test is true
if (count > 5) {
count = 1;
}The test is anything that is true or false: >, <, >=, <=, == for
equal and != for not equal. Join two with && (both) or || (either). An
else after the closing brace gives the block for when it is false.
In the sketch, count goes up by one on every pass of loop(), and the if
catches it at 6 and puts it back to 1. The test is written count > 5 rather
than count == 6 on purpose: if count ever skipped past 6, == would let
it run on for ever, and > would still catch it.
Equal is two signs, ==. One sign, count = 5, stores 5 in count, and as a
test it is always true. Both compile.
What this sketch cannot do
Every blink in this sketch is spent inside delay(), and while the chip is
there it does nothing else. That is fine for a lamp and wrong for anything
that also has to listen. millis() instead of delay()
is the way out.
The code
The lamp on pin 13 blinks once, then twice, up to five times, with a second's gap between groups, and the Serial Monitor at 9600 baud says how many blinks are coming.
// Blink_Times.ino — blink pin 13 once, twice, ... five times
// Tools > Board: Arduino Uno, or Arduino Nano with
// Processor ATmega328P (Old Bootloader if the upload says
// "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: none
int count = 0;
// Blink the pin-13 lamp n times, then return.
void blinkTimes(int n) {
for (int i = 0; i < n; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
}
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
count = count + 1;
if (count > 5) {
count = 1; // back to one after five
}
Serial.print("blinking ");
Serial.println(count);
blinkTimes(count);
delay(1000); // a gap, so the groups can be counted
}Count the blinks against the monitor. If a group has one too many or one too few, the for line is the place to look.
When it does not work
The test is i <= n instead of i < n. Counting from 0, i < n runs the block n times: 0, 1 and 2 for n = 3. Starting at 1 needs i <= n. Pick one way and use it everywhere.
Check for one equals sign where two were meant. if (count = 5) stores 5 in count and is always true; if (count == 5) compares. The compiler accepts both. Setting File, Preferences, Compiler warnings to All makes the IDE point the first one out.
The name is spelt differently where it is called and where it is defined, and C++ cares about capitals: blinkTimes and blinktimes are two names. Check the spelling, and that the function is outside setup() and loop(), not inside one of them.
That is delay(): while blinkTimes() is waiting, the chip does nothing else. For three blinks that is 1.2 seconds in which a button or a typed character is not looked at. The next page replaces delay() with millis() for that reason.
Blink and still listen: timing a sketch by looking at the clock instead of stopping it.
millis() instead of delay() →Edit this page — content/books/atmega328p/if-for-and-functions.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.