A small sketch to show how to implement a command menu using the Serial Monitor of the Arduino IDE. In this example the switch statement is used to identify the received command and execute the corresponding action.
— Command list: —
? -> Print this HELP
a -> LED On “activate”
d -> LED Off “deactivate”
s -> LED “status”
The SKETCH:
/*Serial_LED_02.ino Arduining 4 May 2015 Controlling the LED in pin 13 with the Serial Monitor. --- Command list: --- ? -> Print this HELP a -> LED On "activate" d -> LED Off "deactivate" s -> LED "status" Example using the switch statement. */ #define LED 13 // Pin 13 is connected to the LED char rxChar= 0; // RXcHAR holds the received command. //=== function to print the command list: =========================== void printHelp(void){ Serial.println("--- Command list: ---"); Serial.println("? -> Print this HELP"); Serial.println("a -> LED On \"activate\""); Serial.println("d -> LED Off \"deactivate\""); Serial.println("s -> LED \"status\""); } //---------------- setup --------------------------------------------- void setup(){ Serial.begin(9600); // Open serial port (9600 bauds). pinMode(LED, OUTPUT); // Sets pin 13 as OUTPUT. Serial.flush(); // Clear receive buffer. printHelp(); // Print the command list. } //--------------- loop ----------------------------------------------- void loop(){ if (Serial.available() >0){ // Check receive buffer. rxChar = Serial.read(); // Save character received. Serial.flush(); // Clear receive buffer. switch (rxChar) { case 'a': case 'A': // If received 'a' or 'A': if (digitalRead(LED) == LOW){ // If LED is Off: digitalWrite(LED,HIGH); // Turn On the LED. Serial.println("LED turned On"); } else Serial.println("LED already On!"); break; case 'd': case 'D': // If received 'd' or 'D': if (digitalRead(LED) == HIGH){ // If LED is On: digitalWrite(LED,LOW); // Turn Off the LED. Serial.println("LED turned Off"); } else Serial.println("LED already Off!"); break; case 's': case 'S': // If received 's' or 'S': if (digitalRead(LED) == HIGH) // Read LED status. Serial.println("LED status: On"); else Serial.println("LED status: Off"); break; case '?': // If received a ?: printHelp(); // print the command list. break; default: Serial.print("'"); Serial.print((char)rxChar); Serial.println("' is not a command!"); } } } // End of the Sketch.
Great! Thank you for sharing this with us. Descriptions like your help newbies like me to get a better understanding of arduining.
Two things I noticed: you mixed ab the keystrokes “d”, “D”, “s”, “S” in your comments, I think.
Greetings from Germany. Keep up.
Thanks nazimkenan, corrected.
Hello,
i have a question. Is it possible to change the input into a Byte command?
Hello, is it possible to change the “case” into a byte?
hey
great work!!!!
i want to ask is there any way that if i press a and “LED turned on” is written for some time and than only min menu appears ……. i mean it returns to main menu???
I must say you have high quality content here. Your posts
should go viral. You need initial boost only. How to get massive traffic?
Search for; Murgrabia’s tools go viral
I got this to work but I think because your keyboard language is German or something it doesn’t compile properly. So I re-written it here:
/*Serial_LED_02.ino casArduining 4 May 2015
Controlling the LED in pin 13 with the Serial Monitor.
— Command list: —
? -> Print this HELP
a -> LED On “activate”
d -> LED Off “deactivate”
s -> LED “status”
Example using the switch statement.
*/
const int LED = 13; // Pin 13 is connected to the LED
char rxChar = 0; // RXcHAR holds the received command.
//=== function to print the command list: ===========================
void printHelp(void){
Serial.println(“— Command list: —“);
Serial.println(“? -> Print this HELP”);
Serial.println(“a -> LED On \”activate\””);
Serial.println(“d -> LED Off \”deactivate\””);
Serial.println(“s -> LED \”status\””);
}
//—————- setup ———————————————
void setup(){
Serial.begin(9600); // Open serial port (9600 bauds).
pinMode(LED, OUTPUT); // Sets pin 13 as OUTPUT.
Serial.flush(); // Clear receive buffer.
printHelp();// Print the command list.
}
//————— loop ———————————————–
void loop(){
if (Serial.available() > 0){// Check receive buffer.
rxChar = Serial.read();// Save character received.
Serial.flush(); // Clear receive buffer.
switch(rxChar){
case ‘a’:
case ‘A’: // If received ‘a’ or ‘A’:
if (digitalRead(LED) == LOW){ // If LED is Off:
digitalWrite(LED,HIGH); // Turn On the LED.
Serial.println(“LED turned On”);
}
else {
Serial.println(“LED already On!”);
}
break;
case ‘d’:
case ‘D’: // If received ‘d’ or ‘D’:
if (digitalRead(LED) == HIGH){ // If LED is On:
digitalWrite(LED,LOW); // Turn Off the LED.
Serial.println(“LED turned Off”);
}
else {
Serial.println(“LED already Off!”);
}
break;
case ‘s’:
case ‘S’: // If received ‘s’ or ‘S’:
if (digitalRead(LED) == HIGH) {// Read LED status.
Serial.println(“LED status: On”);
}
else {
Serial.println(“LED status: Off”);
}
break;
case ‘?’: // If received a ?:
printHelp(); // print the command list.
break;
default:
Serial.print(“‘”);
Serial.print((char)rxChar);
Serial.println(“‘ is not a command!”);
break;
}
}
}
// End of the Sketch.
I notice if you type in multiple characters it process them one by one. Is there a way that I would receive a string and process based on that.
For example I want to tell a particular Arduino with unique ID (UID) to do something. The format would be something like [!][Function 1-char][UID 4-bytes][Result #1 2-bytes][Result #2 2-bytes].
For example: [!][F][0000853716][006326][0003625]
I tried to use your code and Concat but either will list the text but I’m not sure how to action it.
Anybody have a clue?
As far as I know Serial.flush() doesn’t clear receive buffer any more in recent Arduino cores, but I don’t remember when this was removed.
Instead it waits until all characters in output buffer are sent.