Hello there.. I'm having some trouble with the lcd display.
Our project is a morse code translator. The idea is to type some words in the serial monitor, it will then be displayed in the lcd and after some delay it will be cleared from the lcd.
The problem is...each letter gets cleared in the lcd. For example if I typed in "Hello" from the serial monitor. The LCD will display.... "H"..clear... "e"...clear... and so on.
You'll notice there is an lcd.clear function at the end of the void loop. If that is not there the lcd display will get crowded with words I keyed in the from serial monitor. But it also the one causing the problem above.
Any kind of help will be greatly appriciated.
Im sorry for my grammer. English is not our native language
here is the code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, NEGATIVE);
#define I2C_ADDR 0x27 // Define the address of the serial communication display
#define LED_OFF 0
#define LED_ON 1
int ledPin = 9;
int speaker = 13;
char* letters[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // letters A-I
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // letters J-R
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." //letters S-Z
};
char* numbers[] = {"-----", ".----", "..---", "...--", "....-", ".....", "-....",
"--...", "---..", "----."
}; // numbers 0-9
int dotDelay = 200; //Dot time
void setup()
{
lcd.begin(16, 2); //Initialize the display
lcd.setBacklight(LED_OFF);
lcd.cursor();
pinMode(ledPin, OUTPUT);
pinMode(speaker, OUTPUT);
Serial.begin(9600);
Serial.println("\nSTART");
}
void loop()
{
char ch;
if (Serial.available()) // is there anything to be read from USB?
{
ch = Serial.read(); // read a single letter
if (ch >= 'a' && ch <= 'z')
{
flashSequence(letters[ch - 'a']);
lcd.write(ch);
Serial.print(ch);
delay(500);
}
else if (ch >= 'A' && ch <= 'Z')
{
flashSequence(letters[ch - 'A']);
lcd.write(ch);
Serial.print(ch);
delay(500);
}
else if (ch >= '0' && ch <= '9')
{
flashSequence(numbers[ch - '0']);
lcd.write(ch);
Serial.print(ch);
delay(500);
}
else if (ch == ' ')
{
delay(dotDelay * 4); // gap between words
lcd.write(ch);
Serial.print(ch);
delay(500);
}
}
lcd.clear();
}
void flashSequence(char* sequence)
{
int i = 0;
while (sequence[i] != '\0')
{
flashDotOrDash(sequence[i]);
i++;
}
delay(dotDelay * 3); // gap between letters
}
void flashDotOrDash(char dotOrDash)
{
digitalWrite(ledPin, HIGH);
if (dotOrDash == '.')
{
delay(dotDelay);
}
else // must be a -
{
delay(dotDelay * 3);
}
digitalWrite(ledPin, LOW);
delay(dotDelay); // gap between flashes
}