# Need some help coding

**URL:** <https://forum.arduino.cc/t/need-some-help-coding/597463>\
**Category:** Programming\
**Created:** [June 17, 2019, 3:29pm UTC](https://forum.arduino.cc/t/need-some-help-coding/597463 "2019-06-17T15:29:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bonecrusher05](https://avatars.discourse-cdn.com/v4/letter/b/839c29/32.png) [@Bonecrusher05](https://forum.arduino.cc/u/Bonecrusher05)\
**Post date:** [June 17, 2019, 3:29pm UTC](https://forum.arduino.cc/t/need-some-help-coding/597463/1 "2019-06-17T15:29:38Z")

</div>

Hello, I am a beginner and am trying to create a morse decoder and have run into a problem with the code. My plan is for a button to be pressed to type out Morse code and for the translation of the code to show up on my computer, but with my current code, all that shows up is the alphabet printed out despite how the button is pressed. As I said I am a beginner so the code is probably inefficient and contains several errors so any help would be very much appreciated.

Here is the code:

```cpp
String Morse, Dash, Dot, Space;

int switchState = 0;

unsigned long btime = 0;

unsigned long mtime = 0;

void setup() {

Serial.begin(9600);
pinMode(2, INPUT);
Morse = String("");
Dash = String("-");
Dot = String(".");
Space = String("");

}

void loop() {
 
unsigned long  overalltime = millis();
  
switchState = digitalRead(2);

if (switchState == HIGH) {
  btime = overalltime;
  }

if (switchState == LOW) {
  mtime = overalltime - btime;
  }
  
  if ((mtime > 1000) && (mtime <2000)) {
    (Morse + Dash); //this is a dash
  }
  if (mtime < 1000) {
    (Morse + Dot); //this is a dot
  }
  delay(1);
  btime = overalltime;
  if (overalltime - btime > 2000) {
    Morse = Space; //this is a space between letters
  }

  

if (Morse = ".-") {
  Serial.print("A");
  delay(500);
}
if (Morse = "-...") {
  Serial.print("B");
  delay(500);
}
if (Morse = "-.-.") {
  Serial.print("C");
  delay(500);
}
if (Morse = "-..") {
  Serial.print("D");
  delay(500);
}
if (Morse = ".") {
  Serial.print("E");
  delay(500);
}
if (Morse = "..-.") {
  Serial.print("F");
  delay(500);
}
if (Morse = "--.") {
  Serial.print("G");
  delay(500);
}
if (Morse = "....") {
  Serial.print("H");
  delay(500);
}
if (Morse = "..") {
  Serial.print("I");
  delay(500);
}
if (Morse = ".---") {
  Serial.print("J");
  delay(500);
}
if (Morse = "-.-") {
  Serial.print("K");
  delay(500);
}
if (Morse = ".-..") {
  Serial.print("L");
  delay(500);
}
if (Morse = "--") {
  Serial.print("M");
  delay(500);
}
if (Morse = "-.") {
  Serial.print("N");
  delay(500);
}
if (Morse = "---") {
  Serial.print("O");
  delay(500);
}
if (Morse = ".--.") {
  Serial.print("P");
  delay(500);
}
if (Morse = "--.-") {
  Serial.print("Q");
  delay(500);
}
if (Morse = ".-.") {
  Serial.print("R");
  delay(500);
}
if (Morse = "...") {
  Serial.print("S");
  delay(500);
}
if (Morse = "-") {
  Serial.print("T");
  delay(500);
}
if (Morse = "..-") {
  Serial.print("U");
  delay(500);
}
if (Morse = "...-") {
  Serial.print("V");
  delay(500);
}
if (Morse = ".--") {
  Serial.print("W");
  delay(500);
}
if (Morse = "-..-") {
  Serial.print("X");
  delay(500);
}
if (Morse = "-.--") {
  Serial.print("Y");
  delay(500);
}
if (Morse = "--..") {
  Serial.print("Z");
  delay(500);
}
else {
  Serial.print("");
}
}

```

---

<div class="post-metadata">

**Author:** ![wildbill](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wildbill/32/1126438_2.png) [@wildbill](https://forum.arduino.cc/u/wildbill)\
**Post date:** [June 17, 2019, 3:48pm UTC](https://forum.arduino.cc/t/need-some-help-coding/597463/2 "2019-06-17T15:48:46Z")

</div>

You're trying to accumulate dashes into your Morse String here:

```cpp
    (Morse + Dash); //this is a dash

```

But it should be like this:

```cpp
    Morse=Morse + Dash; //this is a dash

```

Same for dots below.

You're trying to compare Morse with ".\_":

```cpp
if (Morse = ".-") {

```

But comparison needs == rather than =

---

<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:** [June 17, 2019, 3:50pm UTC](https://forum.arduino.cc/t/need-some-help-coding/597463/3 "2019-06-17T15:50:09Z")

</div>

Suggestions.

First, get this to work without the button. Read data from the serial and convert. Once you get that logic in place, then add the button.

This link looks almost like what you want.

> **[Arduino Morse Code Machine](https://www.hackster.io/vladakrsmanovic/arduino-morse-code-machine-709cc5)**
>
> A simple Arduino Morse code machine with a button, LED, and a buzzer that translates your input into English while you are typing. By Vlada Krsmanovic.

---

<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:** [May 5, 2021, 11:53pm UTC](https://forum.arduino.cc/t/need-some-help-coding/597463/4 "2021-05-05T23:53:15Z")

</div>

Closed on May 5, 2021, 11:53pm UTC


