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);
}
  • 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. – Code Gorilla Jul 5 '17 at 9:45
  • @CodeGorilla Yes, my LCD working fine. And already tried put that things mean set to begin to false. But, not working. – Hasan Jul 5 '17 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. – JRobert Jul 5 '17 at 10:33
  • Did you sent some blank lines before the "{...}" data? – user31481 Jul 5 '17 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. – user31481 Jul 5 '17 at 11:29
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 seen

void 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.
}
  • I tried out your code and it's working fine. And now my code is working fine. Why ? I answer it. – Hasan Jul 5 '17 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?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.