Morse code with push button

const int buttonPin = 11;    
const int ledPin = 13;      
const int buzzer = 9;

int ledState = HIGH;         
int buttonState = LOW;             
int lastButtonState = LOW;  

int pause_value = 400;  // depending on your skill and how fast your fingers are you can change this value to make typing a message faster or slower
long signal_length = 0;
long pause = 0;

String morse = "";
String dash = "-";
String dot = "*";

boolean cheker = false;
boolean linecheker = false;

long lastDebounceTime = 0;  
long debounceDelay = 50;    
void setup()
{
  Serial.begin(9600);
  
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  
  Serial.println("Welcome to Arduino-Uno morse ");
  Serial.println("Press button to start making morse code");
  

  while(!digitalRead(buttonPin));
    
  
}

void loop() {
 
  buttonState = digitalRead(buttonPin);

  
  
  if (buttonState && lastButtonState)       // basic state machine depending on the state of the signal from the button
  {
    ++signal_length;       
    if (signal_length<2*pause_value)        //this help to notice that there is a change in the signal length aka that its not a dot anymore but a dash
    {                                       // best use for the measuring of signal_length would be use of the millis() but this was used  for simplicity 
    tone(buzzer, 1500) ;
    }
    else
    {
      tone(buzzer, 1000) ;
      }
  }
  else if(!buttonState && lastButtonState)          //this part of the code happens when the button is released and it send either * or - into the buffer
  {
     
     if (signal_length>50 && signal_length<2*pause_value )
     {
       morse =  morse + dot;
     } 
      else if (signal_length>2*pause_value)
      {
        morse = morse +  dash;
      }
    signal_length=0; 
    digitalWrite(13, LOW); 
    noTone(buzzer); 
  }
  else if(buttonState && !lastButtonState)          // this part happens when the button is pressed and its use to reset several values
  {
    pause=0;
    digitalWrite(13, HIGH);  
    cheker = true;
    linecheker = true;
  }
  else if (!buttonState && !lastButtonState)
  {  
    ++pause;
    if (( pause>3*pause_value ) && (cheker))
    { 
      printaj(morse);
      cheker = false;
      morse = "";
    }
    if ((pause>15*pause_value) && (linecheker))
    {
      Serial.println();
      linecheker = false;
    }
  }
  lastButtonState=buttonState;
  delay(1);
}
void printaj(String prevodilac)   //ugly part of the code but it works fine
{                                 //compare morse string to known morse values and print out the letter or a number 
                                  //the code is written based on the international morse code, one thing i changed is that insted of typing a special string to end the line it happens with enough delay  
  if (prevodilac=="*-")
    Serial.print("A");
  else if (prevodilac=="-***")  
    Serial.print("B");
  else if (prevodilac=="-*-*")  
    Serial.print("C");
  else if (prevodilac=="-**")  
    Serial.print("D");
  else if (prevodilac=="*")  
    Serial.print("E");
  else if (prevodilac=="**-*")  
    Serial.print("F");
  else if (prevodilac=="--*")  
    Serial.print("G");
  else if (prevodilac=="****")  
    Serial.print("H");
  else if (prevodilac=="**")  
    Serial.print("I");
  else if (prevodilac=="*---")  
    Serial.print("J");
  else if (prevodilac=="-*-")  
    Serial.print("K");
  else if (prevodilac=="*-**")  
    Serial.print("L");
  else if (prevodilac=="--")  
    Serial.print("M");
  else if (prevodilac=="-*")  
    Serial.print("N");
  else if (prevodilac=="---")  
    Serial.print("O");
  else if (prevodilac=="*--*")  
    Serial.print("P");
  else if (prevodilac=="--*-")  
    Serial.print("Q");
  else if (prevodilac=="*-*")  
    Serial.print("R");
  else if (prevodilac=="***")  
    Serial.print("S");
  else if (prevodilac=="-")  
    Serial.print("T");
  else if (prevodilac=="**-")  
    Serial.print("U");
  else if (prevodilac=="***-")  
    Serial.print("V");
  else if (prevodilac=="*--")  
    Serial.print("W");
  else if (prevodilac=="-**-")  
    Serial.print("X");
  else if (prevodilac=="-*--")  
    Serial.print("Y");
  else if (prevodilac=="--**")  
    Serial.print("Z");

  else if (prevodilac=="*----")  
    Serial.print("1");
  else if (prevodilac=="**---")  
    Serial.print("2");
  else if (prevodilac=="***--")  
    Serial.print("3");
  else if (prevodilac=="****-")  
    Serial.print("4");
  else if (prevodilac=="*****")  
    Serial.print("5");
  else if (prevodilac=="-****")
    Serial.print("6");
  else if (prevodilac=="--***")  
    Serial.print("7");
  else if (prevodilac=="---**")  
    Serial.print("8");
  else if (prevodilac=="----*")  
    Serial.print("9");
  else if (prevodilac=="-----")  
    Serial.print("0");
  else if (prevodilac=="*-*-*-")
  break;
    
  prevodilac=""; 
}

I have this code and I want to change when the result will be printed.
I want to print the message only when I will type "star_star_star_ " (star =* )that means finish at morse code so untill that time somehow I need to store the results?

You could store the results in an array or a String.

define a sufficiently long buffer and index

const int N = 100;
char  buf [N];
int     idx
  else if (prevodilac=="----*")  
    buf [idx++] = '9'
  else if (prevodilac=="-----")  
    buf [idx++] = '0'
  else if (prevodilac=="*-*-*-")  {
        buf [idx] = '\0';
        Serial.println (buf);
        idx = 0;
   }

you may need to check that the buffer is too short for the msg. there are a couple ways. one is to simply dump the buffer

  else if (prevodilac=="*-*-*-" || idx == (N-1))  {
1 Like
const byte buttonPin = 11;
const byte ledPin = 13;
const byte buzzer = 9;

bool cheker = false;
bool linecheker = false;
bool buttonState = false;
bool lastButtonState = false;

const int pause_value = 400;  // depending on your skill and how fast your fingers are you can change this value to make typing a message faster or slower
long signal_length = 0;
long pause = 0;
long lastDebounceTime = 0;
long debounceDelay = 50;

String morse = "";
String dash = "-";
String dot = "*";
String Text = "";


void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.println("Welcome to Arduino-Uno morse ");
  Serial.println("Press button to start making morse code");
  while (digitalRead(buttonPin)==LOW);
}

void loop() {
  buttonState = digitalRead(buttonPin)==HIGH;
  if (buttonState && lastButtonState) {      // basic state machine depending on the state of the signal from the button
    ++signal_length;
    //this help to notice that there is a change in the signal length aka that its not a dot anymore but a dash
    // best use for the measuring of signal_length would be use of the millis() but this was used  for simplicity
    if (signal_length < 2 * pause_value)tone(buzzer, 1500);
    else tone(buzzer, 1000);
  }
  else if (!buttonState && lastButtonState) {         //this part of the code happens when the button is released and it send either * or - into the buffer
    if (signal_length > 50 && signal_length < 2 * pause_value )morse += dot;
    else if (signal_length > 2 * pause_value)morse += dash;
    signal_length = 0;
    digitalWrite(13, LOW);
    noTone(buzzer);
  }
  else if (buttonState && !lastButtonState) {   // this part happens when the button is pressed and its use to reset several values
    pause = 0;
    digitalWrite(13, HIGH);
    cheker = true;
    linecheker = true;
  }
  else if (!buttonState && !lastButtonState)  {
    ++pause;
    if (( pause > 3 * pause_value ) && (cheker)) {
      printaj(morse);
      cheker = false;
      morse = "";
    }
    if ((pause > 15 * pause_value) && (linecheker)) {
      Serial.println();
      linecheker = false;
    }
  }
  lastButtonState = buttonState;
  delay(1);
}

//ugly part of the code but it works fine
//compare morse string to known morse values and print out the letter or a number
//the code is written based on the international morse code, one thing i changed is that insted of typing a special string to end the line it happens with enough delay
void printaj(String Code) {
  if (Code == "*-")    Text += "A";
  else if (Code == "-***")    Text += "B";
  else if (Code == "-*-*")    Text += "C";
  else if (Code == "-**")    Text += "D";
  else if (Code == "*")    Text += "E";
  else if (Code == "**-*")    Text += "F";
  else if (Code == "--*")    Text += "G";
  else if (Code == "****")    Text += "H";
  else if (Code == "**")    Text += "I";
  else if (Code == "*---")    Text += "J";
  else if (Code == "-*-")    Text += "K";
  else if (Code == "*-**")    Text += "L";
  else if (Code == "--")    Text += "M";
  else if (Code == "-*")    Text += "N";
  else if (Code == "---")    Text += "O";
  else if (Code == "*--*")    Text += "P";
  else if (Code == "--*-")    Text += "Q";
  else if (Code == "*-*")    Text += "R";
  else if (Code == "***")    Text += "S";
  else if (Code == "-")    Text += "T";
  else if (Code == "**-")    Text += "U";
  else if (Code == "***-")    Text += "V";
  else if (Code == "*--")    Text += "W";
  else if (Code == "-**-")    Text += "X";
  else if (Code == "-*--")    Text += "Y";
  else if (Code == "--**")    Text += "Z";
  else if (Code == "*----")    Text += "1";
  else if (Code == "**---")    Text += "2";
  else if (Code == "***--")    Text += "3";
  else if (Code == "****-")    Text += "4";
  else if (Code == "*****")    Text += "5";
  else if (Code == "-****")    Text += "6";
  else if (Code == "--***")    Text += "7";
  else if (Code == "---**")    Text += "8";
  else if (Code == "----*")    Text += "9";
  else if (Code == "-----")    Text += "0";
  else if (Code == "*-*-*-") {
    Serial.println(Text);
    Text = "";
  }
  Code = "";
}

Hey and thanks for ANSWER!!!
I actually did that but i had to put the array into for loop so it can be increased every time... The bad part was the output, When i was typing for example (* sos) the result was all the alphabet couple times, Couldnt debug it im Beginner

Thanks a lot mate!!! Ill try ur solution Tomorrow afternoon thanks for your time! :heart:

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