I am new to Arduino and I am trying to create a Morse code based "chat".
I can submit a message in Morse code using the send button (the one on the right,) and when I press the button on the left and the whole decoded message should appear on the screen. However, this is not happening unless I press the send button immediately after introducing the Morse code. Can anyone help me out? I tried changing the delay period, but nothing seems to make it send how it should.
As I said, I am a beginner, so please go easy on me.
This is my code:
/* String tone = "-... ..- -. .- ";
Morse code - Receiver
modified on 14 Apr 2019
by Saeed Hosseini @ Electropeak
https://electropeak.com/learn
*/
#include <LiquidCrystal.h>
// Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7):
LiquidCrystal lcd = LiquidCrystal(2, 3, 4, 5, 6, 7);
String code = "";
int len = 0;
char ch;
int ok;
char new_char;
int i1=0;
int i2=1;
const int but_send=8;
const int but = 12;
const int led = 13;
String a="";
unsigned long pres_len = 0, rel_time, pres_time = 0, old_time_len = 0, old_pres = 0, space = 0;
int state = 0;
int unit_delay = 250;
int min_delay = 10;
int buttonState = 0;
char MakeString()
{
if (pres_len < (unit_delay*3) && pres_len > 50)
{
return '.'; //if button press less than 0.6sec, it is a dot
}
else if (pres_len > (unit_delay*3))
{
return '-'; //if button press more than 0.6sec, it is a dash
}
}
void Morse_decod()
{
static String morse[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
"..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "!"
};
int i = 0;
while (morse[i] != "!")
{
if (morse[i] == code)
{
Serial.print(char('A' + i));
a=a+char('A' + i);
//lcd.print(char('A' + i));
Serial.print(" ");
if(i2>=2)
{
i1++;
i2=0;
}
else
{i2++;
}
break;
}
i++;
}
if (morse[i] == "!")
{
Serial.println("");
Serial.println("This code is not exist!");
}
code = "";
}
void sendText()
{
Serial.println(digitalRead(but_send));
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
//if (digitalRead(but_send) == HIGH) {
// turn LED on:
//gitalWrite(led, HIGH);
if(ok==1){
Serial.println(a);}
ok=0;
//}
//else {
// turn LED off:
//Serial.println("NIMIC");
//}
delay(200);
}
void setup() {
Serial.begin(9600);
pinMode(but, INPUT_PULLUP);
pinMode(but_send, INPUT_PULLUP);
pinMode(led, OUTPUT);
lcd.setCursor(i1, 12);
lcd.begin(16, 2);
}
void loop() {
label:
while (digitalRead(but) == HIGH) {}
old_pres = rel_time;
pres_time = millis();
digitalWrite(led, HIGH);
while (digitalRead(but) == LOW) {}
rel_time = millis();
digitalWrite(led, LOW);
pres_len = rel_time - pres_time;
space = pres_time - old_pres;
if (pres_len > min_delay)
{
code += MakeString();
}
while ((millis() - rel_time) < (unit_delay * 3))
{
if (digitalRead(but) == LOW)
{
goto label;
}
}
Morse_decod();
delay(1000);
if (digitalRead(but_send) == LOW)
{
ok=1;
//Serial.println("A intrat aici");
sendText();
//delay(100);
}
//Serial.println(digitalRead(but_send));
}
This is what should happen all the time, not only after pressing the send button very fast:
If needed, I can send a link from my tinkercad simulation.