void String2Morse()
{
len = code.length();
for (int i = 0; i < len; i++)
{
ch = code.charAt(i);
morse();
}
}
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));
Serial.print(" ");
break;
}
i++;
}
if (morse[i] == "!")
{
Serial.println("");
Serial.println("This code is not exist!");
}
code = "";
}
void decoder() {
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();
}
void generator() {
while (Serial.available())
{
code = Serial.readString();
Serial.print(code);
Serial.print(" = ");
String2Morse();
Serial.println("");
}
}
int num = 0;
void project(){
if (digitalRead(button) == (HIGH))
num = 1 - num;
if(num)
decoder();
else
generator();
project();
}
void loop() {
if (digitalRead(button) == (HIGH)) {
mode();
}
else
project();
}
void mode() {
if (digitalRead(button) == (HIGH)){
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);}
else if (digitalRead(button) == (LOW)){
project();
}
}
Theres more to the code but I am not getting any error and the rest of the code is fine. The issue is solely in project(). I want to use generator() but then be able to switch to decoder() at the press of a button (button). The problem is that when I run this code I can use generator() but it doesnt switch to decoder() when I press a button. Once i press a button decoder() will only run after I use generator() again.
I have attached my code as well as my schematic though I dont think this problem extends to that. I may need to use interrupts but I dont know.
Example: I input 2 dots to output 'i' on screen. I then click another button (button) to switch. I input 'o' on my screen to output it as dots and dashes through decoder() but nothing happens. Once I input dots and dashes again using another button (but) it displays the translated word on the screen then it outputs 'o' from the previous step correctly.
MorseKey.ino (10 KB)
