user3513782
user3513782

Reputation: 83

Arduino serial read and check

I am trying to learn some things about arduino serial reading from a bluetooth device. This is the code I found everywhere:

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);
        }
}

When I send the word "word" from my mobile bluetooth I get 4 lines:

I received: w

I received: o

I received: r

I received: d

which is fine... But here 's my question:

I want to check the received characters as soon as they come into the serial input, so if one of them is the character "r", I would like an extra line to be printed in the Serial Monitor, something like: "wow, that was an r!"

So I add an if statement after the println(incomingByte) and my code is now like that:

Serial.print("I received: ");
Serial.println(incomingByte);
if (incomingByte == "r") {
    Serial.println("wow, that was an r!");
}

That code never works, it 's like not having an "r" at all. Could someone explain to me? Thanks

Upvotes: 0

Views: 729

Answers (1)

Emperor Transisthor
Emperor Transisthor

Reputation: 135

I think it is due to the usage of "r" instead of 'r'. The difference is that in first case you have string, which is char[], and in the second you have single char.

Upvotes: 2

Related Questions