Help with Relay coding

Hi

I am completely new to the arduino and I'm currently writing a relay code that will ask for what pin I want set to high or low and then input the high or low. However, the program is not running that way. None of the pins go high or low. Here is the code:

void setup() { Serial.begin(9600); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); }

void loop() { if (Serial.available() > 0) { Serial.print("Enter Switch Letter:"); char id = Serial.read(); switch(id) { case 'X': { Serial.print("Enter Value for X:"); char incoming = Serial.read(); if (incoming == 'a') { digitalWrite(10, HIGH); Serial.println("On"); } else if (incoming == 'b') { digitalWrite(10, LOW); Serial.println("Off"); } } break; case 'Y': { Serial.print("Enter Value for Y:"); char incoming = Serial.read();

if (incoming == 'a') { digitalWrite(11, HIGH); Serial.println("On"); } else if (incoming == 'b') { digitalWrite(11, LOW); Serial.println("Off"); } } break; case 'Z': { Serial.print("Enter Value for Z:");

char incoming = Serial.read();

if (incoming == 'a') { digitalWrite(12, HIGH); Serial.println("On"); } else if (incoming == 'b') { digitalWrite(12, LOW); Serial.println("Off"); } } break; case '4':

break; case '5':

break; case '6':

break; default: break; }

} }

Any advise on what I can do?

Thanks

You need two characters so you should put another Serial.available() before the second read or test for Serial.available()>1 the first time.

If there is data to be read from the serial port, you post a prompt. Otherwise, you do not. Isn’t that backwards?

Posting a prompt does not cause the Arduino to wait for a reply, which your code expects to have happen.

The thing to keep in mind is that serial communication, by definition is asynchronous. That means that data can arrive at any time, in any amount. It means that the data is not in direct response to any specific event. In other words, the next bit of data that arrives on the serial port is not necessarily in response to the data that you wrote to the serial port.

Your code is close to what would work, but it would be better if you re-wrote it to take into consideration fdufnews’ and these suggestions.

Many of us could give you working code that does what you want, but, by the time you write and debug code that achieves the results you desire, you will have a much better understanding of serial communications and arrays. This will be very useful to you in the future.

This is not, in any way, meant to imply that you should go away. If we can help further your understanding of arrays and string processing and serial communication, feel free to ask.