I'm using Bluetooth with Arduino for a while now and I wanted to add a LCD to my project, so I did and when I tried to print characters in my LCD I saw weird characters and it wasn't working, I thought I just connected something wrong, so now a few days later I re-connected everything tried an example sketch for the LCD and yes it was working all good.
Now again when I use my Bluetooth the LCD screen is giving random characters, even in the serial monitor. I'm lost and I think I'm making a big mistake...
I could use some help from you guys!
Sketch will be in the attachments but also here:
#include <SoftwareSerial.h> // Bluetooth library
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 13);
int bluetoothTx = 2;
int bluetoothRx = 3;
int gled = 7; // some LED's added
int rled = 9;
int bled = 8;
int leesBt; // means ReadBluetooth
String readString;
SoftwareSerial bluetooth (bluetoothTx, bluetoothRx);
void setup() {
pinMode(gled, OUTPUT);
pinMode(rled, OUTPUT);
pinMode(bled, OUTPUT);
Serial.begin(9600);
bluetooth.begin(9600);
lcd.autoscroll();
lcd.begin(16, 2);
lcd.clear();
lcd.print("Welkom!"); // Printing welcome
delay(1000);
}
void loop() {
while (bluetooth.available()) {
leesBt = bluetooth.read();
char c = bluetooth.read(); // When I type something in my mobile phone the bluetooth will read it and print it on the LCD
readString += c;
}
if (readString.length() > 0 && readString.length() <= 32) { // If not more than 32 characters (total characters my LCD can have it will print it
lcd.clear();
Serial.println(readString);
lcd.print(readString);
delay(1000);
}
else if (readString.length() > 32) { // if more than 32 characters it will give an error!
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Error teveel");
lcd.setCursor(0, 1);
lcd.print("tekst!");
delay(1000);
}
if (leesBt == '1') { // this part is working all fine..
lcd.clear();
Serial.println("Gled aan");
digitalWrite(gled, HIGH);
bluetooth.print("1");
delay(1000);
}
if (leesBt == '0') {
lcd.clear();
Serial.println("Gled uit");
digitalWrite(gled, LOW);
bluetooth.print("0");
delay(1000);
}
}
while (bluetooth.available()) {
leesBt = bluetooth.read();
char c = bluetooth.read(); // When I type something in my mobile phone the bluetooth will read it and print it on the LCD
readString += c;
}
Why are you reading two characters when all you know is that there's one available?
Yes Bob, they're correct!
Alright AWOL, the weird characters from the serial and LCD screen are gone now.
What I get now is an empty LCD screen with 1 character blinking in the middle of the screen, I see also the text I type in the Serial monitor but it sums everything up I type.
I want everytime I type something to get the thing I typed before it removed. Just check these pictures and you'll see:
Alright I fixed it that the text was duping, and I fixed it that it printed the only first letter..
Now the only problem is that my LCD screen won't print the text.. it's just not working...
CODE WITH IMPROVEMENTS:
#include <SoftwareSerial.h> // Bluetooth library
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 13);
int bluetoothTx = 2;
int bluetoothRx = 3;
int gled = 7; // some LED's added
int rled = 9;
int bled = 8;
int leesBt; // means ReadBluetooth
String readString;
SoftwareSerial bluetooth (bluetoothTx, bluetoothRx);
void setup() {
pinMode(gled, OUTPUT);
pinMode(rled, OUTPUT);
pinMode(bled, OUTPUT);
Serial.begin(9600);
bluetooth.begin(9600);
lcd.autoscroll();
lcd.begin(16, 2);
lcd.clear();
lcd.print("Welkom!"); // Printing welcome
delay(1000);
}
void loop() {
while (bluetooth.available()) {
delay(3);
char c = bluetooth.read(); // When I type something in my mobile phone the bluetooth will read it and print it on the LCD
readString += c;
}
if (readString.length() > 0 && readString.length() <= 32) { // If not more than 32 characters (total characters my LCD can have it will print it
lcd.clear();
Serial.println(readString);
lcd.print(readString);
delay(1000);
readString="";
}
}
Alright so I think I figured out what is the problem, I think the LCD screen needs a pin with a receiver like 2, but the Bluetooth module needs 1 as well, and the Arduino Uno doesn't have space for this I think?
If it does, anyone knows which pins I should use for the LCD and the Bluetooth module?
UKHeliBob: SoftwareSerial bluetooth (bluetoothTx, bluetoothRx);The order of the names still look whacky to me
Hahah that's seriously not it, it's all working as long as I use pin 2 and 3, when I use other pins for the Bluetooth it won't receive anything.. but I need pin 2 for the LCD screen as well, so I'm stuck which pins I have to use for the LCD and which ones for the Bluetooth. Because when I use pin 0 and 1 for the Bluetooth it won't upload the sketch..
Why? What kind of LCD screen REQUIRES that you use pin 2?
It is difficult to believe that SoftwareSerial cannot communicate with your device on other pins, unless you haven't actually moved the device to other pins.
Oh then I have really no idea why my LCD screen is not working, I'm using the following pins now: (12, 11, 10, 9, 8, 7), and these are not working for some reason.. I don't think it's the code because an example sketch on the LCD won't work either.
For the bluetooth module I'm using pin 2 and 3 and these are working fine.
Thanks for all the help.
Sure Bob, it WAS working with the Blink sketch, now I changed some pin positions and the LCD screen just stays blank, are there pins that I do not have to use for my LCD?
The example blink sketch.
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // these are not the pin numbers I'm using, I know I have to change these :P.
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// Turn off the blinking cursor:
lcd.noBlink();
delay(3000);
// Turn on the blinking cursor:
lcd.blink();
delay(3000);
}
I now have my Bluetooth pins is 0 and 1 and that's not working, it's giving an error when I upload the sketch, but when I put my Bluetooth pins in other pins than 2 and 3 (for example 8 and 9) the Bluetooth module won't receive anything.. so I'm stuck there!
The LCD screen uses the pin numbers 2, 3, 4, 5, 6, 7 now but it's not working.
Thanks Paul.. I messed up in the order, my bad. Screen is working now. Do you have a tip or any idea how to easily scroll to the 'setCursor(0, 1); when I have more than 16 characters, I only want the characters after the 16 to be displayed on the 2nd line.
UKHeliBob:
Keep a count of the number of characters printed and reset the cursor position when you have printed the required number of characters.
But how, I get 2 problems then, 1- how do I count the number from the readString and if I have more than > 16 characters and I do setCursor(0, 1); it will print all the characters on the 2nd line, and not the first 16 on the first line, and the remaining characters on the 2nd line.
Actually you don't need to if you wait until readString is complete before printing it to the LCD.
I am not familiar with the String functions but I imagine that it has a method or methods to extract portions of the String. Extract the first 16 characters from readString. Set the cursor to the start of line 1 and print the subString. Set the cursor to the start of line 2 and print the rest of readString.