6
#include <stdio.h>

#define LED 13

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

void loop() {
  int i;
  char command[5];
  for (i = 0; i < 4; i++) {
    command[i] = Serial.read();
  }
  command[4] = '\0';

  Serial.println(command);

  if (strcmp(command, "AAAA") == 0) {
    digitalWrite(LED, HIGH);
    Serial.println("LED13 is ON");
  } else if (strcmp(command, "BBBB") == 0) {
    digitalWrite(LED, LOW);
    Serial.println("LED13 is OFF");
  }
}

I am trying to read a 4 characters long string with Arduino's Serial, and when it is AAAA turn on a LED, when it is BBBB turn off the serial.

However, when I enter "AAAA" it reads "AAAÿ" with lots of "ÿ"'s along the way.

I think I'm reading everything correctly, but it's not working so well, any idea of what I'm doing wrong?

2
  • Confirm that your baudrate, stop bits, flow control, and parity are identical on both ends. Even if you "know it's true," take the 3 minutes and verify it. Save yourself hours. Commented May 4, 2012 at 14:50
  • Whats your Serial.begin() code? Commented May 4, 2012 at 14:52

4 Answers 4

10
String txtMsg = "";  
char s;

void loop() {
    while (serial.available() > 0) {
        s=(char)serial.read();
        if (s == '\n') {
            if(txtMsg=="HIGH") {  digitalWrite(13, HIGH);  }
            if(txtMsg=="LOW")  {  digitalWrite(13, LOW);   }
            // Serial.println(txtMsg); 
            txtMsg = "";  
        } else {  
            txtMsg +=s; 
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
#define numberOfBytes 4
char command[numberOfBytes];

    void serialRX() {
      while (Serial.available() > numberOfBytes) {
        if (Serial.read() == 0x00) { //send a 0 before your string as a start byte
          for (byte i=0; i<numberOfBytes; i++)
            command[i] = Serial.read();
        }
      }
    }

Comments

1

You should check is there is something available to read. If not, then the read() will return -1. You can use Serial.available() to check the read buffer.

Comments

1

It reads 'ÿ' because there is no char to read in the buffer. It takes some time for the others characters to unstack from the uart buffer. So, you cannot do a loop to read chars. You have to wait that another character is available before reading it.

Also, this way of waiting characters is not the best way because it blocks the main loop.

Here is what I do in my programs:

String command;

void loop()
{
    if(readCommand())
    {
        parseCommand();
        Serial.println(command);
        command = "";
    }
}

void parseCommand()
{
  //Parse command here
}

int readCommand() {
    char c;
    if(Serial.available() > 0)
    {
        c = Serial.read();
        if(c != '\n')
        {       
            command += c;
            return false;
        }
        else
            return true;

    }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.