ยาวหน่อยครับ คือผมแริ้นออก serial monotor ได้ แต่ เขียนลงใน micro sd card ไม่ได้อะครับ ตรงที่อักษรแดงไว้
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <Password.h>
File myFile;
const int chipSelect = 10;
Password password[16] = {Password( "0001" ),Password( "0002" ),Password( "0003" ),Password( "0004" ),
Password( "0005" ),Password( "0006" ),Password( "0007" ),Password( "0008" ),
Password( "0009" ),Password( "0010" ),Password( "0011" ),Password( "0012" ),
Password( "0013" ),Password( "0014" ),Password( "0015" ),Password( "0016" ), };
int data[16]={101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116};
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,2}; //connect to the column pinouts of the keypad
LiquidCrystal_I2C lcd(0x27,16,2);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int correct;
int correct1;
int correct2;
int showlcd();
int i;
void setup() {
Serial.begin(9600);
pinMode(SS, OUTPUT);
myFile = SD.open("IDCODE.txt", FILE_WRITE);
lcd.begin();
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
lcd.print(">");
lcd.blink();
boolean correct;
boolean correct1;
boolean correct2;
correct = false;
correct1 = false;
correct2 = false;
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY){
lcd.print(key);
password[0].append(key);password[1].append(key);password[2].append(key);password[3].append(key);
password[4].append(key);password[5].append(key);password[6].append(key);password[7].append(key);
password[8].append(key);password[9].append(key);password[10].append(key);password[11].append(key);
password[12].append(key);password[13].append(key);password[14].append(key);password[15].append(key);
if(password[0].evaluate()){
showlcd();
i = 0;
}
if(password[1].evaluate()){
showlcd();
i = 1;
}
if(password[2].evaluate()){
showlcd();
i = 2;
}
if(password[3].evaluate()){
showlcd();
i = 3;
}
if(password[4].evaluate()){
showlcd();
i = 4;
}
if(password[5].evaluate()){
showlcd();
i = 5;
}
if(password[6].evaluate()){
showlcd();
i = 6;
}
if(password[7].evaluate()){
showlcd();
i = 7;
}
if(password[8].evaluate()){
showlcd();
i = 8;
}
if(password[9].evaluate()){
showlcd();
i = 9;
}
if(password[10].evaluate()){
showlcd();
i = 10;
}
if(password[11].evaluate()){
showlcd();
i = 11;
}
if(password[12].evaluate()){
showlcd();
i = 12;
}
if(password[13].evaluate()){
showlcd();
i = 13;
}
if(password[14].evaluate()){
showlcd();
i = 14;
}
if(password[15].evaluate()){
showlcd();
i = 15;
}
if(correct1){
Serial.print(key);
myFile.println(key);
myFile.close();
}
}
switch (key){
case 'A' : if(correct){
lcd.clear();
lcd.print("Student ID");
lcd.setCursor(0,1);
lcd.print("Pressed:");
lcd.blink();
correct = false;
correct1 = true;
correct2 = true;
}
break;
case 'B': if(correct1){
lcd.clear();
lcd.print("Code to Unlock");
lcd.setCursor(0,1);
lcd.noBlink();
lcd.print(data[i]);
correct1 = false;
}
break;
case '#' : password[0].reset();password[1].reset();password[2].reset();password[3].reset();
password[4].reset();password[5].reset();password[6].reset();password[7].reset();
password[8].reset();password[9].reset();password[10].reset();password[11].reset();
password[12].reset();password[13].reset();password[14].reset();password[15].reset();
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
lcd.print(">");
lcd.blink();
break;
}
}
int showlcd() {
lcd.clear();
lcd.print("Correct Password");
correct = true;
}
แยกเช็คทีละส่วนนะครับ จะได้ดีบั๊กได้ง่าย
เขียนโปรแกรมเช็คเฉพาะ SD Card ก่อน มีโคดตัวอย่างในหน้าบทความครับ
จากนั้นในโคดสีแดง เอา if ออกเพื่อทดสอบว่าต่อ SD Card ถูกต้องแล้วหรือยัง
คือตอนนี้ผมพอทราบปัญหาแล้วครับ คือ เมื่อผมสั่งให้มีการแสดงผลที่จอ lcd ตัว micro sd card module จะมีการ
สร้างไฟล์ txt แต่จะไม่ยอม ปริ้น หรือ บันทึกค่าที่กด ในไฟล์ txt ครับผม ขอบคุณครับ
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <Password.h>
File myFile;
const int chipSelect = 4;
Password password[16] = {Password( "0001" ),Password( "0002" ),Password( "0003" ),Password( "0004" ),
Password( "0005" ),Password( "0006" ),Password( "0007" ),Password( "0008" ),
Password( "0009" ),Password( "0010" ),Password( "0011" ),Password( "0012" ),
Password( "0013" ),Password( "0014" ),Password( "0015" ),Password( "0016" ), };
int data[16]={101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116};
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9,8,7,6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,2}; //connect to the column pinouts of the keypad
LiquidCrystal_I2C lcd(0x27,16,2);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int correct;
int correct1;
int correct2;
int showlcd();
int i;
void setup() {
Serial.begin(9600);
pinMode(SS, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
}
myFile = SD.open("IDCODE.txt", FILE_WRITE);
lcd.begin();
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
lcd.print(">");
lcd.blink();
boolean correct1;
correct1 = false;
}
void loop() {
correct1 = true;
if(correct1){
Serial.print("project");
myFile.println("project");
myFile.close();
}
}
พี่ครับในสองส่วนนี้ จะทำอย่างไงครับให้สามารถทั้งแสดงผล lcd กับบันทึกข้อมูลด้วยได้อะครับ
ผมลองแล้วไม่ได้จริงๆ รบกวนด้วยครับ แนะนำผมก็ได้ครับ
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
lcd.print(">");
lcd.blink();
(correct1 = true;
if(correct1){
Serial.print("project");
myFile.println("project");
myFile.close();
}
เช็คตามนี้ครับ
1. LCD สามารถแสดงผลได้
2. SD Card สามารถบันทึกได้
3. โคดเช็คเงื่อนไข correct1 ว่าเงื่อนไขถูกต้อง เช็คโดยยังไม่ต้องบันทึก sd card ให้ ปริ๊นออก serial monitor แสดงผลแทน
ตามรูปเลยครับตอนนี้ ผมติดตรงที่ผมเขียน loop การรับค่าจากคีย์แพด ( กดเลขได้ไม่เกิน 8 หลัก เป็นรหัสประจำตัว ) ซึ่งเป็ฯเลขอะไรก็ได้ แล้วจะให้เก็บไว้ใน ตัวแปรสักตัวแปรหนึง แล้วค่อยไปปริ้นออกมาดูค่า และก็บันทึก รหัสประจำตัว เมื่อกด B ครับ
ลอง debug ดู ใกล้จะได้แล้ว ผมอยากให้ได้ลองศึกษาโคดครับ