arduino serial HELP ME

94 views Asked by At

I am trying to get an LED to light up when a certain message comes through on the serial port but nothing is working! please help me!!

int awsState = "AWS:0";   // for incoming serial data

 void setup() {
    Serial.begin(9600);   
    pinMode(13, OUTPUT);
}

void loop() {

    // send data only when you receive data:
    if (Serial.available() > 0) {
            // read the incoming byte:
            awsState = Serial.read();

            // say what you got:
            Serial.print(incomingByte);
            Serial.println("Good");
            if (awsState == "AWS:1"){

              digitalWrite(13, HIGH);
            }
    else if (awsState == "AWS:0"){
      digitalWrite(13, LOW);
    }
    }

}
1

There are 1 answers

2
J. Piquard On

Here is a way to read a command from the Serial link.

Step 1 - declare a String to store input data as global variable

String awsState;

Instead of:

int awsState = "AWS:0"; 

Step 2 - initialize the String in the setup()

void setup() {
    Serial.begin(9600);   
    pinMode(13, OUTPUT);
    awsState= "";
}

Step 3 - extract all received character, display them and process the command

void loop() {
    while (Serial.available() > 0) {
        // read all received characters
        char rec =  Serial.read();
        // until <return> character
        if (rec != '\n') {
            awsState += rec; 
        }
        else {
            Serial.print(awsState);
            Serial.println("Good");
            if (awsState == "AWS:1"){
              digitalWrite(13, HIGH);
            }
            else if (awsState == "AWS:0"){
              digitalWrite(13, LOW);
            }
            // reset the command to wait the next one
            awsState= "";
        }
    }
}