Hi Thanks for your help, My code:
Note: The TM1637 Display isn’t connected anymore.
Everything works fine, but instead of “checking” the sequence after 7 inputs I want to check the sequence every time a button is pressed. So after a button on the keypad is pressed, the Arduino checks if the last 7 buttons == code
// INCLUDES
// 4-digit 7-segment LED Display library, download from https://github.com/avishorp/TM1637
#include <TM1637Display.h>
// Keypad library
#include <Keypad.h>
// DEFINES
#define DEBUG
// CONSTANTS
// Define the characters on the keypad layout
const char keys[4][4] = {
{'1', '2', '3', 'a'},
{'4', '5', '6', 'b'},
{'7', '8', '9', 'c'},
{'*', '0', '#', 'd'}
};
// Row pins
const byte keypadRowPins[4] = {6, 7, 8, 9};
// Column pins
const byte keypadColPins[4] = {2, 3, 4, 5};
// Pins which the relay modules are connected to
const byte relayPins[] = {10, 11, 12, 13};
// Clock pin for the display
const byte displayClockPin = A0;
// Data pin for the display
const byte displayDataPin = A1;
// GLOBALS
// Create a keypad input class from definitions above
Keypad keypad = Keypad( makeKeymap(keys), keypadRowPins, keypadColPins, 4, 4 );
// Create a display object, specifying pin parameters (Clock pin, Data pin)
TM1637Display display(displayClockPin, displayDataPin);
// Record the code which the user has entered
char data[] = " ";
// What position through the code is being entered?
int sequenceNum = 0;
// Initial setup
void setup() {
// Initialise serial communication if required for debugging
#ifdef DEBUG
Serial.begin(9600);
#endif
// Set brightness
display.setBrightness(0x0c);
// Initialise relay pins
for (int i = 0; i < 4; i++) {
digitalWrite(relayPins[i], LOW);
pinMode(relayPins[i], OUTPUT);
}
}
// Main Program Loop
void loop() {
// Get the keypad input this frame
char key = keypad.getKey();
// Has a key been pressed?
if (key) {
#ifdef DEBUG
// Log it to the serial output
Serial.println(key);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
#endif
// Set the current position of the code sequence to the key pressed
data[sequenceNum] = key;
// Increment the counter
sequenceNum++;
// Update the display to reflect the current sequence
// If the player has entered all 4 digits of a code
if (sequenceNum == 7) {
#ifdef DEBUG
Serial.print(F("Code entered: "));
// Log the whole code to the serial output
Serial.println(data);
#endif
// Take action based on the code entered
if (strcmp(data, "71053ca") == 0) {
digitalWrite(relayPins[0], HIGH);
Serial.println("valt!");
delay(2000);
digitalWrite(relayPins[0], LOW);
Serial.println("valt!");
delay(2000);
digitalWrite(13, HIGH);
}
// If none of the conditions above have matched, it's an unknown code
else {
// FLash the display
Serial.println("Code Fout");
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
delay(300);
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
delay(300);
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
delay(300);
}
// Clear the data array
memset(data, 0, sizeof(data));
sequenceNum = 0;
}
}
}