Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28

Thread: Arduino help please :)

  1. #21
    Senior Member Studio1's Avatar
    Join Date
    Jan 2008
    Location
    In the back of a 50 kW AM transmitter
    Posts
    1,675
    Thanks
    217
    Thanked 287 Times in 187 Posts
    Rep Power
    273
    Reputation
    2446

    Default

    Quote Originally Posted by ammlione View Post
    I see what you mean. I've just tried it.
    In my head is meant to work but in code it did not, I guess my head simulator is not working as it should

    In any case, I think it is the way we wrote it, this works as expected:

    if (key != NO_KEY){
    if (key == secretCode[position]) {
    position++;
    } else{
    position=0;
    }

    }

    You can figure out where it goes

    Cheers
    Thanks - I gave it a shot, put it where I thought it should go but it doesn't.
    As in it doesn't change from red to green when the correct code is entered.
    This is what I ended up with:

    #include <Keypad.h>

    char* secretCode = "123456";
    int position = 0;
    int CountdownTotal=10; //how many seconds to countdown
    int CountDown2Go = 0;

    const byte rows = 4;
    const byte cols = 3;
    char keys[rows][cols] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
    };

    //byte rowPins[rows] = {4, 5, 6, 7}; //Original keypad pinout
    //byte colPins[cols] = {1, 2, 3}; //original keypad pinout
    byte rowPins[rows] = {7, 6, 5, 4};
    byte colPins[cols] = {3, 2, 12};

    Keypad keypad = Keypad(makeKeymap(keys),
    rowPins, colPins,
    rows, cols);

    int redPin = 8;
    int greenPin = 9;
    int yellowPin = 10;

    void setup()
    {
    Serial.begin(115200); // Any baud rate should work
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(yellowPin, OUTPUT);
    setLocked(true);
    }

    void loop()
    {
    char key = keypad.getKey();
    if (key == '*') {
    position = 0;
    setLocked(true);
    }
    if ( key == '#') {
    hashCountDown();
    }

    if (key == secretCode[position]) {
    position++;
    }

    if (position == 6) {
    setLocked(false);
    }
    if (key != NO_KEY) {
    if (key == secretCode[position]) {
    position++;
    } else{
    position=0;
    }

    }


    {
    Serial.println(key);
    }
    delay(50);
    }

    void setLocked(int locked)
    {
    if (locked) {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    }
    else {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
    }
    }

    void hashCountDown()
    {

    while (CountDown2Go < CountdownTotal){
    CountDown2Go++;
    digitalWrite(yellowPin, HIGH);
    delay(500);
    digitalWrite(yellowPin, LOW);
    delay(500);
    }
    CountDown2Go=0;
    setLocked(true);
    }


    In the shades of UB40, where did I go wrong?
    Last edited by Studio1; 30-09-21 at 04:36 PM.



  • #22
    Junior Member
    Join Date
    Jul 2010
    Posts
    71
    Thanks
    351
    Thanked 44 Times in 31 Posts
    Rep Power
    157
    Reputation
    800

    Default

    In this section:
    if (key == secretCode[position]) {
    position++;
    }

    if (position == 6) {
    setLocked(false);
    }
    if (key != NO_KEY) {
    if (key == secretCode[position]) {
    position++;
    } else{
    position=0;
    }

    }
    this part at the start needs to be removed :
    ]if (key == secretCode[position]) {
    position++;
    }

    because the "key==secretCode[position]" it is there twice otherwise


    I've updated "my" version here

  • The Following User Says Thank You to ammlione For This Useful Post:

    Studio1 (01-10-21)

  • #23
    Senior Member Studio1's Avatar
    Join Date
    Jan 2008
    Location
    In the back of a 50 kW AM transmitter
    Posts
    1,675
    Thanks
    217
    Thanked 287 Times in 187 Posts
    Rep Power
    273
    Reputation
    2446

    Default

    Quote Originally Posted by ammlione View Post
    In this section:


    this part at the start needs to be removed :
    ]if (key == secretCode[position]) {
    position++;
    }

    because the "key==secretCode[position]" it is there twice otherwise


    I've updated "my" version here
    D'oh! I should have seen that.
    My analog mind is having a hard time trying to follow the 'logic' of the programming environment.
    I had a look at your updated version and noticed it still responds to a number such as '4123456'.
    I think what we are missing is something that looks at the length of the password to see if it is correct, too short or too long, and then that the numbers match.

  • #24
    Premium Member
    wotnot's Avatar
    Join Date
    Nov 2019
    Posts
    1,803
    Thanks
    759
    Thanked 1,633 Times in 863 Posts
    Rep Power
    739
    Reputation
    32670

    Default

    [QUOTE=Studio1;849554
    I think what we are missing is something that looks at the length of the password to see if it is correct, too short or too long, and then that the numbers match.[/QUOTE]


  • The Following User Says Thank You to wotnot For This Useful Post:

    ammlione (04-10-21)

  • #25
    Junior Member
    Join Date
    Jul 2010
    Posts
    71
    Thanks
    351
    Thanked 44 Times in 31 Posts
    Rep Power
    157
    Reputation
    800

    Default

    ok, I've put some comments on the previous code to help you follow it (still only needs the full password in a row to unlock, regardless the number of failed characters before the real password)


    I've added more comments to this variation that will only check for the first 6 chars of the secret code and ignore the rest until the "*" is pressed again (or the "#" to activate the countdown)

    Good luck

    Edit: I just realized that the links are the same, because I kept overwriting the file, sorry so you only get to see the last version with the comments
    Last edited by ammlione; 05-10-21 at 09:24 PM.

  • The Following User Says Thank You to ammlione For This Useful Post:

    Studio1 (12-10-21)

  • #26
    Senior Member Studio1's Avatar
    Join Date
    Jan 2008
    Location
    In the back of a 50 kW AM transmitter
    Posts
    1,675
    Thanks
    217
    Thanked 287 Times in 187 Posts
    Rep Power
    273
    Reputation
    2446

    Default

    Quote Originally Posted by ammlione View Post
    ok, I've put some comments on the previous code to help you follow it (still only needs the full password in a row to unlock, regardless the number of failed characters before the real password)


    I've added more comments to this variation that will only check for the first 6 chars of the secret code and ignore the rest until the "*" is pressed again (or the "#" to activate the countdown)

    Good luck

    Edit: I just realized that the links are the same, because I kept overwriting the file, sorry so you only get to see the last version with the comments
    Houston, we have lift-off!
    I have looked at the code, and if I thought I was confused before, I am more so now! Ha ha, this old dog is struggling to learn new tricks.
    Thank you indeed for being so patient, and working through this for me.
    Even with the best of intentions I don't think I could have worked all of that out myself.

  • #27
    Junior Member
    Join Date
    Jul 2010
    Posts
    71
    Thanks
    351
    Thanked 44 Times in 31 Posts
    Rep Power
    157
    Reputation
    800

    Default

    Keep trying mate,
    It is not the easiest thing to learn, but it is not impossible either.
    I don't know everything either, just enough to get in trouble

    Let us know when you get stuck again

  • The Following User Says Thank You to ammlione For This Useful Post:

    Studio1 (19-10-21)

  • #28
    Senior Member Studio1's Avatar
    Join Date
    Jan 2008
    Location
    In the back of a 50 kW AM transmitter
    Posts
    1,675
    Thanks
    217
    Thanked 287 Times in 187 Posts
    Rep Power
    273
    Reputation
    2446

    Default

    Quote Originally Posted by ammlione View Post
    Keep trying mate,
    It is not the easiest thing to learn, but it is not impossible either.
    I don't know everything either, just enough to get in trouble

    Let us know when you get stuck again
    Thanks again, and for sure I will post up if I get stuck with something!

  • The Following User Says Thank You to Studio1 For This Useful Post:

    ammlione (20-10-21)

  • Page 2 of 2 FirstFirst 12

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •