mini project arduino
code
#include <Password.h>
#include <Keypad.h>
Password password = Password( "0000" ); //password to unlock, can be changed
const byte ROWS = 4; // Four rows
const byte COLS = 3; // 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] = {8, 7, 6, 5 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 4, 3, 2 };
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
pinMode(10, OUTPUT);
pinMode(11, OUTPUT); //green light
pinMode(12, OUTPUT); //red light
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:
switch (eKey){
case '*': checkPassword(); delay(1); break;
case '#': password.reset(); delay(1); break;
default: password.append(eKey); delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){ //if password is right open
digitalWrite(11, HIGH);//turn on
delay(5000); //wait 5 seconds
digitalWrite(11, LOW);// turn off
digitalWrite(10, HIGH);//turn on
delay(500); //wait 5 seconds
digitalWrite(10, LOW);// turn off
}else{
digitalWrite(12, HIGH); //turn on
delay(500); //wait 5 seconds
digitalWrite(12, LOW);//turn off
}
}
อุปกรณ์ที่ใช้
led green 1
led rad 1
keypad
arduino
ความคิดเห็น
แสดงความคิดเห็น