Arduino Leonardo - Elektronisk lås
Placeret påFejl i koden i EEPROM-hukommelsen
Nu kan vi få en kode, tjek den, men hvad med at gemme den. For når du nulstiller Arduino ryddes RAM'en, så vi er nødt til at finde en anden måde at gemme den på og kunne ændre den på uden at skulle genuploade programmet. Det er her EEPROM'en træder ind i spillet ...
Så vi er nødt til at bruge EEPROM-biblioteket.
Storing and Getting the code from EEPROM
x
#include <EEPROM.h>
unsigned char Eeprom_PasscodeLength = 0;
void GetEepromCode() {
EEPROM.get(0, Eeprom_PasscodeLength);
if (Eeprom_PasscodeLength) {
NewPasscodeLength = Eeprom_PasscodeLength;
for (unsigned char i = 0; i < Eeprom_PasscodeLength; i++) {
EEPROM.get(i + 1, Master[i]);
}
}
else {
NewPasscodeLength = PasscodeLength;
return;
}
PasscodeLength = NewPasscodeLength;
}
void WriteEepromCode(){
EEPROM.put(0, PasscodeLength);
for (unsigned char i = 0; i < PasscodeLength; i++) {
EEPROM.put(i + 1, Master[i]);
}
}
Så nu er vi i stand til at gemme og få en kode fra EEPROM, det er på tide at aktivere det til at ændre koden, mens programmet stadig kører på Arduino .
Changing the code while the program is running
xxxxxxxxxx
110
void CheckNewCode() {
if (EnableDebug) {
Serial.print("dataCountBuff1: ");
Serial.println(dataCountBuff1);
Serial.print("dataCountBuff2: ");
Serial.println(dataCountBuff2);
Serial.print("codeBuff1: ");
Serial.println(code_buff1);
Serial.print("codeBuff2: ");
Serial.println(code_buff2);
}
if (dataCountBuff1 == dataCountBuff2) {
if(!strcmp(code_buff1, code_buff2)){
NewPasscodeLength = dataCountBuff2;
for (unsigned char i = 0; i <= MaxPasscodeLength; i++) {
Master[i] = 0;
}
for (unsigned char i = 0; i <= NewPasscodeLength; i++) {
Master[i] = code_buff1[i];
}
if (EnableDebug) {
Serial.println();
Serial.print("Master: ");
Serial.println(Master);
}
lcd.clear();
lcd.print("Passcodes Match");
_delay_ms(1000);
lcd.clear();
}
else {
NewPasscodeLength = PasscodeLength;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Codes do not match");
if (EnableDebug) {
Serial.println();
Serial.println("Passcodes do not match");
}
_delay_ms(1000);
lcd.clear();
}
}
else {
NewPasscodeLength = PasscodeLength;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Codes do not match");
if (EnableDebug) {
Serial.println();
Serial.println("Passcodes are not the same length");
}
_delay_ms(1000);
lcd.clear();
}
clearData();
}
void ChangeCode() {
dataCountBuff1 = 0;
dataCountBuff2 = 0;
for (unsigned char i = 0; i <= MaxPasscodeLength; i++) {
code_buff1[i] = 0;
code_buff2[i] = 0;
}
if (EnableDebug) {
Serial.println();
Serial.println("Changing Master");
}
lcd.clear();
lcd.print("Enter new code:");
lcd.setCursor(0,2);
lcd.print("press * to confirm");
Key = WaitForKey();
while(Key != '*') {
GetCode();
Key = WaitForKey();
}
Data[data_count] = 0;
dataCountBuff1 = data_count;
for (unsigned char i =0; i <= data_count; i++) {
code_buff1[i] = Data[i];
}
clearData();
lcd.clear();
_delay_ms(500);
lcd.print("Confirm new code:");
lcd.setCursor(0,2);
lcd.print("press * to confirm");
Key = WaitForKey();
while (Key != '*') {
GetCode();
Key = WaitForKey();
}
Data[data_count] = 0;
dataCountBuff2 = data_count;
for (unsigned char i =0; i <= data_count; i++) {
code_buff2[i] = Data[i];
}
clearData();
CheckNewCode();
PasscodeLength = NewPasscodeLength;
if (EnableDebug) {
Serial.print("PasscodeLength: ");
Serial.println(PasscodeLength);
}
WriteEepromCode();
}