Hi All,
I've just started playing with Arduino and I wrote a simple command line interface (CLI) sketch for testing a pin in an interactive manner. I know that shells exist for arduino, etc... this is just a demo ;)
This lets you turn on, off, and blink pin 13 from a terminal program (hyperterm, PuTTY, etc.)
This isn't meant to an application in an of itself, but more of a stepping stone. Just an example of how to use a serial connection and turn on, off, and blink an LED.
More info (and a prettier presentation of the code) can be found at this short article I wrote: http://digital-salvage.net/?p=36
I'm currently playing with an LCD and a GPS. Will post code examples of that as well once I get it finished, if folks are interested.
Here is the code:
/* simplecli version 001
* -----------------
* C. Cosentino 2009 - http://digital-salvage.net/?p=36
* 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.
*/
#define ledPin 13 // define a pin for the LED
#define MCHARS 8 // max characters to accept
#define ENTER 13 // byte value of the Enter key (LF)
char inStr[MCHARS]; // the input string
int inByte = 0; // byte being read in
int strCount = 0; // counter for the string
int blink = 0; // toggle for blink routine
int value = LOW; // Initial Pin value of LED for blink
void setup() {
pinMode(ledPin, OUTPUT); // sets ledPin as an OUTPUT pin
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.print(">"); // print prompt when serial is ready
}
void loop() {
if(Serial.available()) { // if bytes are available to be read
inByte = Serial.read(); // read the byte
Serial.print(char(inByte)); // echo byte entered to terminal as a char
if(inByte != ENTER) { // if the byte is not a NEWLINE
inStr[strCount] = char(inByte); // add the byte to the "String" array
strCount++; // increase the item string count
}
}
if(inByte == ENTER || strCount >= MCHARS) { // if enter was pressed or max chars reached
Serial.flush(); // flush the serial data (overkill?)
Serial.println(""); // print a newline
if (strcmp(inStr, "blink") == 0){ // string compare, does entered text == blink?
if (blink == 1) { // if so, if blink is 1, pin was already blinking
Serial.println("LED Off"); // print to the terminal
digitalWrite(ledPin, LOW); // make certain LED is in LOW state
blink = 0; // toggle blink variable
} else { // if it was not 1...
Serial.println("LED Blinking"); // print to the terminal
blink = 1; // ... then set to 1
}
} else if (strcmp(inStr, "on") == 0){ // string compare, does entered text == on?
digitalWrite(ledPin, HIGH); // if so, then turn on ledPin
Serial.println("LED On"); // print to the terminal
blink = 0;
} else if (strcmp(inStr, "off") == 0){ // string compare, does entered text == off?
digitalWrite(ledPin, LOW); // if so, then turn off ledPin
Serial.println("LED Off"); // print to the terminal
blink = 0;
} else { // if the input text doesn't match any defined above
Serial.println("Invalid."); // echo back to the terminal
}
strCount = 0; // get ready for new input... reset strCount
inByte = 0; // reset the inByte variable
for(int i = 0; inStr[i] != '\0'; i++) { // while the string does not have null
inStr[i] = '\0'; // fill it with null to erase it
}
Serial.println(""); // print a newline
Serial.print(">"); // print the prompt
}
if(blink == 1) { // if blink
delay(100); // wait for it! (you could use millis if/then here)
if(value == LOW) { // if pin is LOW
value = HIGH; // set it for HIGH
} else { // otherwise it is HIGH
value = LOW; // so set it fot low
}
digitalWrite(ledPin, value); // send the command to the pin
}
}
Edit: Any comments or suggests are much appreciated. I have not touched C in almost 15 years :)