【Arduino】利用4*4矩阵键盘来实现输入密码功能

弄清楚类比于手机锁屏密码的模式,使用Arduino利用4*4矩阵键盘来实现输入密码功能,利用EEPROM实现保存密码功能。

本次完成的内容需要的材料:

Arduino UNO

一块4*4矩阵键盘

一个LED灯

导线(若干)

线路图:

实验所用的代码:

/* @file CustomKeypad.pde

|| @version 1.0

|| @author Alexander Brevig

|| @contact alexanderbrevig@gmail.com

||

|| @description

|| | Demonstrates changing the keypad size and key values.

|| #

*/

#include "Keypad.h"

#include <EEPROM.h>



const byte ROWS = 4; //four rows

const byte COLS = 4; //four columns

//define the cymbols on the buttons of the keypads

char hexaKeys[ROWS][COLS] = {

  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}

};

byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad



//initialize an instance of class NewKeypad

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

int pass;

void setup(){

  Serial.begin(115200);

  pinMode(13,OUTPUT); 

  pass = EEPROM.read(0);

   Serial.println(pass);

  delay(500);

}

  

void loop(){

  char customKey = customKeypad.getKey();

if (customKey){

    Serial.println(customKey);

    digitalWrite(13, LOW);

  }

  if(customKey == 'A')

  {

    int  a,b,c,d,q,i=0;

        while(1)

        {

        

        char customKey = customKeypad.getKey();

        if (customKey){

    Serial.println(customKey);

  }

        if(i == 0&&customKey&&customKey!='#')

        a = (int)(customKey)-48;

        

    

        if(i == 1&&customKey)

        {

        b = (int)(customKey)-48;

        

        }

        if(i == 2&&customKey)

        c = (int)(customKey)-48;

        

        if(i == 3&&customKey){

        d = (int)(customKey)-48;

         

        }

        

        if(customKey == '*')

        a=b=c=d=q=i=0;    

        else if(customKey == '#')

        {

          q = a*1000+b*100+c*10+d;

         EEPROM.write(0,q);

         delay(100);

          pass = EEPROM.read(0);

          Serial.println(pass);

         break;

        }

        

        if(customKey == '0'||customKey == '1'||customKey == '2'||customKey == '3'||customKey == '4'||customKey == '5'||customKey == '6'||customKey == '7'||customKey == '8'||customKey == '9')

        {

          i++;

          if(i == 4)

          {i = 0;}

          }

        }

  }

        if(customKey == 'B')

  {

      int  a,b,c,d,q,i=0;

      char customKey = ' ';

        while(1)

        {

          

        char customKey = customKeypad.getKey();

        if (customKey){

    Serial.println(customKey);

  }

  

        if(i == 0&&customKey&&customKey!='#')

        a = (int)(customKey)-48;

        

    

        if(i == 1&&customKey)

        {

        b = (int)(customKey)-48;

        

        }

        if(i == 2&&customKey)

        c = (int)(customKey)-48;

        

        if(i == 3&&customKey){

        d = (int)(customKey)-48;

       

        }

        

        if(customKey == '0'||customKey == '1'||customKey == '2'||customKey == '3'||customKey == '4'||customKey == '5'||customKey == '6'||customKey == '7'||customKey == '8'||customKey == '9')

        {

          i++;

          if(i == 4)

          {i = 0;}

          }



        if(customKey == '*'){

        a=b=c=d=q=i=0;    

        }

        else if(customKey == '#')

        {

        Serial.println(a);

        Serial.println(b);

        Serial.println(c);

         Serial.println(d);

        

          q = a*1000+b*100+c*10+d;

         

           

          if(q == pass)

          {

            Serial.println("sucess");

             digitalWrite(13, HIGH);

              break;

          }

          else

          {

            a=b=c=d=q=i=0;  

           }

          

          }

        

        }

    }

  



}
跳至工具栏