Hi
I'm trying to make a program where I can write an "h" to the board and it will turn an LED on for a sec, the program is made from the the example in Serial.read() - Arduino Reference I know the DEC code for the h (104) but the LED turns on with any serial input.. what is done wrong? the format for the Serial.read() is DEC right?
/*
/The programs turns on a LED on pin 9 when a "h" is revived over the serial communication.
/
/Template: http://arduino.cc/en/Serial/Read
/Edited by: Mikkel_Callesen aka Shellduck
///////////////////////////////////////////////////////////////////////////////
/
/NOTE!!
/ The LED turns on with any Serial input :(
/
/ Note to self:
/ Ask arduino forum!
/
*/
int incomingByte = 0; // for incoming serial data
int ledpin=9; // the LED Pin
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(ledpin, OUTPUT); // output mode for ledpin
digitalWrite(ledpin, LOW); // starting ledpin on low (no ligth)
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=104){ // if an "h" (DEC h=104) is resived....
digitalWrite(ledpin, HIGH); // set LED HIGH
delay(1000);
digitalWrite(ledpin, LOW); // set LED LOW after 1000 ms
}
/*
part of the original code
say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
*/
}
}
Also, why are you (trying to) compare to 104 instead of 'h'? It is obvious to anyone looking at a sketch containing this code:
if(incomingByte == 'h')
{
// Turn on the LED
}
else if (incomingByte == 'f')
{
// Turn off the LED
}
what to type to make the LED turn on or off. Without an ASCII table handy, and who caries one around with them, it is impossible to tell in your code what to type to make the LED turn on or off.
The == operator does not work for strings. 'led1' is a multi-byte character constant, but it is not what you can store in a character variable on the Arduino.
Strings, on the other hand, can be used on the Arduino. They are defined by ", not '.
if(strcmp(array, "led1") == 0)
// They match.
Of course, you need to read all the characters, and store them in an array, adding a NULL terminator, before you can use strcmp.
You need a char array that is at least one char longer than the longest string you expect.
As you read in a character, put it into the next free space in the array and set the next character to null (zero, not the character '0').
When you've read your string, use strcmp.
The arduino library has some built in string functions that can make things easier. The below code captures strings sent via the serial monitor and sends them back to the serial monitor.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}