เขียนโปรแกรมเก็บค่ารหัสผ่านไว้ใน eeprom หรือ sd card ครับ
arduino uno มี EEPROM ให้ครับไม่ต้องต่อเพิ่ม http://arduino.cc/en/Reference/EEPROM
มีตัวอย่างในเว็บ arduino.cc ครับ
ผมลอง ทำแบบนี้เปลี่ยน โค๊ดนิดหน่อย มันก็ยัง สร้างได้แค่ รหัสเดียว ผมอยากจะสร้าง 2 รหัส แต่ ยังทำไม่ได้ เลย
#include <Password.h>
#include <EEPROM.h>
byte pass1;
byte pass2;
byte pass3;
byte pass4;
int i=0;
char newPassword[5]= " ";
Password password1 = Password(newPassword);
Password password2 = Password(newPassword);
byte currentLength = 0;
void setup()
{
Serial.begin(9600);
pass1 = EEPROM.read(0);
delay(100);
pass2 = EEPROM.read(1);
delay(100);
pass3 = EEPROM.read(2);
delay(100);
pass4 = EEPROM.read(3);
delay(100);
for (int ii=0; ii<4; ii++)
{
newPassword[ii]=EEPROM.read(0+ii);
delay(5);
}
}
void loop()
{
if (Serial.available())
{
char input = Serial.read();
switch (input)
{
case '*': //reset password
password1.reset();
password2.reset();
currentLength = 0;
Serial.println("\tPassword is reset!");
break;
case 'A': //evaluate password
if (password1.evaluate())
{
Serial.println("\tOK Password Correct");
password1.reset();
currentLength = 0;
}
else
{
Serial.println("\tNO Password Did Not");
password1.reset();
currentLength = 0;
Serial.println("\tPassword is reset!");
}
break;
case '#': //evaluate password
if (password2.evaluate())
{
Serial.println("\tOK Password Correct");
password2.reset();
currentLength = 0;
}
else
{
Serial.println("\tNO Password Did Not");
password2.reset();
currentLength = 0;
Serial.println("\tPassword is reset!");
}
break;
case 'n': // New Pasword
delay(100);
{
EEPROM.write(0,'2');
delay(100);
EEPROM.write(1,'5');
delay(100);
EEPROM.write(2,'1');
delay(100);
EEPROM.write(3,'4');
delay(100);
break;
default:
password1 << input;
password2 << input;
currentLength++;
}
}
}
}
ลองศึกษาโคดอีกซักนิดนะครับ ถ้าไม่ได้จริง ๆ เดียวผมช่วย ทำได้ขนาดนี้แล้วไม่น่ายากเกินความสามารถครับ
ผมลอง ทำแบบนี้ พอเพิ่มไปมัน บอกว่า OK Password Correct หมดเลย ไม่มีผิดพลาด ผมเลยสงสัยว่า แบบนี้ โค๊ดมันมีปัญหาไหมครับ //ถ้ามีปัญหา บอกใบ้ซักนิดอิอิ
#include <Password.h>
#include <EEPROM.h>
byte pass1;
byte pass2;
byte pass3;
byte pass4;
int i=0;
char newPassword[10]= {1,2,3,4,5,6,7,8,9,10};
Password password = Password(newPassword);
byte currentLength = 0;
void setup()
{
Serial.begin(9600);
pass1 = EEPROM.read(0);
delay(100);
pass2 = EEPROM.read(1);
delay(100);
pass3 = EEPROM.read(2);
delay(100);
pass4 = EEPROM.read(3);
delay(100);
for (int ii=0; ii<4; ii++)
{
newPassword[ii]=EEPROM.read(0+ii);
delay(5);
}
}
void loop()
{
if (Serial.available())
{
char input = Serial.read();
switch (input)
{
case '*': //reset password
password.reset();
currentLength = 0;
Serial.println("\tPassword is reset!");
break;
case '#': //evaluate password
if (password.evaluate())
{
Serial.println("\tOK Password Correct");
currentLength = 0;
}
else
{
Serial.println("\tNO Password Did Not");
currentLength = 0;
Serial.println("\tPassword is reset!");
}
break;
case 'n': // New Pasword
delay(100);
{
EEPROM.write(0,'2');
delay(100);
EEPROM.write(1,'5');
delay(100);
EEPROM.write(2,'1');
delay(100);
EEPROM.write(3,'4');
delay(100);
break;
char input1 = Serial.read();
switch (input1){
case '*': //reset password
password.reset();
currentLength = 0;
Serial.println("\tPassword is reset!");
break;
case '#': //evaluate password
if (password.evaluate())
{
Serial.println("\tOK Password Correct");
currentLength = 0;
}
else
{
Serial.println("\tNO Password Did Not");
currentLength = 0;
Serial.println("\tPassword is reset!");
}
break;
case 'n': // New Pasword
delay(100);
{
EEPROM.write(0,'2');
delay(100);
EEPROM.write(1,'5');
delay(100);
EEPROM.write(2,'1');
delay(100);
EEPROM.write(3,'4');
delay(100);
break; }
default:
password << input;
password << input1;
currentLength++;
}
}
}
} }
ก็ต้องลองเทสใช้งานจริงครับ ดูคร่าว ๆ แล้วไม่น่าจะมีปัญหา