how to check serialread

Hi all,

I have found the following example on http://arduino.cc/en/Tutorial/PhysicalPixel

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    } 
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}

It worked well, but I do not want a letter

I want it to read words like 'on' and 'off'

so I changed the code to :

const int ledPin = 9; // the pin that the LED is attached to

int incomingByte2,incomingByte; 
int incomingByte3; 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
   pinMode(ledPin2, OUTPUT);
  
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    delay(5);

      incomingByte2 = Serial.read();
  delay(5);
  
   incomingByte3 = Serial.read();
    delay(5);
  
 
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'O'&& incomingByte2 == 'N') {
      digitalWrite(ledPin, HIGH);
    } 
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'O'&& incomingByte2 == 'F'&& incomingByte3 == 'F') {
      digitalWrite(ledPin, LOW);
    }
 
    
  }
}
}

it is working but the led lights up even when I type ONNNNNNNNNNNNNNNNNNNNNNNNNN

the led turns off even when I type OFFKFKFKFKFKFKFKFKFKFFK

It means it only read the first two bytes right?

How should I make it work, please?

Maybe I should store the byte into an array

but how do I compare the stored data in the array to match ' ON' and' OFF'?

Thanks for reading this.

It means it only read the first two bytes right?

You wrote the program.

Why does in matter whether typing "ON" and "ONNNN" do the same thing?

If you want to distinguish between the two possibilities, you need to count the characters received and reject those lines with more than the expected number of characters. Don't forget to check for the newline or return character:

    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') { do something; }

(you may have to set Serial Monitor to send the newline character)

Look at the examples in serial input basics.

If a single byte will do the job why send additional redundant bytes ?

...R

but how do I compare the stored data in the array to match ' ON' and' OFF'?

Very basic serial code that captures incoming characters, then evaluates what is captured.

// 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="";
  } 
}