Elektroniskt värdeskåp med Arduino (2 / 7 steg)
Steg 2: Koden
Jag kommer att dela koden direkt före tid - kopiera och klistra in detta i din Arduino IDE. Om du inte har programvaran Arduino ännu, kan du hämta det här: https://www.arduino.cc/en/Main/Software
#include < Liquid.Crystal.h >
#include < EEPROM.h >
#include < Servo.h >
int adress = 0;
statiska osignerade långa SaveTimer;
statiska osignerade långa SaveDelay = (30 * 1000);
röding kod [10] = "123142E"; ändra lösenord här
char Str [10].
char CodeLength = 6;
int Pos = 0;
bool upplåst;
statiska osignerade långa DisplayTimer;
statiska osignerade långa DisplayDelay = 200.
LiquidCrystal lcd (12, 11, 9, 8, 7, 6);
int buttonPin1 = 2;
int buttonPin2 = 3;
int buttonPin3 = 4;
int buttonPin4 = 5;
int Enter-knappen = 10;
int clearlockbutton = 13.
Servo myServo;
void setup() {
myServo.attach(A1);
Lock();
int EEPROMCodeOK = sant;
för (Pos = 0; POS < = (CodeLength); POS++) {
Str [Pos] = EEPROM.read(Pos);
om (! () strrchr ("1123456789", Str[Pos]))) {
inte en giltig kod
EEPROMCodeOK = false;
}
}
POS ++;
Str [Pos] = EEPROM.read(Pos);
om (Str [CodeLength + 1]! = 'E') EEPROMCodeOK = false;
om (EEPROMCodeOK) {
Str [CodeLength + 2] = '\0';
strncpy (kod, Str, CodeLength + 1).
}
ClearCode();
pinMode (buttonPin1, INPUT_PULLUP);
pinMode (buttonPin2, INPUT_PULLUP);
pinMode (buttonPin3, INPUT_PULLUP);
pinMode (buttonPin4, INPUT_PULLUP);
pinMode (Enter-knappen, INPUT_PULLUP);
pinMode (clearlockbutton, INPUT_PULLUP);
LCD.BEGIN (16, 2);
lcd.setCursor (0, 0);
LCD.Print("Hello.");
Delay(2000);
LCD.Clear();
lcd.setCursor (0, 0);
LCD.Print("Password:");
DisplayTimer = millis() + 200.
}
void loop() {
Lock();
POS = begränsa (Pos, 0, CodeLength);
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
int buttonState4 = digitalRead(buttonPin4);
int clButtonState = digitalRead(clearlockbutton);
int enterButtonState = digitalRead(enterbutton);
lcd.setCursor (9, 0);
om (buttonState1 == låg) {
Str [Pos] = '1';
POS ++;
Str [Pos] = '\0';
Delay(250);
medan (digitalRead(buttonPin1) == låg);
}
annat if (buttonState2 == låg) {
Str [Pos] = '2';
POS ++;
Str [Pos] = '\0';
Delay(250);
medan (digitalRead(buttonPin2) == låg);
}
annat if (buttonState3 == låg) {
Str [Pos] = '3';
POS ++;
Str [Pos] = '\0';
Delay(250);
medan (digitalRead(buttonPin3) == låg);
}
annat if (buttonState4 == låg) {
Str [Pos] = "4";
POS ++;
Str [Pos] = '\0';
Delay(250);
medan (digitalRead(buttonPin4) == låg);
}
annat if (enterButtonState == låg) {
Str [Pos] = "E";
POS ++;
Str [Pos] = '\0';
Delay(250);
lcd.setCursor (15, 0);
LCD.Print("E");
Delay(400);
lcd.setCursor (15, 0);
LCD.Print("");
medan (digitalRead(buttonPin1) == låg);
om (strcmp (Str, koden) == 0) {
Olåst = sant;
lcd.setCursor (0, 0);
LCD.Print ("tillgång beviljas");
Delay(2000);
LCD.Clear();
LCD.Print ("olåst");
}
annat {
LCD.Clear();
LCD.Print ("åtkomst nekad.");
Delay(2000);
LCD.Clear();
LCD.Print("Password:");
}
medan (olåst) {
Unlock();
om (digitalRead(clearlockbutton) == låg) {
Delay(200);
LCD.Clear();
LCD.Print ("låst");
Delay(2000);
LCD.Clear();
Olåst = false;
SaveTimer = millis() + 30000;
}
}
ClearCode();
}
annat if (clButtonState == låg) {
Delay(500);
medan (clearlockbutton == låg);
om ((millis()-SaveTimer) > 4500) {
}
ClearCode();
}
om ((lång) (millis() - DisplayTimer) > = 0) {
DisplayTimer += DisplayDelay;
lcd.setCursor (9, 0);
LCD.Print(Str);
lcd.setCursor (15, 0);
LCD.Print("");
om (clButtonState == låg) {
LCD.Clear();
LCD.Print("Password:");
}
}
}
void ClearCode() {
POS = 0;
Str [Pos] = '\0';
lcd.setCursor (0, 0);
LCD.Print("Password:");
lcd.setCursor (0, 1);
LCD.Print("");
}
void Unlock() {
myServo.write(117);
}
void Lock() {
myServo.write(26);
}