Morse Code Project

I want my program to turn on output 9 when the setup triggers, but no matter what I do I cant seem to get it to work. I feel so dumb, what am I doing wrong here?

int tonePin = 2;
int toneFreq = 2500;
int ledPin = 13;
int buttonPin = 8;
int outputPin = 9;  // <-- Added for activating on ORDERS
int debounceDelay = 30;

int dotLength = 440; // dotLength = basic unit of speed in milliseconds
int dashLength = dotLength * 3;
int letterSpace = dotLength * 3;
int wordSpace = dotLength * 3.5;
float wpm = 1200. / dotLength;

int t1, t2, onTime, gap;
bool newLetter, newWord, letterFound, keyboardText;
int lineLength = 0;
int maxLineLength = 20;

char* letters[] = {
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
  ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
  "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
};

char* numbers[] = {
  "-----", ".----", "..---", "...--", "....-", //0-4
  ".....", "-....", "--...", "---..", "----." //5-9
};

String dashSeq = "";
char keyLetter, ch;
int i, index;
String morseInput = ""; // Track the incoming Morse code sequence
unsigned long lastInputTime = 0; // Track last input time for timeout
const unsigned long timeoutDuration = 20000; // 20 seconds timeout for new word

void setup() {
  delay(500);
  pinMode(ledPin, OUTPUT);
  pinMode(tonePin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(outputPin, OUTPUT);  // <-- Configure pin 6 as output

  Serial.begin(9600);
  Serial.println();
  Serial.println("-------------------------------");
  Serial.println("Morse Code decoder/encoder");
  Serial.print("Speed=");
  Serial.print(wpm);
  Serial.print("wpm, ");
  Serial.print("dot=");
  Serial.print(dotLength);
  Serial.println("ms");

  // Test the LED and tone
  tone(tonePin, toneFreq);
  digitalWrite(ledPin, HIGH);
  delay(2000);
  digitalWrite(ledPin, LOW);
  noTone(tonePin);
  delay(600);

  // Flash the word "HOWDY" in Morse code
  Serial.print("H .... ");
  flashSequence("...."); // H
  delay(wordSpace);

  Serial.print("O --- ");
  flashSequence("---"); // O
  delay(wordSpace);

  Serial.print("W .-- ");
  flashSequence(".--"); // W
  delay(wordSpace);

  Serial.print("D -.. ");
  flashSequence("-.."); // D
  delay(wordSpace);

  Serial.print("Y -.-- ");
  flashSequence("-.--"); // Y
  delay(wordSpace);

  Serial.println();
  Serial.println("-------------------------------");
  Serial.println("Click field in Serial Monitor,");
  Serial.println("type text and press Enter, or");
  Serial.println("Key in Morse Code to decode:");
  Serial.println("-------------------------------");

  newLetter = false;
  newWord = false;
  keyboardText = false;
}

void loop() {
  if (millis() - lastInputTime > timeoutDuration) {
    morseInput = "";
    Serial.println("\nTimeout! Waiting for new Morse code input...");
  }

  if (digitalRead(buttonPin) == LOW) {
    newLetter = true;
    newWord = true;
    t1 = millis();
    digitalWrite(ledPin, HIGH);
    tone(tonePin, toneFreq);
    delay(debounceDelay);

    while (digitalRead(buttonPin) == LOW) {
      delay(debounceDelay);
    }

    delay(debounceDelay);
    t2 = millis();
    onTime = t2 - t1;
    digitalWrite(ledPin, LOW);
    noTone(tonePin);

    if (onTime <= dotLength * 1.5) {
      dashSeq = dashSeq + ".";
    } else {
      dashSeq = dashSeq + "-";
    }

    lastInputTime = millis();
  }

  gap = millis() - t2;

  if (newLetter == true && gap >= letterSpace) {
    letterFound = false;
    keyLetter = 63;

    for (i = 0; i <= 25; i++) {
      if (dashSeq == letters[i]) {
        keyLetter = i + 65;
        letterFound = true;
        break;
      }
    }

    if (!letterFound) {
      for (i = 0; i <= 9; i++) {
        if (dashSeq == numbers[i]) {
          keyLetter = i + 48;
          letterFound = true;
          break;
        }
      }
    }

    Serial.print(keyLetter);

    if (!letterFound) {
      tone(tonePin, 100, 500);
    }

    morseInput += dashSeq + " ";
    dashSeq = "";

    if (morseInput == "--- .-. -.. . .-. ... ") {
      Serial.println("\nDetected 'ORDERS' in Morse code. Activating setup...");
      triggerSetup();
    }

    newLetter = false;
    lineLength = lineLength + 1;
  }

  if (lineLength >= maxLineLength) {
    Serial.println();
    lineLength = 0;
  }
}

void triggerSetup() {
  digitalWrite(outputPin, HIGH);  // <-- Turn on pin 6 when "ORDERS" is received

  // Flash "INITIATE BOMB SEQUENCE" in Morse
  Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
  Serial.print("N -. "); flashSequence("-."); delay(wordSpace);
  Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
  Serial.print("T - "); flashSequence("-"); delay(wordSpace);
  Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
  Serial.print("A .- "); flashSequence(".-"); delay(wordSpace);
  Serial.print("T - "); flashSequence("-"); delay(wordSpace);
  Serial.print("E . "); flashSequence("."); delay(wordSpace);

  Serial.print("B -... "); flashSequence("-..."); delay(wordSpace);
  Serial.print("O --- "); flashSequence("---"); delay(wordSpace);
  Serial.print("M -- "); flashSequence("--"); delay(wordSpace);
  Serial.print("B -... "); flashSequence("-..."); delay(wordSpace);

  Serial.print("S ... "); flashSequence("..."); delay(wordSpace);
  Serial.print("E . "); flashSequence("."); delay(wordSpace);
  Serial.print("Q --.- "); flashSequence("--.-"); delay(wordSpace);
  Serial.print("U ..- "); flashSequence("..-"); delay(wordSpace);
  Serial.print("E . "); flashSequence("."); delay(wordSpace);
  Serial.print("N -. "); flashSequence("-."); delay(wordSpace);
  Serial.print("C -.-. "); flashSequence("-.-."); delay(wordSpace);
  Serial.print("E . "); flashSequence("."); delay(wordSpace);

  Serial.println();
  Serial.println("-------------------------------");
  Serial.println("Click field in Serial Monitor,");
  Serial.println("type text and press Enter, or");
  Serial.println("Key in Morse Code to decode:");
  Serial.println("-------------------------------");
}

void flashSequence(char* sequence) {
  int i = 0;
  while (sequence[i] == '.' || sequence[i] == '-') {
    flashDotOrDash(sequence[i]);
    i++;
  }
}

void flashDotOrDash(char dotOrDash) {
  digitalWrite(ledPin, HIGH);
  tone(tonePin, toneFreq);

  if (dotOrDash == '.') {
    delay(dotLength);
  } else {
    delay(dashLength);
  }

  digitalWrite(ledPin, LOW);
  noTone(tonePin);
  delay(dotLength);
}

Possibly, in setup, set the output pin low after setting it's mode.
digitalWrite(outputPin, low);
You may also want to set it low again after setting it high, with some time period in between.

basically not supplying enough information.

You might want to look at this How to get the best out of this forum before you proceed any further.
We only know what you tell us, and without knowing what you have, and why you want to do this, we don't stand a chance.

for example you don't say what sort of Arduino you are using. Also a quick Look at your code shows you are using the String class, which is never a good idea.

2 Likes

This is only a comment on a line of code, but it shows that you are not paying attention to your code... which could be the cause.

1 Like

its just a comment on the code. It has no bearing on the code or its functionality

Yes, it does. You asked, "What am I doing wrong...?" and my answer was, is and will be; you allow visible, correctable, inaccuracies in your code. You are asking for help, given help, and dismiss the help. Typical.

Ask your classmate who just posted this same "howdy" sketch... oh, that was you...

Morse Code Project.

I got it to work in your other topic with the same subject.

Seems like you have been, are, and will be dismissing all help until someone writes your project code for you.

1 Like

not sure what is with the rude attitude. Nobody ever dismissed any help, I was just letting you know that comments have no bearing on the actual functionality of the code. I don't have any classmates as I am not a student, this is simply a fun project I am working on. I never have and never will expect anyone to write my code for me because again this is a project i'm doing for fun. So kindly take your unnecessary attitude elsewhere unless you actually have something helpful or constructive to contribute. Thanks

Because your previous thread was started as a new member, I am on this occasion going to spend a little time explaining. Part of the problem here I think is that you have started a new thread rather than continuing with the existing one which makes it difficult to follow the conversation and understand what help might have already been given. Please don't do that. Just continue with the story on the original thread. You have also posted exactly the same code as you did last time without any changes, so it looks like you have dismissed the previous suggestions that you received.

The error in the comment, is just that an error, but as pointed out, does show an inattention to detail. Such an oversight might have easily been made if, say, you changed the pin you were using, but rather than thanking the poster for spotting it and then correcting it, your response was to berate them. Don't you think they understand what a comment is for?

A lot of school/college kids come on here expecting the forum to just write code for them without putting in any effort into understanding the coding principles they are meant to be learning. Neither do they appreciate the free advice they are given. Many just disappear without any acknowledgement. You do not appear to have acknowledged any of the suggestions you have been given either on the earlier thread that you created so unfortunately your new thread with identical topic comes across that way as well.

I am hoping that this contribution is constructive enough to help you appreciate that volunteers are willing to freely offer help if the person receiving that help is willing to show appreciation for the help they are being given.

Flag it if you feel this way.

I showed you how to fix your code. Code that did not work because of something... big or small... I showed you the fix... that you dismissed... you call me rude. What a world you live in.

You will never tell me what to do or not do. I fixed your mess, and you dismissed even the littlest advice, and call me rude for pointing out the problem. What a world.

You didn’t show me a fix. You pointed out a comment on my code, which again, has no bearing on the functionality of the code. You should know that. You fixed nothing. You contributed nothing except a rude attitude. What a world. How typical of you. I will tell you what to do in regards communicating with me or communicating under my post. Thank you. Move along and have a nice day

Just in case you missed it.

https://forum.arduino.cc/t/morse-code-project/1403848/5

It was in your previous thread.

1 Like

Inaccurate comments show you do not observe your own code, all of it. You should know that. Maybe everyone who tried pointing this out to you got the same treatment from you, so they all just quit helping, making you think "sloppy" is just fine.

Never.

Fix your code how I said to fix it six days ago. It is that simple.

Thank you but the previous thread has already served its purpose. The new thread displays a new separate problem in which this person offered no help. I have managed to solve the problem on my own

Mark the other topic with the "solution" checkbox.

The thread remains open so I can get as much advice as I can so I can learn as much as I can. Again you can move along

Maybe going away like I told you to? Maybe go find something better to do? Maybe get a life? And I look down on people who enjoy programming electronics? Me? Who is programming electronics for fun? I’m looking down on them? Yeah that logic seems sturdy

Typical.

You expect to advance while you see no value in accuracy. One realization that you will need to understand before you get anywhere is that you, alone, broke the program, "it" didn't break itself, and you need to find where you broke the program. Inaccurate comments will give you, the reader, false information. That is where accuracy is imperative, even in the comments.. which you can clean up (or remove) without breaking the code further. Being offended by someone saying "your comments are inaccurate" is going to make "programming" more of a challenge than it should be.

This is not "your" forum. You are not the center.

Referring to post #1 of this topic (which is the same subject as your other topic)... work on button debounce and arrays....

int toneFreq = 2500;

2500Hz will become annoying, soon. Use low, natural frequencies, like 440 (dit), 220 (dah).
references for more ear-pleasing tones:

int debounceDelay = 30;

Button debounce will be near 100ms if only polling one button. When the processor is busy, lower debounce values can work more than fail. Your debounce method is strange.

int wordSpace = dotLength * 3.5;

Again, referring to your other topic with the same subject, this is also incorrect (you were using 9 before)... intra-word gap is ( dit * 7 ).

int index;

... is never used.

  Serial.begin(9600);

You will find Serial.begin(115200); more responsive.

  pinMode(outputPin, OUTPUT);  // <-- Configure pin 6 as output

Still wrong comment. outputPin is pin 9.

  Serial.print("H .... ");

You have ASCII value of letters and an array of Morse equivalents. Use 'A' = 65 and letters[0] to make your Morse. See "arrays" below...

  if (millis() - lastInputTime > timeoutDuration) {
    morseInput = "";
    Serial.println("\nTimeout! Waiting for new Morse code input...");
  }

This will trigger "time out waiting," always, after "HOWDY" and again after "INITIATE." Your HOWDY has 35 element times at 2.73 wpm taking over 30 seconds, with your timeout set at only 20 seconds (timeoutDuration) and "INITIATE" is well over two minutes... so everything becomes "waiting."

Use more-descriptive variables that are "searchable"...

int t1, t2;

...for example, searching for the variable "i" is impossible.

    while (digitalRead(buttonPin) == LOW) {
      delay(debounceDelay);
    }

    delay(debounceDelay);

This looks like your "button press"... What you want to look for is changes from HIGH (released) to LOW (pressed) rather than just LOW/pressed.

  if (previousState == HIGH && thisState = LOW)
  //    button was pressed

Then, you want to use a non-blocking timer (millis()) to count the time between the first syllable of the button press, and the debounce timeout. You set it at 30ms... which will still be bouncing, so move it up toward 100ms.

Read about arrays... https://docs.arduino.cc/language-reference/en/variables/data-types/array/

This code pile...

  // Flash "INITIATE BOMB SEQUENCE" in Morse
  Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
  Serial.print("N -. "); flashSequence("-."); delay(wordSpace);
  Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
  Serial.print("T - "); flashSequence("-"); delay(wordSpace);
  Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
  Serial.print("A .- "); flashSequence(".-"); delay(wordSpace);
  Serial.print("T - "); flashSequence("-"); delay(wordSpace);
  Serial.print("E . "); flashSequence("."); delay(wordSpace);

  Serial.print("B -... "); flashSequence("-..."); delay(wordSpace);
  Serial.print("O --- "); flashSequence("---"); delay(wordSpace);
  Serial.print("M -- "); flashSequence("--"); delay(wordSpace);
  Serial.print("B -... "); flashSequence("-..."); delay(wordSpace);

  Serial.print("S ... "); flashSequence("..."); delay(wordSpace);
  Serial.print("E . "); flashSequence("."); delay(wordSpace);
  Serial.print("Q --.- "); flashSequence("--.-"); delay(wordSpace);
  Serial.print("U ..- "); flashSequence("..-"); delay(wordSpace);
  Serial.print("E . "); flashSequence("."); delay(wordSpace);
  Serial.print("N -. "); flashSequence("-."); delay(wordSpace);
  Serial.print("C -.-. "); flashSequence("-.-."); delay(wordSpace);
  Serial.print("E . "); flashSequence("."); delay(wordSpace);

Could be this... (compile and run this code) missing wordSpace and flashSequence

char* letters[] = {
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
  ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
  "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
};

char words[] = {"INITIATE BOMB SEQUENCE"};
int wordssize = sizeof(words) / sizeof(words[0]);

void setup() {
  Serial.begin(115200);

  // Serial.print(words[1] - 'A');

  for (int i = 0; i < wordssize; i++) {
    Serial.print(words[i]);
    Serial.print(" ");
    Serial.print(letters[words[i] - 'A']); // or - 65
    // flashsequence
    // delay(wordspace)
    if (words[i] == ' ')
      Serial.println();
  }
}

void loop() {}

Ugh. So many flags.

@AnnieDoesCoding, do you still need help?