Working on a morse code lock

I am working on a project. A Morse machine that if a certain Morse code is pressed a electromagnet is engaged. An indicator LED will tell you when the button is being pressed as indicated in the script bellow. I am uncertain how to merge the first part of the script regarding Morse code, with the ladder part concerning the password check. What am i doing wrong here?

Thanks in advance.

Also I get an error regarding " ‘readio’ was not declared in this scope" at line 31. How do i fix this?

#include <Password.h>


Password password = Password( "0000" ); //password to unlock

unsigned long signal_len,t1,t2;   //time for which button is pressed
int inputPin = 2;                 //input pin for push button
int ledPin = 4;                   //outpu pin for LED
int magnet = 11;                  //Output for electro magnet
String code = "";                 //string in which one alphabet is stored

void setup()

{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}

void loop()
{
NextDotDash:
  while (digitalRead(inputPin) == HIGH) {}
  t1 = millis();                            //time at button press
  digitalWrite(ledPin, HIGH);               //LED on while button pressed
  while (digitalRead(inputPin) == LOW) {}
  t2 = millis();                            //time at button release
  digitalWrite(ledPin, LOW);                //LED off on button release
  signal_len = t2 - t1;                     //time for which button is pressed
  if (signal_len > 50)                      //to account for switch debouncing
  {
    code += readio();                       //function to read dot or dash
  }
  while ((millis() - t2) < 500)           //if time between button press greater than 0.5sec, skip loop and go to next alphabet
  {     
    if (digitalRead(inputPin) == LOW)
    {
  }
  convertor();                          //function to decipher code into alphabet
}


char readio()
{
  if (signal_len < 600 && signal_len > 50)
  {
    return '.';                        //if button press less than 0.6sec, it is a dot
  }
  else if (signal_len > 600)
  {
    return '-';                        //if button press more than 0.6sec, it is a dash
  }
}

void convertor()


{
  static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
                             ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E"
                            };
  int i = 0;
  if (code == ".-.-.-")
  {
    Serial.print(".");        //for break
  }
  else
  {
    while (letters[i] != "E")  //loop for comparing input code with letters array
    {
      if (letters[i] == code)
      {
        Serial.print(char('A' + i));
        break;
      }
      i++;
    }
    if (letters[i] == "E")
    {
      Serial.println("");  //if input code doesn't match any letter, error
    }
  }
  code = "";                            //reset code to blank string
}

void checkPassword(){
  
if (password.evaluate()){  //if password is right open
    
    Serial.println("Accepted");
    Serial.write(254);delay(10);
    //Add code to run if it works    
        digitalWrite(magnet, HIGH);//turn on
    delay(15000); //wait 15 seconds
    digitalWrite(magnet, LOW);// turn off
    
    
}else{
    Serial.println("Denied"); //if passwords wrong keep locked
    Serial.write(254);delay(10);
    //add code to run if it did not work
    digitalWrite(ledPin, HIGH); //turn on
    delay(500); //wait 5 seconds
    digitalWrite(ledPin, LOW);//turn off
    
}
}

Can’t help with code we can’t see. Please read the “how to use the forum” stickies to see how to format and post code.

That's my problem i can't get started properly because i can't seem to wrap my head around a duel purpose button or how to code the password

Did google not turn up any morse code projects for the arduino?

it did, but not regarding using a predetermined code to open a lock. I imagine something along the lines of a one button keypad :stuck_out_tongue: tried to find something along those lines with little success. i found a code regarding double-clicking and a hold event but it ha to be too precise. if the user was to fast it wouldn’t be worth much.

That’s where i start hitting a wall. A way to make a keypad with only one button and a fairly long code.

What I'd be looking for is a library that takes in pulses, and interprets those pulses as Morse code. You may have to amend it a bit for speed of input, but as long as you get a string of letters out of it (I assume you're expecting the user to input some wording in Morse) you can simply compare that to your password.

Or use pulseIn() on the pin your keypad is connected to, and some smarts to determine short and long pulses.
Take the pulses, if less than certain time it's a dot, if longer than another time it's a dash.
Make it smarter, and base the time length on the length of the pulses received.
Check length of pauses between pulses to see whether it's a completed letter.

Morse code decoder.

Hmm the last link was useful how ever i will not be able to have a laptop with me, i might be able to play around with it a bit. still have to figure out how to make it activate a pin by a certain code.

No, it shouldnt have italics.
Or goto.

So ditch the "Goto NextDotDash;" and change the font got ya xD
I can see i fogot to remove ther myservo.write aswell. That's probably changed to another kind of lock.

Even better for code readability are the venerable code tags.

Another thing: you mention you're using the internal pull up to simplify the circuit; I guess that means the button pulls the signal low in which case have to reverse HIGH and LOW as HIGH is the rest state and LOW is the active state.

That goto takes me back 30 years - that's just about the last time I've used it or seen it being used (in good old BASIC that was a common way to jump around your program).

The internal pullup might be a remnant from some of the code i reverse engineered to make this script. I have taken some parts from some scrips in order to speed up the process

But am i safe to assume that it will read the morse code generated by the first part of the code as password or do i have to write something else to combine the two?

Welcome to the Forum. Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Your cooperation in this regard will greatly increase the chances of getting help here.

agnonthewolf:
The internal pullup might be a remnant from some of the code i reverse engineered to make this script. I have taken some parts from some scrips in order to speed up the process

\

Not updating comments to match your situation can come back to bite you big time. You're confusing yourself with it, when you come back to the code later. It also confuses other people that know nothing about your code or what hardware it's attached to, and that rely on the comments for understanding of what's going on.

Read and understood. I am not sure if i can take down this entire topic and redo a better one, but i will make a new updated topic that follows the Forum rules better. Sorry for the inconvenience have a nice day.

Where is your include file?

What include file am i missing aarg?

I would like to hear some feedback, i have revisited parts of the original code. Let me hear what i am missing.

, i have revisited parts of the original code.

Didn't you think others would like to see what you revisited?