# Morse code decoder problems

**URL:** <https://forum.arduino.cc/t/morse-code-decoder-problems/986061>\
**Category:** Programming\
**Created:** [April 28, 2022, 9:34am UTC](https://forum.arduino.cc/t/morse-code-decoder-problems/986061 "2022-04-28T09:34:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sneakyturtle-05](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sneakyturtle-05/32/774641_2.png) [@sneakyturtle-05](https://forum.arduino.cc/u/sneakyturtle-05)\
**Post date:** [April 28, 2022, 9:34am UTC](https://forum.arduino.cc/t/morse-code-decoder-problems/986061/1 "2022-04-28T09:34:54Z")

</div>

i am struggeling with making a morse code decoder for a school project.  
can anyone help me. A bit of info. the cuircet is containing of 2 buttons. one witch is a push button for typing in the morse code (long and short). the other button is for entering the morse code in the Serial monitor.

this is the code for my push buttons

//// SWITCH ////  
const int buttonPin = 8; // switch pin  
const int outPin = 13; // outputpin  
const int IR = 6; // IR pin  
const int Enter = 9; // Enter pin  
int buttonStatePrevious = LOW; // previousstate of the switch

unsigned long minButtonLongPressDuration = 600; // Time we wait before we see the press as a long press  
unsigned long buttonLongPressMillis; // Time in ms when we the button was pressed  
bool buttonStateLongPress = false; // True if it is a long press

const int intervalButton = 50; // Time between two readings of the button state  
unsigned long previousButtonMillis; // Timestamp of the latest reading

unsigned long buttonPressDuration; // Time the button is pressed in ms

//duration of the long and short  
int Short = 200;  
int Long = 600;

//// GENERAL ////

unsigned long currentMillis; // Variabele to store the number of milleseconds since the Arduino has started

void setup(){  
Serial.begin(9600); // Initialise the serial monitor

pinMode(buttonPin, INPUT); // set buttonPin as input  
pinMode(outPin, OUTPUT); // set outPin as OUTPUT  
Serial.println("Press button");

}

// Function for reading the button state  
void readButtonState() {

// If the difference in time between the previous reading is larger than intervalButton  
if(currentMillis - previousButtonMillis \> intervalButton) {

```
// Read the digital value of the button (LOW/HIGH)
int buttonState = digitalRead(buttonPin);    

// If the button has been pushed AND
// If the button wasn't pressed before AND
// IF there was not already a measurement running to determine how long the button has been pressed
if (buttonState == HIGH && buttonStatePrevious == LOW && !buttonStateLongPress) {
  buttonLongPressMillis = currentMillis;
  buttonStatePrevious = HIGH;
  //Serial.println("Button pressed");
}

// Calculate how long the button has been pressed
buttonPressDuration = currentMillis - buttonLongPressMillis;

// If the button is pressed AND
// If there is no measurement running to determine how long the button is pressed AND
// If the time the button has been pressed is larger or equal to the time needed for a long press
if (buttonState == HIGH && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
  buttonStateLongPress = true;
  //Serial.println("Button long pressed");
  Serial.print("-");
  digitalWrite(outPin, HIGH);
    tone(IR, 38000, Long);
    delay(Long);
    digitalWrite(outPin, LOW);
}
  
// If the button is released AND
// If the button was pressed before
if (buttonState == LOW && buttonStatePrevious == HIGH) {
  buttonStatePrevious = LOW;
  buttonStateLongPress = false;
  //Serial.println("Button released");

  // If there is no measurement running to determine how long the button was pressed AND
  // If the time the button has been pressed is smaller than the minimal time needed for a long press
 
  if (buttonPressDuration < minButtonLongPressDuration) {
    //Serial.println("Button pressed shortly");
    Serial.print(".");
    digitalWrite(outPin, HIGH);
    tone(IR, 38000, Short);
    delay(Short);
    digitalWrite(outPin, LOW);
  }
}

// store the current timestamp in previousButtonMillis
previousButtonMillis = currentMillis;

```

}

if (Enter == HIGH){  
delay(150);  
Enter\_morse();  
}

}

void loop() {

currentMillis = millis(); // store the current time  
readButtonState(); // read the button state

}

void Enter\_morse(){  
Serial.println("");  
}

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [April 28, 2022, 9:45am UTC](https://forum.arduino.cc/t/morse-code-decoder-problems/986061/2 "2022-04-28T09:45:20Z")

</div>

These words "arduino morse code decoder" in an internet searchie thingy produce a number of results.  
Such as

> **[GitHub - garrysblog/Arduino-Morse-Code-Decoder: Arduino sketch to convert...](https://github.com/garrysblog/Arduino-Morse-Code-Decoder)**
>
> Arduino sketch to convert morse code into plain text - GitHub - garrysblog/Arduino-Morse-Code-Decoder: Arduino sketch to convert morse code into plain text

> **[Arduino Morse Decoder](https://www.instructables.com/Arduino-Morse-Decoder/)**
>
> Arduino Morse Decoder: This device reads hand keyed Morse code from a signal key (or an ordinary switch) and translates it to plain text and show the Morse code you're keying on an LCD. I use it to train my own Morse keying skills.
> The decoder...

and more.

Also by typing the words "morse code" into this thing,  
 ![](https://europe1.discourse-cdn.com/arduino/original/4X/9/1/1/911747022edf1c0042b81f449cc2d7fb2b6293f3.jpeg)  
one can find links to numerous morse code projects that have solutions.

---

<div class="post-metadata">

**Author:** ![sneakyturtle-05](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sneakyturtle-05/32/774641_2.png) [@sneakyturtle-05](https://forum.arduino.cc/u/sneakyturtle-05)\
**Post date:** [April 28, 2022, 9:58am UTC](https://forum.arduino.cc/t/morse-code-decoder-problems/986061/3 "2022-04-28T09:58:12Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 25, 2022, 9:58am UTC](https://forum.arduino.cc/t/morse-code-decoder-problems/986061/4 "2022-10-25T09:58:43Z")

</div>

Closed on October 25, 2022, 9:58am UTC

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