Chapter 14

So far, Arduino has been:

  • reading inputs

  • changing outputs

  • following instructions

Now we make it decide.

This chapter is about how Arduino chooses what to do based on conditions — which is the foundation of all intelligent behavior in embedded systems.


🔁 Understanding the Loop

You already know this:

void loop() {
  // code here
}

But let’s say it clearly:

loop() runs continuously, thousands of times per second.

Arduino doesn’t run code once and stop.
It checks conditions repeatedly and reacts.

This is very different from normal computer programs.


❓ Conditions — Asking Questions

if-else

A condition is a question with a YES or NO answer.

Example:

if (digitalRead(buttonPin) == LOW) {
  // do something
}

Arduino asks:

“Is the button pressed right now?”

If YES → run the code
If NO → skip it

That’s decision-making.


🧠 The if Statement

Basic form:

if (condition) {
  // runs if condition is true
}

Example:

if (sensorValue > 500) {
  digitalWrite(ledPin, HIGH);
}

Meaning:

“If the sensor value crosses a threshold, turn the LED ON.”


🔄 else — The Other Case

if (condition) {
  // do this
} else {
  // do that
}

Example:

if (buttonState == LOW) {
  digitalWrite(ledPin, HIGH);
} else {
  digitalWrite(ledPin, LOW);
}

Now Arduino explicitly handles both situations.


🔁 Multiple Conditions (else if)

Sometimes, two options aren’t enough.

if (value < 300) {
  // low
} else if (value < 700) {
  // medium
} else {
  // high
}

This is how you build:

  • ranges

  • modes

  • states

Very common in sensor-based systems.


🔂 Loops Inside Code (for and while)

for-while

for Loop — Repeating a Known Number of Times

for (int i = 0; i < 5; i++) {
  blinkLED(9, 200);
}

Used when:

  • count is known

  • behavior is repetitive

while Loop — Repeating Until a Condition Changes

while (digitalRead(buttonPin) == LOW) {
  // keep doing something
}

Be careful:

  • if the condition never changes, Arduino gets stuck

In embedded systems, while must be used very carefully.


⚠️ A Critical Embedded Rule

Never block the system unintentionally.

Bad example:

while (true) {
  // infinite loop
}

Unless you really know what you’re doing, this freezes everything.

This connects back to the delay vs millis() chapter.


🧪 Example: Button-Controlled LED

if (digitalRead(buttonPin) == HIGH) {
  digitalWrite(ledPin, HIGH);
} else {
  digitalWrite(ledPin, LOW);
}

Arduino:

  • reads input

  • evaluates condition

  • decides output

  • repeats

Try pressing the button and observe how the LED responds in real-time.

This is the core loop of all control systems.


Which loop is guaranteed to execute at least once? for loop while loop do...while loop Which statement is used to make decisions based on a condition? break if...else continue do...while loopLoops allow a program to run atleast once as it checks the condition after running the block if...else enable the microcontroller to make decisions based on sensor inputs or other conditions.

🧰 Common Beginner Mistakes

  • Using = instead of ==

  • Forgetting braces {}

  • Writing deeply nested conditions

  • Blocking logic with long delays

Simple logic > clever logic.


📌 What Comes Next

Now that Arduino can:

  • read inputs

  • make decisions

  • execute logic

We need to make sure those inputs are stable by design.

👉 Next Chapter — Pull-up & Pull-down Resistors (Internal vs External)