Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Key.h>
- #include <Keypad.h>
- /* Keypadtest.pde
- *
- * The first step is to connect your keypad to the
- * Arduino using the pin numbers listed below in
- * rowPins[] and colPins[]. If you want to use different
- * pins then you can change the numbers below to
- * match your setup.
- *
- */
- #include <Keypad.h>
- String code = "1235"; //The code
- String userInput = ""; //Log of what the user has pressed
- boolean blinking = false; //Check if Arduino should be blinking
- int delayTime = 125; //Delay between blinks
- int maxBlinks = 8; //Max amount of blinks
- int port; //Port in which it should blink. Port 4 is correct and 5 incorrect
- const byte ROWS = 4; // Four rows
- const byte COLS = 3; // Three columns
- // Define the Keymap
- char keys[ROWS][COLS] = {
- {'1','2','3'},
- {'4','5','6'},
- {'7','8','9'},
- {'*','0','#'}
- };
- // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
- byte rowPins[ROWS] = { 6, 7, 8, 9 };
- // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
- byte colPins[COLS] = { 10, 11, 12 };
- // Create the Keypad
- Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- #define ledpin 13
- void setup(){
- pinMode(ledpin,OUTPUT);
- pinMode(4, OUTPUT);
- pinMode(5, OUTPUT);
- digitalWrite(ledpin, HIGH);
- Serial.begin(9600);
- Serial.print("Please enter the secret code: ");
- }
- void loop(){
- /*
- * If all the characters has been pressed, it
- * should be blinking.
- */
- if(blinking){
- blinking = false;
- for(int timesBlinked = 0; timesBlinked < maxBlinks; timesBlinked++){
- delay(delayTime);
- digitalWrite(port, HIGH);
- delay(delayTime);
- digitalWrite(port, LOW);
- }
- userInput = "";
- Serial.print("Please enter the secret code: ");
- }
- /*
- * Waiting for keypresses
- */
- else{
- char key = kpd.getKey();
- if(key) // Check for a valid key.
- {
- //Something something
- if(key == '*'){
- }
- //Resets the input
- else if(key == '#'){
- userInput = "";
- Serial.println("");
- Serial.print("Please enter the secret code: ");
- }
- //Logs the user input
- else{
- Serial.print(key);
- userInput += key;
- }
- //If the user has pressed the code
- if(userInput.length() == code.length()){
- Serial.println("");
- //The code is correct
- if(userInput.equals(code)){
- Serial.println("Correct!");
- port = 4;
- }
- //The code is incorrect
- else{
- Serial.println("Wrong!");
- port = 5;
- }
- blinking = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement