Serial Control?

I'm trying to use the following code to get two LEDs to light up when the serial code for "f" and "r" are sent, but they don't seem to be responding. Any idea why? Feel free to ignore the majority of the comments, the program is for a different project and this is just testing.

int enablePin = 3; // Enable pin for both coils int coilA1Pin = 4; // Control pin for coil A int coilA2Pin = 5; // Control pin 2 for coil A int coilB1Pin = 6; // Control pin for coil B int coilB2Pin = 7; // Control pin 2 for coil B int IncByte = 0;

void setup() // run once, when the sketch starts { pinMode(enablePin, OUTPUT); // sets the digital pin as output pinMode(coilA1Pin, OUTPUT); // sets the digital pin as output pinMode(coilA2Pin, OUTPUT); // sets the digital pin as output pinMode(coilB1Pin, OUTPUT); // sets the digital pin as output pinMode(coilB2Pin, OUTPUT); // sets the digital pin as output Serial.begin(9600); // begins serial transfer digitalWrite(coilA1Pin, HIGH); digitalWrite(coilB1Pin, HIGH); delay(1000); digitalWrite(coilA1Pin, LOW); digitalWrite(coilB1Pin, LOW);

}

void loop() // run over and over again { // send data only when you receive data: if (Serial.available() > 0) { // read the incoming byte: IncByte = Serial.read();

// say what you got: Serial.print("I received: "); Serial.println(IncByte, DEC); }

if (digitalRead(IncByte) == 102) { digitalWrite(coilA1Pin, HIGH); // Sets coil A to positive delay(2000); } else { digitalWrite(coilA1Pin, LOW); // disables coils if button is not active }

if (digitalRead(IncByte) == 114) // check if the input is HIGH { digitalWrite(coilB1Pin, HIGH); // Enables both coils delay(2000); } else { digitalWrite(coilB1Pin, LOW); // disables coils if button is not active } }

Shouldn’t:

if (digitalRead(IncByte) == 102)
and
if (digitalRead(IncByte) == 114)

Be:

if (IncByte == 102)
if (IncByte == 114)

digitalRead is a function that reads a single input pin and returns just a true false value. http://arduino.cc/en/Reference/DigitalRead
You have already received the serial input character from the PC and have it stored in the IncByte variable so that is what you should test for in your if statement.

Lefty

You can condense a lot of that...

digitalWrite(coilA1Pin, (IncByte==102)); digitalWrite(coilB1Pin, (IncByte==114)); delay(2000);

you could also do:

if (IncByte == ‘f’) {}

Let the compiler do the conversion for you.