为代码的不同部分设置第二个密码

时间:2017-11-17 23:48:28

标签: c++ arduino

我的门上有电动门锁和外面的键盘。我有一段代码可以工作并解锁门。但是现在我要做的就是要有一个关闭车库门的第二个代码。解锁门[我有一个引脚连接到一个继电器,激活车库门遥控器上的一个按钮]一切个人工作,但我不知道如何在那里有两个代码(我有第二个代码标记为妈妈,因为她将是那个使用它的人)

/*
||  Simple Password Entry Using Matrix Keypad
||  4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
||
*/


//* is to validate password   
//# is to reset password attempt

/////////////////////////////////////////////////////////////////

//#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip

Password password = Password( "9952" );
Password mum = Password( "1234" );

const byte ROWS = 4; // Four rows
const byte COLS = 4; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = { 9,8,7,6 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5,4,3,2, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

int RedLed = 10;
int GreenLed = 11;
int YellowLed = 12;
int Door = 13;
int GarageDoor = A1;

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
pinMode(GarageDoor, OUTPUT);
pinMode(Door, OUTPUT);
pinMode(GreenLed, OUTPUT);
pinMode(RedLed, OUTPUT);
pinMode(YellowLed, OUTPUT);
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
  keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
    Serial.print("Pressed: ");

    digitalWrite(YellowLed, HIGH);
    delay(150);
    digitalWrite(YellowLed, LOW);

    Serial.println(eKey);
    switch (eKey){
      case '*': checkPassword(); break;
      case '#': password.reset(); break;
    case 'A': garage(); break;
    case '0': checkmum(); break;
      default: mum.append(eKey);
     }



  }
}
void checkmum(){
  if (mum.evaluate()){
     digitalWrite(Door, HIGH);
     digitalWrite(GarageDoor, 255);
    digitalWrite(GreenLed, HIGH);
    delay(500);
    digitalWrite(GarageDoor, 0);
    digitalWrite(YellowLed, HIGH);
    delay(100);
    digitalWrite(YellowLed, LOW);
    delay(100);
    digitalWrite(YellowLed, HIGH);
    delay(100);
    digitalWrite(YellowLed, LOW);
    delay(3000);
    digitalWrite(GreenLed, LOW);
    digitalWrite(Door, LOW);
    mum.reset();
    //Add code to run if it works
  }else{
    Serial.println("Wrong");
    digitalWrite(RedLed, HIGH);
    digitalWrite(YellowLed, HIGH);
    delay(1000);
    digitalWrite(YellowLed, LOW);
    digitalWrite(RedLed, LOW);
    mum.reset();

    //add code to run if it did not work
  }
}
void garage(){
Serial.print("Garage Door");
digitalWrite(GarageDoor, 255);
delay(100);
digitalWrite(YellowLed, HIGH);
delay(50);
digitalWrite(YellowLed, LOW);
delay(50);
digitalWrite(YellowLed, HIGH);
delay(100);
digitalWrite(YellowLed, LOW);
delay(500);
digitalWrite(GarageDoor, 0);
password.reset();
}





  void checkPassword(){
  if (password.evaluate()){
    Serial.println("Success");
     digitalWrite(Door, HIGH);
    digitalWrite(GreenLed, HIGH);
    delay(500);
    digitalWrite(YellowLed, HIGH);
    delay(100);
    digitalWrite(YellowLed, LOW);
    delay(100);
    digitalWrite(YellowLed, HIGH);
    delay(100);
    digitalWrite(YellowLed, LOW);
    delay(3000);
    digitalWrite(GreenLed, LOW);
    digitalWrite(Door, LOW);
    password.reset();

  }else{
    Serial.println("Wrong");
    digitalWrite(RedLed, HIGH);
    digitalWrite(YellowLed, HIGH);
    delay(1000);
    digitalWrite(YellowLed, LOW);
    digitalWrite(RedLed, LOW);
    password.reset();

    //add code to run if it did not work
  }
} 

如果有人能帮助我弄清楚如何让两者同时运行那就太棒了,谢谢。

0 个答案:

没有答案