0
0
Arduinoprogramming~10 mins

Receiving commands over Bluetooth in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Receiving commands over Bluetooth
Start Bluetooth Serial
Wait for Data Available?
NoKeep Waiting
Yes
Read Incoming Command
Process Command
Execute Action
Loop Back to Wait
The Arduino waits for Bluetooth data, reads commands when available, processes them, and executes actions in a loop.
Execution Sample
Arduino
void loop() {
  if (Serial.available()) {
    char cmd = Serial.read();
    if (cmd == 'A') {
      digitalWrite(LED_BUILTIN, HIGH);
    }
  }
}
This code reads a character from Bluetooth and turns on the LED if the command is 'A'.
Execution Table
StepSerial.available()Serial.read()CommandAction TakenOutput
10 (No data)--WaitNo change
21 (Data available)'A'ATurn LED ONLED ON
31 (Data available)'B'BNo actionLED stays ON
40 (No data)--WaitLED stays ON
💡 Loop runs continuously; waits when no data, acts when data received.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
cmdundefined'A''B'undefined
LED stateOFFONONON
Key Moments - 2 Insights
Why does the program wait when Serial.available() is 0?
Because no data has arrived yet, so the program skips reading and waits for data, as shown in step 1 and 4 of the execution_table.
What happens if the command is not 'A'?
No action is taken and the LED state remains unchanged, as seen in step 3 where command 'B' does not turn the LED on.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'cmd' at step 3?
A'B'
B'A'
Cundefined
D'C'
💡 Hint
Check the 'Serial.read()' and 'Command' columns at step 3 in the execution_table.
At which step does the LED turn ON according to the execution_table?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action Taken' and 'Output' columns for when LED changes state.
If Serial.available() is always 0, what will happen to the LED state?
ALED will turn ON
BLED will turn OFF
CLED state will never change
DLED will blink
💡 Hint
Refer to variable_tracker and execution_table steps where Serial.available() is 0.
Concept Snapshot
Receiving commands over Bluetooth:
- Use Serial.available() to check data
- Use Serial.read() to get command
- Use if conditions to process commands
- Execute actions like turning LED on/off
- Loop continuously to keep checking
Full Transcript
This example shows how an Arduino receives commands over Bluetooth using the Serial interface. The program continuously checks if data is available with Serial.available(). When data arrives, it reads one character with Serial.read(). If the character matches a command like 'A', it performs an action such as turning on the built-in LED. If no data is available, the program waits and checks again. This loop runs forever, allowing the Arduino to respond to Bluetooth commands anytime they come in.