I am using this code to turn an led on and off through serial communication:
// turn LED on and off through serial comunication
int myData = 0;
int const ledPin = 13;
void setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
void loop()
{
if(Serial.available()>0)
{
myData=Serial.read();
if(myData == '1')
{
digitalWrite(ledPin,HIGH);
Serial.println("LED on");
}
if(myData == '2')
{
digitalWrite(ledPin,LOW);
Serial.println("LED off");
}
else{}
}
}
as you can see i have to enter 1 or 0 to turn my led on and off, but i want to be able to write something in the serial monitor like "LED On" or "LED Off". How do I do this?
Something like this:
// turn LED on and off through serial comunication
int myData = 0;
int const ledPin = 13;
#define CHAR_MAX 20
char myCommand[CHAR_MAX];
char charCount=0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
void loop()
{
if(Serial.available()>0)
{
myData=Serial.read();
if (myData != 0x0D && charCount < CHAR_MAX) {
myCommand[charCount] = myData;
charCount++;
myCommand[charCount] = '\0';
}
else if (myData == 0x0D) {
charCount = 0;
if(strcmp(myCommand, "LED on") == 0) {
digitalWrite(ledPin,HIGH);
Serial.println("LED on");
}
else if(strcmp(myCommand, "LED off") == 0)
{
digitalWrite(ledPin,LOW);
Serial.println("LED off");
}
}
}
}
The character 0x0D is the code for the "enter" (not exactly but part of it, is the return to the begin of the line, after a enter) search for ASCII table. The character '\0' marks the end of the string (is the NULL character).
To test the sketch use the serial monitor and at the bottom of the window select "Carriage return";
EDIT: I don't know if it works. I found 2 errors after I post it.
A couple of ideas:
I think the code in your for loop would be easier to read if you used a switch:
if(Serial.available()>0)
{
myData=Serial.read();
switch (myData) {
case '1':
digitalWrite(ledPin,HIGH);
Serial.println("LED on");
break;
case '2'
digitalWrite(ledPin,LOW);
Serial.println("LED off");
break;
default:
Serial.println("Shouldn't be here...");
break;
}
}
Of course, that's personal preference. Second, use toupper() to convert all the characters to upper case, then do a substring search for " ON" or " OFF", and act accordingly. That way, the user has some flexibility in the way they pass the input to your program. That is, "TURN LED ON", "LED ON", "POWER LED ON", etc. all produce the desired result. Notice the leading space so some words with "ON" in them don't trigger a false response.
Very basic serial code.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}
There are lots of reasons why you might need to send several characters to an Arduino and the code people have suggested is suitable for receiving and comparing it.
But I can think of NO reason to send anything other than 1 or 0 to turn an LED on or off. The additional characters don't convey any additional information and just waste both computers' time.
...R