This project is expected to be continuation of PJ019: 1st Experience with Arduino, after getting a basic idea of what is Arduino and the basic development flow, it's time to get familiar with C++ as a beginner. This project brings starting knowledge of C++ programming, covers variables, if-else and serial communication. This project is designed to be used for 2-3 learning sessions. Goals:
Enhance understanding of development flow with Arduino
Get to know basic syntax of C++, setup() and loop() integration with Arduino
Understand int, char and string type of variables, practice with basic if-else logic
Get to know Serial Communication with Arduino, learn how to use it for debugging and interaction with PC
START A BIT > Projects Library > This page
[ ] Simple: 30-60 minutes
[ ] Moderate: 1-2 hours
[x] Challenging: more than 2 hours
The estimation bases on average situation without unexpected troubleshooting, assuming that the student meets prerequisites. It only indicates rough time needed to complete this project, but not about technical difficulty, not covering discussion and sharing time. Here is a possible breakdown:
20-30 minutes: Basic knowledge input, preparations, and Task 1
30-45 minutes: Task 2
30-45 minutes: Task 3
20-30 minutes: Task 4
30-45 minutes: Task 5
15-30 minutes: Task 6
One should have prior knowledge
Nice to have: Micro:bit and basic projects experiences
KB028: Arduino IDE Basic Knowledge, KB027: Arduino UNO Boards
This project is designed as continuation of PJ019: 1st Experience with Arduino
Arduino UNO R3
Arduino UNO WiFi Rev2
Purpose: Basic C++ syntax, comments, use Int variables
Try to use comment to document the logical steps.
Consider these questions as learning points:
Why to declare (create) a variable, how?
How to assign value to a variable?
How a name a variable?
/*
Task 1: Use Int variables for blinking LED
*/
// Pin for built-in LED, please assign right value
// 13 for Arduino UNO R3
// 25 for Arduino UNO WiFi Rev2
int led_pin = 13;
// Sleep time in ms
int sleep_ms = 500;
void setup() {
// Set pin mode as OUTPUT for LED
pinMode(led_pin, OUTPUT);
}
void loop() {
// Turn on LED
digitalWrite(led_pin, HIGH);
delay(sleep_ms);
// Turn off LED
digitalWrite(led_pin, LOW);
delay(sleep_ms);
}
Purpose: Basic C++ syntax, comments, serial communication setup and sending
Official document:
/*
Task 2: Add Serial Communication Sending
*/
// Pin for built-in LED, please assign right value
// 13 for Arduino UNO R3
// 25 for Arduino UNO WiFi Rev2
int led_pin = 13;
// Sleep time in ms
int sleep_ms = 500;
void setup() {
// Initialize serial communication with baud rate (speed)
Serial.begin(9600);
// Set pin mode as OUTPUT for LED
pinMode(led_pin, OUTPUT);
}
void loop() {
// Turn on LED, after printing a message
Serial.print("Turning on...");
digitalWrite(led_pin, HIGH);
delay(sleep_ms);
// Turn off LED, after printing a message
Serial.println("Turning off");
digitalWrite(led_pin, LOW);
delay(sleep_ms);
}
Purpose: Basic C++ syntax, comments, if-else, char variable, serial commnucation receiving
/*
Task 3: Add Serial Communication Receiving, Make an Echo
*/
// A char variable to store received character
char received_char;
void setup() {
// Initialize serial communication with baud rate (speed)
Serial.begin(9600);
}
void loop() {
// Check if serial buffer has received any chars
if (Serial.available() > 0) {
// Read in the char and store to variable
received_char = Serial.read();
// Echo a message back to the sender
Serial.print("Arduino has received:");
Serial.println(received_char);
}
}
Purpose: C++ syntax, comments, if-else, int and char varialbles, serial commnucation sending & receiving
// Tips: Skip the useless line feeding char.
// Method 1:
if (isspace(received_char)) {}
// Method 2:
if (received_char != '\n') {}
// Method 3:
if (int(received_char) == 10) {}
// Tips: Logical OR
if (received_char == 'H' || received_char == 'h') {}
/*
Task 4: Use Received Char to On/off LED
Challanges:
Use 'H' or 'h' to turn on LED, use 'L' or 'l' to turn off LED.
Skip the useless line feeding char, don't handle it (a little about ASCII)
*/
// Pin for built-in LED, please assign right value
// 13 for Arduino UNO R3
// 25 for Arduino UNO WiFi Rev2
int led_pin = 13;
// A char variable to store received character
char received_char;
void setup() {
// Initialize serial communication with baud rate (speed)
Serial.begin(9600);
// Set pin mode as OUTPUT for LED
pinMode(led_pin, OUTPUT);
}
void loop() {
// Check if serial buffer has received any chars
if (Serial.available() > 0) {
// Read in the char and store to variable
received_char = Serial.read();
if (received_char == 'H') {
Serial.println("Turning on...");
digitalWrite(led_pin, HIGH);
}
else if (received_char == 'L') {
Serial.println("Turning off...");
digitalWrite(led_pin, LOW);
}
else {
Serial.print("Unknown command:");
Serial.println(received_char);
}
}
}
Click for the full codes with challenges
/*
Task 4: Use Received Char to On/off LED
Challanges:
Use 'H' or 'h' to turn on LED, use 'L' or 'l' to turn off LED
Skip the useless line feeding char, don't handle it (a little about ASCII)
*/
// Pin for built-in LED, please assign right value
// 13 for Arduino UNO R3
// 25 for Arduino UNO WiFi Rev2
int led_pin = 13;
// A char variable to store received character
char received_char;
void setup() {
// Initialize serial communication with baud rate (speed)
Serial.begin(9600);
// Set pin mode as OUTPUT for LED
pinMode(led_pin, OUTPUT);
}
void loop() {
// Check if serial buffer has received any chars
if (Serial.available() > 0) {
// Read in the char and store to variable
received_char = Serial.read();
// Skip the useless line feeding char.
// Also can use isspace(received_char)
if (received_char != '\n') {
// Echo a message back to the sender
Serial.print("Arduino has received:");
Serial.println(received_char);
if (received_char == 'H' || received_char == 'h') {
Serial.println("Turning on...");
digitalWrite(led_pin, HIGH);
}
else if (received_char == 'L' || received_char == 'l') {
Serial.println("Turning off...");
digitalWrite(led_pin, LOW);
}
else {
Serial.print("Unknown command:");
Serial.println(received_char);
}
}
}
}
Purpose: Basic C++ syntax, comments, if-else, serial communication sending & receiving string, string handling by trim, case converting
// Tips: Use those methods to compare string concerning case
// Method 1:
if (received_string.equalsIgnoreCase("high")) {}
// Method 2:
received_string.toUpperCase();
if (received_string == "HIGH") {}
// Method 3:
received_string.toLowerCase();
if (received_string == "high") {}
Question:
Is it reliable to readString() when available() > 0? Why?
/*
Task 5: Use Received String to On/off LED
Challenges:
Use "high"(ignore case) to turn on LED, use "low" (ignore case) to turn off LED
*/
// Pin for built-in LED, please assign right value
// 13 for Arduino UNO R3
// 25 for Arduino UNO WiFi Rev2
int led_pin = 13;
// A String variable to store received string
String received_string;
void setup() {
// Initialize serial communication with baud rate (speed)
Serial.begin(9600);
// Set pin mode as OUTPUT for LED
pinMode(led_pin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
// Read the string ending with a line feeding char
received_string = Serial.readString();
// Trim the trailing "empty" char, i.e. '\n'
received_string.trim();
// Echo a message back to the sender
Serial.print("Arduino has received:");
Serial.println(received_string);
if (received_string == "High") {
Serial.println("Turning on...");
digitalWrite(led_pin, HIGH);
}
else if (received_string == "Low") {
Serial.println("Turning off...");
digitalWrite(led_pin, LOW);
}
else {
Serial.print("Unknown command:");
Serial.println(received_string);
}
}
}
Purpose: Enhance basic knowledge understanding, reflect and summarize what we have learnt
Go to this test: https://forms.gle/PzyxKA62y7GbUKKM6
No time limit, but should be possibly done within 15 minutes
Expectation is at least 60% correctness
Follow the tutor to take 5-10 minutes to review the questions and answers, correct some possible misunderstood issues or faults
Take 1-2 minutes to explore anything on the tools and the making flow, with curiosity, no matter how much prior knowledge you have, you should make
At least 1 interesting finding
At least 1 curious question
Used by LP2508: Starting Experience of C++ with Arduino, 25w08 for the continuous 3 sessions