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.