Recieving string and displaying it on a LCD screen

I am trying to make a performance monitor, I wrote a program in C# that reads the usage of my RAM, then it is sent by Serial port to my arduino uno. The arduino programs displays is it on a LCD screen.

So I recieve the data from the serial port and save it to a char variable, which then is displayed on the LCD, hower it does not do it correctly.

It always displays only the last number of the usage, lets say the usage is 87%. It gets sent to the board and only displays the 7 from 87. If the usage changes lets say to 85, it only displays the 5.

I have spent alot of time trying to fix this, but nothing I did fixed this issue.

So I wanted to ask if any of you guys could help me!

The code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(3, 4, 5, 6, 7, 8);
int menuButton = 12;

void setup ()
{
  Serial.begin(9600);
  lcd.begin (16, 2);
  pinMode(menuButton, INPUT_PULLUP); 
  MainScreen();
}

void loop ()
{
  if (!digitalRead(menuButton))
  {
    tone(9, 330, 75);
    UpdateScreen();
    _delay_ms(1000);
    PerformanceMenu();
   while(!digitalRead(menuButton));     
  }
}

void MainScreen()
{
 lcd.setCursor(5, 0);
 lcd.print("READER");
 lcd.setCursor(6, 1);
 lcd.print("1.2.");  
}

void PerformanceMenu ()
{
  lcd.clear();
  lcd.print("RAM:");
  if (Serial.available())
  {
  delay(100);
  }
  
  while (Serial.available()>0)
  {
  char i = Serial.read();
  lcd.setCursor(4,0);
  lcd.print(i);
  }
}

void UpdateScreen ()
{
  lcd.clear();
  lcd.setCursor(3,0);
  lcd.print("PERFORMANCE");
  lcd.setCursor(6,1);
  lcd.print("MENU");
}

Welcome to the forum

    while (Serial.available() > 0)
    {
        char i = Serial.read();
        lcd.setCursor(4, 0);
        lcd.print(i);
    }

Serial.read() only reads a single character. You print every character to the same position on the LCD so no wonder that you only see the last character received

The above 2 statements are in a while loop

Each time you read a character from the PC, you set the cursor position, and print the character at that position.

So when the PC sends the 2 characters 8 and 7, you first print the first character (8) at location [4,0], and then you print the second character (7) at location [4,0]... right over top of the first character.

Try moving lcd.setCursor(4,0); to before the loop.

Thanks guys! IT WORKS! I moved it to the loop as you said. Thanks for the help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.