1

I have a sketch as follows:

void loop()
    char End_Char = ‘\n’;
    if (Serial.available()) {
        aInt = Serial.read();
    }
    switch (aInt) {
    case 1:
        // does a task that is triggered by a number
        // I get correct responses if aInt=1
        break;
    case 2:
        bString = Serial.readStringUntil(End_Char);
        Serial.print(bString);
        // The sketch freezes and doesn’t do anything if I enter 2 but restarts through case 1 if I enter 1 when the sketch is seemingly frozen
        break;
    Default:
        break;
    } // end of switch
} // end of loop

I have tried re-ordering the case statements, putting in delays at various points to give time to read the string, changed the Serial.setTimeout to 100 and 2000 at various points to give time to read the string. However, the sketch fails to read the string.

This code works:

char end_char = '\n';
String texto;

void setup() {
    Serial.begin(9600);
}

void loop() {
    if (Serial.available()) {
        texto = Serial.readStringUntil(end_char);
        Serial.println(texto);
    }
}

I’m using an Arduino UNO, HC-06 and an Android app.

Any ideas?

15
  • Are you actually sending the endline character at the end of the string? Also: You are using Serial.read() for the integer. So are you really sending first a binary digit and then an ASCII encoded text? – chrisl Apr 14 at 12:15
  • 1
    That you have char End_Char = /n; in the above indicates that you're not showing real code. Also it seems unlikely you want the body of your if statement to end where it is. – timemage Apr 14 at 12:20
  • 1
    To be clear, char End_Char = /n; will not compile. If you think it is compiling, something else is wrong. – timemage Apr 14 at 12:26
  • 2
    Please no code in the comments. You can edit your question to post the actual code and refine your question. – chrisl Apr 14 at 12:41
  • 1
    I second the comment of timemage. Can you please explain how exactly you are sending the serial data to the Arduino? Are you using the Serial Monitor of the Arduino IDE? Or another program? What settings does that program use? With these information be can check, if our guess is right, so please give us every detail when answering (all!) my questions. – chrisl Apr 17 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.