A New simple Command Line Interface (CLI) to Arduino

Last Updated: April 7th, 2013 by Chris
Filed under: Physical Computing
Tags:

I’ve updated my simple CLI to take advantage of the native String class that is now available in Arduino. This version also uses the serialEvent() loop.

This version also captures backspaces correctly.

Code is after the jump!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* simplecli version 002
 * -----------------
 * C. Cosentino 2013 - http://digital-salvage.net/
 *  This software is licensed under the CC-GNU GPL version 2.0
 *     See: http://creativecommons.org/licenses/GPL/2.0/
 *     
 *  Simple CLI example that demonstrates how to use serial to perform
 *  interactive commands (turning a pin on or off) via a terminal application.
 *  This version uses strings (instead of a char array) and uses the
 *  serialEvent() loop available in new versions of the Arduino IDE
 */
 
boolean stringComplete = false;
String inputStr = "";
int led = 13;
int ledmode = 1;
 
void setup()
{
    pinMode(13, OUTPUT);
    Serial.begin(9600);
    inputStr.reserve(200); //reserve some memory for input string
    print_menu();
}
 
void print_menu() {
  Serial.println();
  Serial.println("Enter number or type command:");
  Serial.println("1. off");
  Serial.println("2. on");
  Serial.println("3. blink");     
  Serial.print("> ");   
}
 
void get_input()
{
        char inChar = Serial.read();
        if(inChar == 8 || inChar == 127) { //backspace or delete
            int len = inputStr.length();
            inputStr = inputStr.substring(0, len -1);
        } else {
            inputStr += (char)inChar;
        }
        if(inChar == '\n') {
            inputStr.trim(); //clean up the string
            stringComplete = true;
        }
}
 
void loop()
{
    if(ledmode == 1) {
       digitalWrite(led, LOW);
    } else if (ledmode == 2) {
       digitalWrite(led, HIGH);
    } else if (ledmode == 3) {
        digitalWrite(led, HIGH); 
        delay(150); 
        digitalWrite(led, LOW); 
        delay(150);     
    }
    if(stringComplete) {
        if(inputStr == "1" || inputStr == "off") {
            Serial.println("LED OFF");
            ledmode = 1;
        } else if (inputStr == "2" || inputStr == "on") {
            Serial.println("LED ON");   
            ledmode = 2;
        } else if (inputStr == "3" || inputStr == "blink") {
 
            ledmode = 3;
        } else {
            Serial.println("Command \"" + inputStr + "\" not valid!"); 
        }
        inputStr = "";
        stringComplete = false;
        print_menu();
    }
}
 
void serialEvent() {
    while(Serial.available()) { // Make sure there is a key press
        get_input();
    }
}

Leave a Reply

Login with one of the following services:


(you must login to post comments. Otherwise we get lots of Spammers!