#include <Keypad.h>
#include <Password.h>
int led1 = 11;
int led2 = 12;
int locked =1;
int passinput = 0;
int lockedled = A1;
int unlockedled = A2;
long ledflashvar = 0;
long ledflashtime = 300;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
Password password = Password("0000"); // รหัสของการปลดล็อค
void setup(){
// กำหนดค่าเริ่มต้น และ PIN ต่างๆ
Serial.begin(9600);
pinMode(led1, OUTPUT);
digitalWrite(led1, 0);
pinMode(led2, OUTPUT);
digitalWrite(led2, 0);
pinMode(unlockedled, OUTPUT);
digitalWrite(unlockedled, 0);
}
void loop(){
char key = keypad.getKey(); // รับค่าจาก keypad
// กรณีที่ Lock อยู่ LED สีแดงจะติด และหากอยู่ระหว่างการใส่รหัส LED สีแดงจะกระพริบ
if(locked){
if(passinput){
unsigned long ledcurrentvar = millis();
if(ledcurrentvar - ledflashvar > ledflashtime) {
ledflashvar = ledcurrentvar;
digitalWrite(lockedled, !digitalRead(lockedled));
}
}
// ถ้าไม่ได้อยู่ระหว่างการใส่รหัส
else{
digitalWrite(lockedled, 0); // LED สีแดงจะติด
}
digitalWrite(unlockedled, 255); // LED สีเขียวดับ
}
if (key != NO_KEY){
Serial.println(key); // แสดงค่าที่กดบน Serial monitor
password.append(key); // บันทึกค่ารหัสที่กดและเปรียบเทียบกับรหัสที่ถูกต้อง
passinput = 1; // อยู่ระหว่างการใส่รหัส
// ถ้ากด '*' ให้เริ่มใส่รหัสใหม่
if(key == '*'){
password.reset(); // เริ่มต้นใส่รหัสใหม่ตั้งแต่ตัวแรก
passinput = 0; // ไม่อยู่ระหว่างการกดรหัส
locked = 1;
digitalWrite(led1, LOW); // ตั้งค่า Relay ให้อยู่ในสถานะ Hight
digitalWrite(led2, LOW);
}
if(password.evaluate()) { // ถ้าใส่ Password ถูกต้องให้ปลดล็อค
locked = !locked;
password.reset(); // เริ่มต้นใส่รหัสใหม่ตั้งแต่ตัวแรก
passinput = 0;
}
// ถ้าปลดล๊อคแล้วให้ไฟสีเขียวติด สีแดงดับ และสามารถควบคุมการเปิดปิดของ Relay ได้
if(!locked) {
passinput = 0;
digitalWrite(lockedled, 0);
digitalWrite(unlockedled, 0);
switch (key) { // และเลือก Relay ที่ต้องการควบคุม
case '1':
digitalWrite(led1, !digitalRead(led1));
break;
case '2':
digitalWrite(led2, !digitalRead(led2));
break;
}
password.reset(); // เริ่มต้นใส่รหัสใหม่ตั้งแต่ตัวแรก
}
}
}
ผมลองเขียนแบบ Password password[2] = Password("0000"),Password("1234"); มันใช้ได้ แค่ตัวใดตัวหนึ่งเท่านั้น ใครพอทราบ วิธีเขียน เขียนให้เป็นตัวอย่างซัก บรรทัด ก็ยังดีครับ ผมมึนหมดแล้ว *-*
ผมลองหาข้อมูลช่วย เห็นหลายคนถามเรื่องการรับรหัส ไลบารีตัวนี้เป็นตัวที่นิยมใช้ ขออธิบายเพิ่มเติมครับ
น่าจะใช้ http://playground.arduino.cc/Code/Password มีวิธีใช้ในเว็บครับ
สร้างออปเจก password ตามนี้
Password password = Password("รหัสผ่าน");
ใช้ตั้งค่า password
Warning: The new password should be a global variable (not a local function variable) as this function copies a pointer to the new password into the library, not a copy of the password.
เช็คว่ารหัสผ่านเป็นรหัสนี้หรือไม่
เพิ่มข้อความรหัสผ่านเข้าไปทีละตัว
รีเซตรหัสที่รับเข้าไป
เปรียบเทียบว่าค่าที่เพิ่มเข้ามา ใช่ค่าเดียวกับ password ที่ตั้งไว้ตอนแรก
ผมทำตัวอย่างให้ดูตามนี้ครับ
http://www.allarduino.com/download/w_001.txt
#include "Password.h"
Password password = Password( "1234" );
Password password2 = Password( "555" );
void setup(){
Serial.begin(9600);
password.append('1');
password.append('2');
password.append('3');
password.append('4');
if(password.evaluate()){
Serial.println("OK");
}
else{
Serial.println("NO");
}
if(password.is("1234")){
Serial.println("OKK");
}else{
Serial.println("NNNO");
}
}
void loop(){/*nothing to loop*/
}
ถ้าต้องการประกาศแบบ array เหมือนโคด ทำแบบนี้ครับ
#include "Password.h"
void setup(){
Serial.begin(9600);
Password p[2] = {Password("1234"),Password("bbb")};
p[0].append('1');
p[0].append('2');
p[0].append('3');
p[0].append('4');
if(p[0].evaluate()){
Serial.println("OK");
}
else{
Serial.println("NO");
}
if(p[1].is("bbb")){
Serial.println("OKK");
}else{
Serial.println("NNNO");
}
}
void loop(){/*nothing to loop*/
}
ผมเขียน แบบนี้ ผมลองทดสอบดู ใน priteus แล้วผลปรกฎว่า ok แต่ไม่รู้จะมีปัญหาตามมาภายหลังไหม พึ่งคิดได้หลักจาก ตั้งกระทู้
#include <Keypad.h>
#include <Password.h>
int led1 = 11;
int led2 = 12;
int locked =1;
int passinput = 0;
int lockedled = A1;
int unlockedled = A2;
long ledflashvar = 0;
long ledflashtime = 300;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
Password password1 = Password("0000");
Password password2 = Password("1234"); // รหัสของการปลดล็อค
void setup(){
// กำหนดค่าเริ่มต้น และ PIN ต่างๆ
Serial.begin(9600);
pinMode(led1, OUTPUT);
digitalWrite(led1, 0);
pinMode(led2, OUTPUT);
digitalWrite(led2, 0);
pinMode(unlockedled, OUTPUT);
digitalWrite(unlockedled, 0);
}
void loop(){
char key = keypad.getKey(); // รับค่าจาก keypad
// กรณีที่ Lock อยู่ LED สีแดงจะติด และหากอยู่ระหว่างการใส่รหัส LED สีแดงจะกระพริบ
if(locked){
if(passinput){
unsigned long ledcurrentvar = millis();
if(ledcurrentvar - ledflashvar > ledflashtime) {
ledflashvar = ledcurrentvar;
digitalWrite(lockedled, !digitalRead(lockedled));
}
}
// ถ้าไม่ได้อยู่ระหว่างการใส่รหัส
else{
digitalWrite(lockedled, 0); // LED สีแดงจะติด
}
digitalWrite(unlockedled, 255); // LED สีเขียวดับ
}
if (key != NO_KEY){
Serial.println(key); // แสดงค่าที่กดบน Serial monitor
password1.append(key);
password2.append(key); // บันทึกค่ารหัสที่กดและเปรียบเทียบกับรหัสที่ถูกต้อง
passinput = 1; // อยู่ระหว่างการใส่รหัส
// ถ้ากด '*' ให้เริ่มใส่รหัสใหม่
if(key == '*'||key == '#'){
password1.reset(); // เริ่มต้นใส่รหัสใหม่ตั้งแต่ตัวแรก
passinput = 0; // ไม่อยู่ระหว่างการกดรหัส
locked = 1;
digitalWrite(led1, LOW); // ตั้งค่า Relay ให้อยู่ในสถานะ Hight
digitalWrite(led2, LOW);
}
if(password1.evaluate()||password2.evaluate()) { // ถ้าใส่ Password ถูกต้องให้ปลดล็อค
locked = !locked;
password1.reset(); // เริ่มต้นใส่รหัสใหม่ตั้งแต่ตัวแรก
passinput = 0;
}
// ถ้าปลดล๊อคแล้วให้ไฟสีเขียวติด สีแดงดับ และสามารถควบคุมการเปิดปิดของ Relay ได้
if(!locked) {
passinput = 0;
digitalWrite(lockedled, 0);
digitalWrite(unlockedled, 0);
switch (key) { // และเลือก Relay ที่ต้องการควบคุม
case '1':
digitalWrite(led1, !digitalRead(led1));
break;
case '2':
digitalWrite(led2, !digitalRead(led2));
break;
}
password1.reset(); // เริ่มต้นใส่รหัสใหม่ตั้งแต่ตัวแรก
}
}
}
ได้ครับ ปัญหานี้ตอนแรกคือประกาศแบบ array ซึ่งเขียนผิดนิดหน่อย เลยกลายเป็นชี้ไปค่าเดียวกัน
วิธีประกาศ array ของ object ทำแบบนี้
Password p[2] = {Password("1234"),Password("bbb")};
อีกวิธีที่น่าจะง่าย ในกรณ๊ที่ไม่ต้องใช้แบบ array ก็ประกาศ object ทีละตัวตามตัวอย่างครับ
Password password = Password( "1234" );
Password password2 = Password( "555" );
โคดใหม่น่าจะไม่มีปัญหาแล้วนะครับ 
รบกวนปรึกษาหน่อยคับ พอดีใช้arduino เชื่อมต่อกับตัวรับค่าความชื้นในดินคับและใช้คีย์แพด ในการป้อนค่าซึ่งตอนนี้ได้เขียนโปรแกรมเสจเรียบร้อยแล้ว ***แต่ตรงที่ทำการsetting มันใส่ค่าได้แค่เลขหลักเดียว คือผมต้องการเลข2หลักไม่ทราบว่าจะต้องเพิ่มครงไหนคับ
#include <Keypad.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> // Set the LCD address to 0x3f for a 16 chars and 4 line display LiquidCrystal_I2C lcd(0x3F, 16, 4); int sensor1 = A0; // select the input pin for the potentiometer int sensor2 = A1; // select the input pin for the potentiometer int sen1 = 0; // variable to store the value coming from the sensor int sen2 = 0; // variable to store the value coming from the sensor int i; int sw1 = 2; int va1 = 0; int sw2 = 3; int va2 = 0; int redPin = 12; const byte ROWS = 4; //four rows const byte COLS = 3; //three columns char keys[ROWS][COLS] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); void setup() { pinMode(redPin, OUTPUT); // initialize the LCD lcd.begin(); lcd.setCursor(5, 0); lcd.print("WELCOME"); lcd.setCursor(4, 1); lcd.print("KMITL PCC"); lcd.setCursor(-2, 2); lcd.print("SOIL MOISTURE"); lcd.setCursor(-1, 3); lcd.print("Palm & Jook"); delay(3000); lcd.clear(); pinMode(sw1, INPUT); pinMode(sw2, INPUT); } void loop() { sen1 = analogRead(sensor1); sen2 = analogRead(sensor2); char key = keypad.getKey(); int j=(sen1/100); int k=(sen2/100); int rh=(j+k)/2; va1 = digitalRead(sw1); va2 = digitalRead(sw2); delay(1000); lcd.setCursor(1, 0); lcd.print("Moisture1:"); lcd.setCursor(12, 0); lcd.print(j); lcd.setCursor(1, 1); lcd.print("Moisture2:"); lcd.setCursor(12, 1); lcd.print(k); lcd.setCursor(-3, 2); lcd.print("%RH :"); lcd.setCursor(3, 2); lcd.print(rh); lcd.setCursor(-3, 3); lcd.print("Setting:"); if (key){ delay(1000); lcd.setCursor(6, 3); lcd.print(key); } if(rh>=key); { digitalWrite(redPin, LOW); } //if (va1 == LOW){ //delay(500); //i++; } //if (va2 == LOW){ //delay(500); // i--; } }
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x3f for a 16 chars and 4 line display
LiquidCrystal_I2C lcd(0x3F, 16, 4);
int sensor1 = A0; // select the input pin for the potentiometer
int sensor2 = A1; // select the input pin for the potentiometer
int sen1 = 0; // variable to store the value coming from the sensor
int sen2 = 0; // variable to store the value coming from the sensor
int i;
int sw1 = 2;
int va1 = 0;
int sw2 = 3;
int va2 = 0;
int redPin = 12;
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
pinMode(redPin, OUTPUT);
// initialize the LCD
lcd.begin();
lcd.setCursor(5, 0);
lcd.print("WELCOME");
lcd.setCursor(4, 1);
lcd.print("KMITL PCC");
lcd.setCursor(-2, 2);
lcd.print("SOIL MOISTURE");
lcd.setCursor(-1, 3);
lcd.print("Palm & Jook");
delay(3000);
lcd.clear();
pinMode(sw1, INPUT);
pinMode(sw2, INPUT);
}
void loop()
{
sen1 = analogRead(sensor1);
sen2 = analogRead(sensor2);
char key = keypad.getKey();
int j=(sen1/100);
int k=(sen2/100);
int rh=(j+k)/2;
va1 = digitalRead(sw1);
va2 = digitalRead(sw2);
delay(1000);
lcd.setCursor(1, 0);
lcd.print("Moisture1:");
lcd.setCursor(12, 0);
lcd.print(j);
lcd.setCursor(1, 1);
lcd.print("Moisture2:");
lcd.setCursor(12, 1);
lcd.print(k);
lcd.setCursor(-3, 2);
lcd.print("%RH :");
lcd.setCursor(3, 2);
lcd.print(rh);
lcd.setCursor(-3, 3);
lcd.print("Setting:");
if (key){
delay(1000);
lcd.setCursor(6, 3);
lcd.print(key);
}
if(rh>=key);
{
digitalWrite(redPin, LOW);
}
//if (va1 == LOW){
//delay(500);
//i++; }
//if (va2 == LOW){
//delay(500);
// i--; }
}
รบกวนปรึกษาหน่อยคับ พอดีใช้arduino เชื่อมต่อกับตัวรับค่าความชื้นในดินคับและใช้คีย์แพด ในการป้อนค่าซึ่งตอนนี้ได้เขียนโปรแกรมเสจเรียบร้อยแล้ว ***แต่ตรงที่ทำการsetting มันใส่ค่าได้แค่เลขหลักเดียว คือผมต้องการเลข2หลักไม่ทราบว่าจะต้องเพิ่มครงไหนคับ