Challenge - 5 Problems
Bluetooth Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Bluetooth command receiver sketch?
Consider this Arduino code that reads characters from a Bluetooth serial connection and prints them to the Serial Monitor. What will be printed if the Bluetooth device sends the characters 'HELLO'?
Arduino
void setup() {
Serial.begin(9600); // Serial monitor
Serial1.begin(9600); // Bluetooth module
}
void loop() {
if (Serial1.available()) {
char c = Serial1.read();
Serial.print(c);
}
}Attempts:
2 left
💡 Hint
Think about what Serial1.read() returns and how Serial.print() works.
✗ Incorrect
The code reads each character sent over Bluetooth (Serial1) and prints it exactly as received to the Serial Monitor. Since the device sends 'HELLO', the output is 'HELLO'.
🧠 Conceptual
intermediate1:30remaining
Which Arduino function checks if Bluetooth data is ready to read?
You want to read incoming data from a Bluetooth module connected to Serial1. Which function tells you if there is data waiting to be read?
Attempts:
2 left
💡 Hint
This function returns how many bytes are ready to read.
✗ Incorrect
Serial1.available() returns the number of bytes available to read from the Bluetooth serial buffer. It is used to check if data has arrived.
🔧 Debug
advanced2:30remaining
Why does this Bluetooth command receiver code not print anything?
Look at this Arduino code snippet. It should print characters received from Bluetooth on Serial1 to the Serial Monitor. But nothing appears on the Serial Monitor. What is the problem?
Arduino
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
if (Serial1.available() > 0) {
char c = Serial1.read();
Serial.print(c);
}
}Attempts:
2 left
💡 Hint
Check how functions are called in Arduino code.
✗ Incorrect
Serial1.available is a function and must be called with parentheses: Serial1.available(). Without parentheses, it refers to the function itself, which is always true, but does not return the number of bytes available. So the if condition never works as intended.
📝 Syntax
advanced2:30remaining
Which option correctly reads a full command string from Bluetooth until newline?
You want to read a full command sent over Bluetooth ending with a newline character '\n'. Which code snippet correctly reads characters into a string until '\n' is received?
Arduino
String command = ""; void loop() { while (Serial1.available()) { char c = Serial1.read(); if (c == '\n') { // command complete Serial.println(command); command = ""; } else { command += c; } } }
Attempts:
2 left
💡 Hint
Look for the code that resets the command string after printing.
✗ Incorrect
Option D correctly accumulates characters until '\n' is found, then prints the full command and resets the string for the next command.
🚀 Application
expert3:00remaining
How many commands will be stored in the array after this Bluetooth input?
This Arduino code reads commands separated by commas from Bluetooth and stores them in an array. If the Bluetooth device sends 'ON,OFF,TOGGLE,STATUS', how many commands will be stored?
Arduino
String commands[5]; int index = 0; String current = ""; void loop() { while (Serial1.available()) { char c = Serial1.read(); if (c == ',') { commands[index++] = current; current = ""; } else { current += c; } } // After input ends, store last command if (current.length() > 0) { commands[index++] = current; current = ""; } }
Attempts:
2 left
💡 Hint
Count the number of commas and remember the last command after the last comma.
✗ Incorrect
The input has 3 commas, so 4 commands: 'ON', 'OFF', 'TOGGLE', 'STATUS'. The code stores each command when a comma is found and also stores the last command after input ends.