Else If not getting implemented

Hi Folks.

I am having trouble with the code below. I am using the keyboard characters ‘c’ and ‘d’ to turn on and off a transistor switch. The arduino then reads the voltage of a part of my circuit. When I press ‘c’ everything works fine which is the first part of the if statement. When I press ‘d’ nothing happens so the else if part is not getting implemented. Why is this?

I did try replacing the “else if (Serial.read() == ‘d’)” with a standard else statement. This did work, however I will need to use ‘d’ and I will be incorporating other characters later and will have multiple else if’s. Any ideas on how to solve this?

Mark.

int therm1 = 13;
int state;
int sensorPin = A0;
int sensorValue;

// the setup function runs once when you press reset or power the board
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
digitalWrite(13, LOW);
}

// the loop function runs over and over again forever
void loop() {

if(Serial.available() > 0) {
if(Serial.read() == ‘c’) {
digitalWrite(therm1, HIGH);
for(int i =0; i<5; i++){
sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (3.3 / 1023.0);
Serial.println(voltage);
}

}
else if (Serial.read() == ‘d’){
digitalWrite(therm1, LOW);

for(int j =0; j<5; j++){
sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (3.3 / 1023.0);
Serial.println(voltage);
}
}

while (Serial.available() > 0){
Serial.read();
}
}
}

you need to store the serial.read() then check it, or you lose the character after the first "if"

see the example here: http://arduino.cc/en/Serial/read

and post your code in code blocks, like this (also an example)

int incomingByte = 0;   // for incoming serial data

void setup() {
        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();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
                if (incomingByte == 'c')......
                else if (incomingByte == 'd')....
        }
}

Thanks a mill. Works perfect now.