hi,
I am trying to use serial.read() function. But when I write "On", it goes to else statment! I know that Serial.read() function can only read 1 byte, but I created an array to store the byte.
Here's the code:
const int max_char = 12;
const int led = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()){
static char message[max_char];
static int pos = 0;
char input = Serial.read();
if(input != '\n'){
message[pos] = input;
pos++;
}
else{
message[pos] = '\0';
Serial.println(String("Your message: ") + message);
pos = 0;
}
if(message == "on"){
digitalWrite(led,HIGH);
}
else if(message == "off"){
digitalWrite(led,LOW);
}
else{
Serial.println("only on/off !");
}
}
}