Toggling between 2 functions with a use of a button (SIMPLE)

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)

I've had a quick look through your code and I think it may be to do with the wat your reading your button(I could be wrong). Try changing all the code how you read buttons.

if (digitalRead(button) == (HIGH)){

Should read like this

 buttonState = digitalRead(button);
if (buttonState== HIGH) {

A Tip it may help that you post all your code, Have you declared the input/output pins in your setup ?
You may have missed something that we can't see with snippets of code

I attached my full code as a file along with the screenshot of my schematic. Is it not showing up? I tried using buttonState but the problem still persists. It seems like decoder() will only be run if generator() is run first without the button doing anything.

I copy pasted the decoder() and generator() from this website https://create.arduino.cc/projecthub/electropeak/how-to-make-a-morse-code-translator-with-arduino-d6ecc8?ref=user&ref_id=573543&offset=1 . I need to have a blinking LED until a button is pressed then after that be able to switch between decoder() and generator(). Thats my entire aim but the 2nd part isnt happening properly. I will paste my entire code here but there is a lot of fluff.

#include <DueTimer.h>


String code = "";
int len = 0;
char ch;
char new_char;
const int but = 2;
const int button = 5;
const int buz = 8;
int buttonState;
const int led = 13;
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 num = 0;


void setup() {
Serial.begin(9600);
pinMode(but, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(buz, OUTPUT);
pinMode(button, INPUT_PULLUP);
Serial.println("I am ready...");
}



void dot()
{
  Serial.print(".");
  digitalWrite(led, HIGH);
  digitalWrite(buz, HIGH);
  delay(unit_delay);
  digitalWrite(led, LOW);
  digitalWrite(buz, LOW);
  delay(unit_delay);
}

void dash()
{
  Serial.print("-");
  digitalWrite(led, HIGH);
  digitalWrite(buz, HIGH);
  delay(unit_delay * 3);
  digitalWrite(led, LOW);
  digitalWrite(buz, LOW);
  delay(unit_delay);
}

void A()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void B()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}

void zero()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void morse()
{
  if (ch == 'A' || ch == 'a')
  {
    A();
    Serial.print(" ");
  }
  else if (ch == 'B' || ch == 'b')
  {
    B();
    Serial.print(" ");
  }
 
  else if (ch == '9')
  {
    nine();
    Serial.print(" ");
  }
  else if(ch == ' ')
  {
    delay(unit_delay*7);
    Serial.print("/ ");
  }
  else
    Serial.println("");
}

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("");
  }
}



void project(){
  buttonState = digitalRead(button);
  if (buttonState == (HIGH)){
    num = 1 - num;
  }
  if (num){
    decoder();
  }
  else 
  {
    generator();
  }
  project();
}

void loop() {
  
  if (digitalRead(button) == (HIGH)) {
    mode();
  }
  else
  {
    project();
  }
}

Looking at the frizzy thing I expect you to possibly fry your Arduino. Post a schematic, by the time you get it drawn you will probably see what I am talking about. I could be wrong I do not read frizzy very well.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.