Menu
CoddyTech

Decisions with if

Part of the First Circuits section of Coddy's Arduino journey. Lesson 18 of 22.

if runs some lines only when something is true:

if (waitTime > 200) {
  waitTime = waitTime - 200;
}

The part in parentheses is the condition. waitTime > 200 means "waitTime is more than 200", and it is either true or false. When it is true, the board runs the lines inside the braces. When it is false, it skips them and carries on after the closing brace.

< means less than. You will meet the other comparisons soon.

There is no semicolon after the condition. if (waitTime > 200); ends the if right there, with nothing in it, and the lines in the braces then run every time.

challenge icon

Challenge

Easy

The starter is the blink that speeds up and then freezes. Fix it:

  • only make waitTime smaller while it is more than 200

The blink should speed up and then keep blinking at 200 ms. Press Run, then Check.

Try it yourself

int waitTime = 1000;

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(waitTime);
  digitalWrite(13, LOW);
  delay(waitTime);
  // only do this while waitTime is more than 200
  waitTime = waitTime - 200;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in First Circuits

Practice on your own: Arduino playground