Arduino Programming Essentials: Logic & Control Flow
Digital Read Serial Example
This section presents an Arduino sketch demonstrating how to read a digital input and print its state to the Serial Monitor.
/*
Leen - Digital Read Serial - May 13
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the Serial Monitor
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 4;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
RGB LED Dimmer Sketch (Placeholder)
This section is intended for an RGB LED dimmer sketch. The provided code is a duplicate of the Digital Read Serial example. Please replace it with the correct RGB LED dimmer code for proper functionality.
/*
Leen - Digital Read Serial - May 13
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the Serial Monitor
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 4;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
Arduino Boolean Logic: TRUE and FALSE
In Arduino programming, the numbers 0
and 1
are directly connected to the boolean terms TRUE
and FALSE
:
TRUE
is represented by1
.FALSE
is represented by0
.
Specifically, in logical operations, 1
signifies “true” and 0
signifies “false”.
Logical Operators: OR (||) vs. AND (&&)
Understanding the difference between logical OR
and AND
operators is crucial for conditional logic:
- OR (
||
): ReturnsTRUE
(1
) if at least one condition is true. - AND (
&&
): ReturnsTRUE
(1
) only if both conditions are true.
The Equality Operator (==) in Arduino
The ==
command is used to check for equality between two values:
a == b
checks if the value ofa
is equal to the value ofb
.- If they are equal, it returns
TRUE
(1
); otherwise, it returnsFALSE
(0
).
Main Relational Operators in Arduino
Relational operators are used to compare two values. The primary relational operators include:
==
(equal to)!=
(not equal to)<
(less than)>
(greater than)<=
(less than or equal to)>=
(greater than or equal to)
Return Values of Relational Operators
Relational operators return an integer value representing a boolean state:
1
forTRUE
0
forFALSE
Conditional Statements in Flowcharts
In flowcharts, conditional statements are visually represented as diamonds. Inside the diamond, a decision is made (e.g., “Is one number less than another?”). Based on the result (TRUE
or FALSE
), different actions or paths are executed within the program flow.
Program Command Execution Order in Arduino
Commands in an Arduino program execute sequentially. This means that each command runs one at a time, in the exact order they are written from top to bottom within the sketch.
Control Flow Commands: Purpose and Function
Control Flow commands, such as loops
(e.g., for
, while
) and if statements
, dictate the order in which different parts of the program run. They enable functionalities like repeating actions multiple times or choosing different execution paths based on specific conditions.
Pseudocode vs. Flowchart: Key Differences
Both pseudocode and flowcharts are tools for planning program logic, but they differ in their representation:
- Pseudocode: A written description of what a program does, expressed in plain, human-readable language, resembling code but without strict syntax rules.
- Flowchart: A visual diagram that uses standard symbols to illustrate the steps, decisions, and overall flow of a program.
Purpose of Semicolons in Arduino Sketches
In Arduino (C/C++ based) code, semicolons (;
) are essential. They act as statement terminators, indicating to the compiler that a command or instruction is complete. Every statement in an Arduino sketch must end with a semicolon.