I have a 16x2 LCD display, I am making a simple morse code generator / decoder project, and I added a counter to determine when 16 letters have been entered. The issue is depending on the letter I type the display will not move the cursor down (and even produces a weird error where it looks like a character is seemingly inserted into the 16th row ontop of the character that is already displayed).
I've noticed that when my 16th letter is an S it will work, as will anything above the char(S), anything below it will also work, if i keep inputting and the characters eventually overflow to the next line naturally.
Any help as to why this is happening would be greatly appreciated!
On a side note, anytime the buffer overflows with DITS or DAHs and Char goes above the numeric characters it overflows to 124 ( | ), any ideas on why it stops here? I already have a plan of action to fix this by determining if the value overflows a numerical char that represents a character it will erase the buffer and will not write to the LCD screen waiting for a valid character to write into the current cursor position.
#include <LiquidCrystal.h> // Bibloteca para tela LCD
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Pinos da arduino ligados respectivamente ao (rs, enable, d4, d5, d6, d7).
int botao = 8, piezo = 9;
bool ultimoEstadobotao = false; // Ultimo estado do botao.
int botaoEstado = 0; // Estado atual do botao.
int inicioPressionado = 0; // Momento em que o botao foi acionado.
int fimPressionado = 0; // Momento em que o botao foi liberado.
int tempoSegurado = 0; // Quanto tempo o botao foi acionado.
int tempoParado = 0; // Quanto tempo o botao esta parado.
int numeroLetras = 0; // Contador que armazena a quantidade de letras que já foram digitadas.
String buffer = ""; // Buffer para armazenar os Dits e Dats que compoem uma letra.
void setup ()
{
// Inicializa a interface da tela LCD e define as dimensoes da tela (quantidade de colunas e linhas respectivamente).
lcd.begin(16, 2);
pinMode (botao, INPUT);
Serial.begin(9600);
}
void loop()
{
// Leia o input do botao
botaoEstado = digitalRead(botao);
if (botaoEstado == LOW)
{
// Letra
if ((millis() - fimPressionado) % 65536 > 500)
{
// Se o buffer tiver algum valor printa a letra.
if (buffer != "")
{
Serial.println(retornarBuffer());
limpaBuffer();
numeroLetras++;
ultimoEstadobotao = true;
}
}
// Palavra
if ((millis() - fimPressionado) % 65536 > 1350)
{
if (ultimoEstadobotao == true)
{
Serial.println("Espaço");
ultimoEstadobotao = false;
}
}
}
// Estado do botao foi alterado
else if (botaoEstado == HIGH )
{
inicioPressionado = millis();
atualizarEstado();
}
moverCursor();
}
void moverCursor()
{
// Quando 16 letras forem escritas move o cursor da tela LCD para a linha de baixo, adicione um ao contador de letras para que o Loop n seja infinito
if(numeroLetras == 15)
{
Serial.println("LCD configurada para linha 1");
numeroLetras++;
lcd.setCursor(0, 1);
}
}
// Limpa o Buffer
void limpaBuffer()
{
buffer = "";
}
// Aplica o valor do buffer a função do codigo morso para que ele seja comparado a lista existente.
String retornarBuffer()
{
return morseParaChar(buffer);
}
void atualizarEstado()
{
// O botao foi apertado.
while (digitalRead(botao) == HIGH)
{
fimPressionado = millis();
}
// O botao foi liberado
tempoSegurado = fimPressionado - inicioPressionado;
// Se o botao foi segurado por menos de 300 millisegundos, adicione um DIT (.) ao buffer.
if (tempoSegurado < 300)
{
tone(9, 675, 200);
Serial.println("DIT");
buffer += ".";
}
// Se o botao foi segurado por mais de 300 millisegundos, adicione um DAH (-) ao buffer.
else if (tempoSegurado >= 300)
{
tone(9, 675, 500);
Serial.println("DAH");
buffer += "-";
}
}
// Define uma lista de strings que contem todo o alfabeto morse, relacionado a sua posição dentro da lista (seu indice)
String morseParaChar(const String& morse)
{
static String letras[26] =
{".-", "-...", "-.-.", "-..", ".",
"..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-",
"-.--", "--.."
};
// Um for loop que compara o buffer (morse) com o indice da lista especifica e adiciona ele ao valor do char, assim o A seria indice 0 + 65 = char(65)
// que é o char A, o 66 seria o B e assim em diante.
for (int k = 0; k < sizeof(letras); k++)
{
if (letras[k] == morse)
{
lcd.print((char) (k + 65));
return ((String) (k + 65));
}
}
}