Serial port questions

HI there

I’m having some slight issues with the serial port. I’m not sure if i’m doing this correctly or not but i’m trying to send data to the serial port to switch an led on or off. for example if i send ‘1’ it should turn on and ‘2’ should turn it off. when i send any data to it it runs through the whole program and i’m not sure what is wrong with it. Can someone point me in the right direction with it?

int incomingByte = 0; // for incoming serial data
int ledPin = 13;

void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() == 0) {
// read the incoming byte:
incomingByte = Serial.read();
digitalWrite(ledPin, HIGH);
// say what you got:
Serial.println(“led = on”);
// Serial.println(incomingByte, DEC);
}

else if(Serial.available() == 1){
incomingByte = Serial.read();
digitalWrite(ledPin, LOW);
Serial.println(“led = off”);
delay(5000);
}
}

try a switch/case statement.

This tutorial should point you in the right direction...

http://arduino.cc/en/Tutorial/SwitchCase2

:)

thank you very much, i think i may of figured it out anyways.

Your logic is wrong:

if (Serial.available() == 0) {

“available” tells you how many bytes are available in the buffer, not their values.

You may also need to test the character you read against its ASCII value, not one or zero.

thank you

I've managed to figure it out but for thanks for replying