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