0

I am trying to write an Arduino program that will translate a text string transmitted through the Serial Monitor to Morse code. This is the offending function:

void serialEvent() {
  while (Serial.available()){
    char inChar = Serial.read();
    input += inChar;
    if (inChar == '\0'){  
      Serial.print("END!");
      stringComplete = true;
    }
  }
}

It should take characters from the serial input one by one, adding them to the input string until it reaches the end of the serial input (ie a null character). For some reason the 'if' statement won't execute for

inChar == '\0'

But if I replace '\0' with an arbitrary character as in

inChar == 'g'

It executes just fine. Am I somehow calling the null character '\0' wrong?

4
  • What you want is read word by word, or letter by letter and determine the end of the entire message? Commented Dec 18, 2012 at 0:46
  • Yeah, something like that. The function reads each incoming character and appends it to String input. When it finds a null character I want stringComplete to be true, which is the cue for a separate function to take over. Commented Dec 18, 2012 at 1:07
  • What I think is that you think that you read all characters in the while loop, Am I right? The truth is that only a character is read by each call to the loop() method, so, you can set stringComplete = true when Serial.available() == 0. Commented Dec 18, 2012 at 1:14
  • Great! I have posted the answer :) Commented Dec 18, 2012 at 1:25

1 Answer 1

4

What I think is that you think that you read all characters in the while loop, Am I right? The truth is that only a character is read by each call to the loop() method, so, you can set stringComplete = true when Serial.available() == 0.

Sign up to request clarification or add additional context in comments.

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.