Consider this Arduino code snippet that reads a character from the computer and responds:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char cmd = Serial.read();
if (cmd == 'A') {
Serial.println("Command A received");
} else {
Serial.println("Unknown command");
}
}
}If the user sends the character 'A' from the computer, what will the Arduino print?
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char cmd = Serial.read();
if (cmd == 'A') {
Serial.println("Command A received");
} else {
Serial.println("Unknown command");
}
}
}Check what the code prints when the character matches 'A'.
The code reads the character sent from the computer. If it is 'A', it prints "Command A received". Otherwise, it prints "Unknown command".
Given this Arduino code:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char cmd = Serial.read();
Serial.print("Received: ");
Serial.println(cmd);
} else {
Serial.println("Waiting for command...");
}
delay(1000);
}If no data is sent from the computer, what will the Arduino print repeatedly?
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char cmd = Serial.read();
Serial.print("Received: ");
Serial.println(cmd);
} else {
Serial.println("Waiting for command...");
}
delay(1000);
}Look at what the code does when Serial.available() is zero.
When no data is available, the else branch prints "Waiting for command..." every second.
Analyze this Arduino code:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char cmd = Serial.read();
switch(cmd) {
case 'A':
Serial.println("Alpha");
break;
case 'B':
Serial.println("Bravo");
break;
case 'C':
Serial.println("Charlie");
break;
default:
Serial.println("Unknown");
}
}
}If the user sends the characters 'B' then 'C' in sequence, what will be printed?
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char cmd = Serial.read();
switch(cmd) {
case 'A':
Serial.println("Alpha");
break;
case 'B':
Serial.println("Bravo");
break;
case 'C':
Serial.println("Charlie");
break;
default:
Serial.println("Unknown");
}
}
}Check the switch cases for 'B' and 'C'.
The switch prints "Bravo" for 'B' and "Charlie" for 'C'. So the output lines are "Bravo" then "Charlie".
Look at this Arduino code snippet:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int cmd = Serial.read();
if (cmd == 'A') {
Serial.println("Got A");
}
}
}What is the type of the variable cmd and what problem might it cause?
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int cmd = Serial.read();
if (cmd == 'A') {
Serial.println("Got A");
}
}
}Consider the return type of Serial.read() and how it compares to char.
Serial.read() returns an int to allow -1 if no data. Using int for cmd is okay, but comparing to char can cause subtle bugs if not careful. Usually, cmd should be char for clarity.
You want to receive a full command string (like "START" or "STOP") from the computer via Serial and act only after the full command is received. Which approach is best?
Think about how to know when a full command is complete.
Reading characters one by one and buffering until a newline is received ensures the full command is captured before processing. Other options risk partial reads or blocking.
