A collection of Arduino tutorials put together to help a colleague dive into embedded systems!
Phil, look how far we have come. Just a few short tutorials ago, you knew nothing! And now you know slightly more than nothing! Such progress! Anyway, now that the pleasantries are out of the way, lets get down to the task at hand. Now, if you have been doing the homework exercises, you will have inevitably stumbled across many of the control structures that we will discuss here. But we both know that you haven’t been doing the homework exercises, as your daily plethora of excuses demonstrate.
He who is good at making excuses is seldom good for anything else.
Thanks, Benjamin Franklin, couldn’t have put it better myself.
Control structures in programmings are like traffic directing construction workers. They allow traffic to flow depending on which conditions the construction project meet. Sometimes they let all the traffic through. Sometimes they let traffic through one way and block the opposite way, and then when a certain condition is met (maybe a timer expires or a piece of machinery needs to move) they swap which lane can go through and which one is blocked. It may not be a perfect analogy, but you get the idea. Control structures are a mechanism for directing how a program flows.
The simplest way of controlling control flow is to have code execute sequentially. A good example would be how we have typically been using the setup()
function.
#include <Arduino.h>
#define PWM_PIN 5
#define LED_PIN 13
void setup() {
pinMode(PWM_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
We setup a pin for PWM, then we setup a pin for a LED, then we set the baud rate of the serial monitor. Pretty straight forward.
I am not sure what the correct term is for these but who cares. What is important is that we all have fun. Participation awards all round!
It looks like this:
if(expression) {
statement;
}
If the expression evaluates to 1 (AKA TRUE), the statement will be executed. If the expression evaluates to 0 (AKA FALSE), the statement will not be executed. We use ‘relational operators’ to evaluate expressions. They are:
==
!=
>
<
>=
<=
Pop quiz: What will be printed?
if(10 < 13) {
Serial.print("P");
} else {
Serial.print("H");
}
if(5 != 6) {
Serial.print("I");
} else if(7 < 7) {
Serial.print("L");
} else {
Serial.print("!");
}
I feel like I don’t have to explain how the if else
and else
works. If you don’t get it, there is no hope.
Build some more interesting logic using boolean operators:
||
&&
if(1 < 2) && (1 != 2) && (2 < 1) {
Serial.print("Boobs");
} else {
Serial.print("Bums");
}
This evaluates to:
if(TRUE) && (TRUE) && (FALSE) {
Serial.print("Boobs");
} else {
Serial.print("Bums");
}
which evaluates to false. Simple, isn’t it?
if(1 < 2) && (1 != 2) || (2 < 1) {
Serial.print("Boobs");
} else {
Serial.print("Bums");
}
What will the above evaluate to?
while(expression) {
statement;
}
for(initialize; condition; modifier) {
statement;
}
Work In Progress