0

In my recent project, I am working with serial data. Till now, I'm able to receive data and display on LCD. Now, suddenly I can't get any serial data. Nothing display on LCD.

My incoming string is:

{"Action":"ONE","TPS":"0.40","MAP":"0.95","LOAD":"14"}

And my code is below:

#include<LiquidCrystal.h>LiquidCrystal lcd(12, 11, 7, 6, 5, 4);String response = "";bool begin = false;void setup(){  Serial.begin(9600);  lcd.begin(16, 2);}void loop(){  while(Serial.available() || !begin)  {    char in = Serial.read();    if (in == '{')    {      begin = true;    }    if(begin)    {      response += (in);    }    if(in == '}')    {        break;    }    delay(1);  }  lcd.setCursor(0, 0);  lcd.print(response);}
askedJul 5, 2017 at 9:26
Hasan's user avatar
7
  • 1
    :) Does your LCD work? Can you print a literal value to it? I suspect the problem is that rather than you have forgot to set begin to false before the break statement.CommentedJul 5, 2017 at 9:45
  • @CodeGorilla Yes, my LCD working fine. And already tried put that things mean set to begin to false. But, not working.CommentedJul 5, 2017 at 9:56
  • 1
    Something changed from the time the LCD worked until when it didn't - what changed? Did you reload the code? Same code/ different code? Change any wiring, even accidentally? Short circuit something? Go back and figure out what might have changed and try to undo the change. Unless it was accidental damage, you should be able to get working again.CommentedJul 5, 2017 at 10:33
  • Did you sent some blank lines before the "{...}" data?CommentedJul 5, 2017 at 10:38
  • 1
    I tested your code and it works. The only way to see nothing in the LCD is that response start with a lot of spaces. Mistery.CommentedJul 5, 2017 at 11:29

2 Answers2

1

Actually I can see what I think may be an issue with your code.

 while(Serial.available() || !begin)

This isn't right. If there is no data available in the serial buffer but the first{ hasn't been seen then go and read the non-existent data anyway?

Rather than try and explain what I mean in multiple comments I have put it all together and this is what I think your loop should be this:

/// This is un-compiled code, so you might have to fix some little issues./// bool begin = false;         // True when the initial { has been seenvoid loop(){  while (finished == false)  {    if (Serial.available())    {      const char in = Serial.read();      if (in == '{')      {        response = "";  // Blank the string        begin = true;      }      if (begin)      {        response += (in); // Only write if within the {}      }      if (in == '}')      {         begin = false;   // Prevent any chars between } and { leaking through.        lcd.setCursor(0, 0);        lcd.print(response);      }    }    delay(1);  }  delay(500); // Not sure what will happen if you send multiple {} to the LCD too fast.}
answeredJul 5, 2017 at 12:08
Code Gorilla's user avatar
1
  • I tried out your code and it's working fine. And now my code is working fine. Why ? I answer it.CommentedJul 5, 2017 at 13:46
0

I just switch from Arduino UNO to Arduino MEGA. And everything works fine. I know this is not a proper answer but I want to know why this happens. I don't understand why this happens? Can anyone explain me?

answeredJul 5, 2017 at 13:48
Hasan's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.