Using Keypad : Clear held buttons if button is no longer being held.

Hey everyone,

I'm working on a project that uses the keypad library. Attachment of project below. It's a logic puzzle in which the players need to place the wires in the correct slots. When all wires are correct a signal is sent to another Arduino. My problem is with the wires being removed.

It takes most people several attempts of changing the location of the wires, but my code isn't built for it. If each wire is taken out and placed back into a different location, it will eventually be right because it remembers any correct input. I need it to immediately forget a wire being correct if the wire is removed from the correct location.

Jason

//POLES PUZZLE. DATE: 04AUG2018 BUILT FOR UNO.

#include <Keypad.h> //Library to build poles matrix //COM 13
const byte ROWS = 6; //Six upper poles
const byte COLS = 6; //Six lower poles

// We will use a grid configuration to define all possible pole connections
char poles[ROWS][COLS] = {
  {'A', 'B', 'C', 'D', 'E', 'F'}, //9, A1 White
  {'G', 'H', 'I', 'J', 'K', 'L'}, //8, A0 yellow
  {'M', 'N', 'O', 'P', 'Q', 'R'}, //7, A2 orange
  {'S', 'T', 'U', 'V', 'W', 'X'}, //6, A5 grey
  {'Y', 'Z', '0', '1', '2', '3'}, //5, A4 blue
  {'4', '5', '6', '7', '8', '9'} //4, A3 red
};

byte rowPins[ROWS] = {9, 8, 7, 6, 5, 4}; //upper poles
byte colPins[COLS] = {A0, A1, A2, A3, A4, A5}; //lower poles

//initialize Pole Connectors
Keypad keypad = Keypad( makeKeymap(poles), rowPins, colPins, ROWS, COLS);

int music1 = 13; //Sends signal to VS1053 for LockDown message
byte white;
byte yellow; //B,G,O,X,2,7
byte orange;
byte grey;
byte blue1;
byte red1;

void setup() {
  Serial.begin(9600);
  Serial.println("It has begun");
  pinMode(music1, OUTPUT);
  digitalWrite(music1, LOW); // Stop signal to VS1053 for message
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop() {
  keypad.getKeys(); { // If keypad buttons are active
    unLock();
  }
}

void keypadEvent(KeypadEvent key) {
  switch (keypad.getState()) {
    case HOLD:
      switch (key) {
        case'B': white = HIGH; break;
        case'G': yellow = HIGH; break;
        case'O': orange = HIGH; break;
        case'X': grey = HIGH; break;
        case'2': blue1 = HIGH; break;
        case'7': red1 = HIGH; break;
      }
     }
    }

void unLock() {
  int closed;

  Serial.print("white: "); //For testing to show if the wire is correct
  Serial.println(white);

  Serial.print("yellow: ");
  Serial.println(yellow);

  Serial.print("orange: ");
  Serial.println(orange);

  Serial.print("grey: ");
  Serial.println(grey);

  Serial.print("blue: ");
  Serial.println(blue1);

  Serial.print("red: ");
  Serial.println(red1);

  if ((white == HIGH) && (yellow == HIGH) && (orange == HIGH) //If all are correct, signal is sent.
      && (grey == HIGH) && (blue1 == HIGH) && (red1 == HIGH)) {
    digitalWrite(music1, HIGH); //Pin 13
    Serial.println("Open");
    delay(1000);
    digitalWrite(music1, LOW);
    //  delay(500000);
    // closed = closed +1;
  }

  if (closed >= 1)
  {
    Serial.println("Closed");
    digitalWrite(music1, LOW);
  }
}

polesFront.jpg

I need it to immediately forget a wire being correct if the wire is removed from the correct location.

How will you know that that has happened?

  keypad.getKeys(); { // If keypad buttons are active
    unLock();
  }

What's with the useless curly braces? What is the point of calling getKeys() if you don't care about what it returns?

What does placing or removing wires have to do with the keypad library?

You aren't really letting people connect wires at run time to the Arduino, are you? Plan on getting a new one, if you are.

Hey Paul,

Thanks for the reply. To answer your first question: I know that this happens because of the current Serial output that I am monitoring. If I plug in five out of the six wires in the correct location and those wires out, the Serial will still show me that all wires are "1" and it is waiting for the final correct wire to be inserted instead of it displaying "0" on all spots.

  1. *"What's with the useless curly braces? What is the point of calling getKeys() if you don't care about what it returns?" *
  • I thought it had to be in the sketch, my mistake, I'll remove them.
  1. What does placing or removing wires have to do with the keypad library?
  • I'm using the "HOLD" command. The keypad library is being using as a way to ensure the wires are in the correct location, just as if you were holding a key on a keypad.
  1. You aren't really letting people connect wires at run time to the Arduino, are you? Plan on getting a new one, if you are.

-Sorry, I don't understand your confusion with this part of my project. It has been running well for over a year using the same Arduino, I'm not sure why I would have to get a new one, but I would love to know more.

As hard as I tried, I think I didn't present my issue well. How can I get the Arduino to constantly check if a key is being held.

Any direction would be awesome, I don't need the code. I just need a direction to travel.

regards,

Jason

How can I get the Arduino to constantly check if a key is being held.

If, on any given pass through loop(), a key is being pressed, and it was being pressed last time, then it is, by definition, being held, at least for the amount of time between two iterations of loop().

If the state of the pin on this pass through loop() is what you consider released, and on the last pass it was what you consider pressed (the actual values depend on whether the internal pullup resistors are being used and/or how the external resistors are wired (pull up or pull down)), then the key became released, and you could remove it from the list of pressed keys.