Lösenord beskyddat dörrlås med arduino (4 / 4 steg)
Steg 4: kod
#include < Keypad.h >
#include < LiquidCrystal.h >
#include < tråd >
int greenLed = 22.
int redLed = 23.
int motorPin = 24.
int piezoPin = 25;
char * ourCode = "1234";
int aktuellaposition = 0;
CONST byte rader = 4;
CONST byte cols = 3;
char nycklar [rader] [cols] = {
{"1", "2", "3"},
{'4', '5','6 '},
{'7', '8','9 '},
{'*','0','#'}
};
byte rowPins [rader] = {13,10,9,8}.
byte colPins [cols] = {7,6,1}.
Knappsatsen keypad=Keypad(makeKeymap(keys),rowPins,colPins,rows,cols);
LiquidCrystal Lcd (12,11,5,4,3,2);
void setup()
{
LCD.BEGIN(16,2);
LCD.init();
displayCodeEntryScreen();
pinMode(piezoPin,OUTPUT);
pinMode(motorPin,OUTPUT);
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
digitalWrite(redLed,LOW);
digitalWrite(greenLed,LOW);
sätta din installation kod här, om du vill köra en gång:
}
void loop() {
int l;
char key=keypad.getKey();
IF(int(Key)!=0) {
Lcd.setCursor(10,0);
LCD.Print("");
Lcd.setCursor(10,0);
för (l = 0; l < = aktuellaposition; ++ l)
{Lcd.print("*");}
IF(Key==ourCode[currentPosition])
{
++ aktuellaposition;
IF(currentPosition==4)
{unlockDoor();
aktuellaposition = 0;
}
}
Else{invalidCode();
aktuellaposition = 0;
}
}
}
void invalidCode()
{
digitalWrite(redLed,HIGH);
digitalWrite(motorPin,LOW);
digitalWrite(piezoPin,HIGH);
clearScreen();
Lcd.setCursor(0,0);
LCD.Print ("åtkomst nekad!");
Lcd.setCursor(0,1);
LCD.Print ("ogiltig kod").
Delay(5000);
digitalWrite(redLed,LOW);
digitalWrite(piezoPin,LOW);
displayCodeEntryScreen();
}
void unlockDoor()
{
digitalWrite(greenLed,HIGH);
clearScreen();
Lcd.setCursor(0,0);
LCD.Print ("tillgång beviljas");
Lcd.setCursor(0,1);
LCD.Print ("Välkommen!");
digitalWrite(motorPin,HIGH);
Delay(5000);
digitalWrite(greenLed,LOW);
digitalWrite(motorPin,LOW);
displayCodeEntryScreen();
}
void displayCodeEntryScreen()
{
clearScreen();
Lcd.setCursor(0,0);
LCD.Print("Hallow,Welcome");
Lcd.setCursor(0,1);
LCD.Print ("Ange kod").
}
void clearScreen()
{
Lcd.setCursor(0,0);
LCD.Print("");
Lcd.setCursor(0,1);
LCD.Print("");
}
sätta din huvudsakliga kod här, för att köra flera gånger: