Bluetooth(serial) to LCD

I have an Arduino uno connected to a HC-05 Bluetooth module and a 16x2 LCD screen, I have connected to the Bluetooth module from my phone and I have written a code to display on the LCD screen characters that are sent from my phone to the Arduino using Bluetooth.

my code is,

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
char val; 
int c=0;
int r=0;
int R=0;
void setup() {
 // put your setup code here, to run once:

Serial.begin(9600);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
//lcd.print(val);

}

void loop() {
 // put your main code here, to run repeatedly:
if(Serial.available())
{
 val = Serial.read();
 if((c<=15)&&(r==0))
 {
 lcd.write(val);
 c++;
 lcd.setCursor(c,r);
 
 }
 if(c>15)
 {
   lcd.setCursor(R,1);
   lcd.write(val);
   R++;
 }
 if(R>15)
 {
   lcd.clear();
   lcd.setCursor(0,0);
   c=0;
   r=0;
   R=0;
 }
 }
delay(200);
}

Say for example if I send “hello there” from my phone the characters get displayed on the LCD but at the start there’s always a random character(garbage value) you can see in the attached picture. Can anyone offer some help on this?

Thanks

I don’t know if this anecdotal information is pertinent but here it is anyway.

Back in the digital stone age, when dealing with serial communication using UARTs, we typically had to discard an initial ‘garbage’ character coming from said UART. Maybe something similar is occurring here.

Also: Go back to your initial post, highlight your code, and use the ‘code’ button which is has the icon </> all the way at the left.

Don

maybe a little delay after reading the serial or before try half second or one second

delay(3);

should suffice

Thanks for all your replies, I tried the the delay(3), this didnt solve my problem, I need to write code to ignore the first byte of the serial receive buffer like floresta pointed out, any hints on how that can be done?

I don't think you should need to do that.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

Hello,

thanks for the help the documents were really useful, can serve as good reference for future as well, I was able to run the code in the first try no issues what so ever and no garbage value received on the LCD, however I could only send one character at a time. Following some of the earlier advices I decided to write a code that would ignore the first byte received from the serial receive buffer and display the rest on the LCD. I came up with something like this…

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
char val[20];//variable to store character from serial.read command 
int c=0; //Defines column number for LCD
int r=0;//Defines row number for LCD
int R=0;//Defines column number when cursor in is second row of LCD
int i=0;
int totalbytes;
void setup() {
  // put your setup code here, to run once:
pinMode(13,OUTPUT);
Serial.begin(9600);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
//lcd.print(val);

}

void loop() {
  // put your main code here, to run repeatedly:
if(Serial.available())
{
  delay(50);
  totalbytes = Serial.available();
 for(i=0;i<=totalbytes;i++)
  {
  val[i] = Serial.read();
  }
  if(val[1]=='#')
  {
    lcd.clear();
    c=0;
  }
  else
  {
  printonLCD();
  lcd.setCursor(0,1);
  lcd.print("total bytes RX");
  lcd.setCursor(14,1);
  lcd.print(totalbytes);
  }
 /* if((c<=15)&&(r==0))
  {
  lcd.print(val);
  //Serial.print(val);
  c++;
  lcd.setCursor(c,r);
  
  }
  if(c>15)
  {
    lcd.setCursor(R,1);
    lcd.print(val);
    //Serial.print(val);
    R++;
  }
  if(R>15)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    c=0;
    r=0;
    R=0;
  }*/
  }
delay(50);
}

void printonLCD()
{
  for(i=1;i<totalbytes;i++)
  {
    lcd.setCursor(c,0);
    lcd.print(val[i]);
    delay(500);
   c++;
  }
}

This isn’t finished, I was just experimenting so that explains why the code is in such bad shape, has a few minor issues but its atleast a step in the right direction, now there is no garbage value on the screen and I can send multiple characters at a time.

Thanks for all the help.

waqaszahid:
however I could only send one character at a time.

Aside from regular data transmission, I just use bluetooth to receive a four character command in MMDD format to download files. I used to display that on LCD as confirmation. You can see this on p12. Your problem could be at the phone end, i.e. the way the characters are being sent.

i dont understand how does the bluetooth to mobile phone communication works if in code 9600 is set as baud rate, actually 9600 is the communication between computer and arduino then why are we not giving 38400 as baud rate for serial communication..

Your post is incoherent and nonsense. There are three communication operations that you may, but likely don’t, need to consider.

monitor <> Arduino, at a baud rate you may set

Arduino <> Bluetooth at a baud rate you may set, and may be different to the above

Bluetooth <> phone, by a different procedure, over which you have no control and don’t need to know about.

There is no obligation to use 38400 for communication, but it is required for configuring an HC-05. You might fine the link in reply #5 useful.