/*---------------------- การบันทึกข้อมูลลงใน EEPROM -----------------------*/
/*-------- ATmega328P เขียนได้ประมาณ 1 แสนครั้งต่อ Address ก็จะหมดอายุ --------*/
/*------ ทั้งหมดมี 1024 Address เขียนตัวอักษรได้ไม่เกิน 1Kb หรือ 1024 bytes ------*/
/*------ หรือ 1024 ตัวอักษร (1 byte (0-254) คือ 1 Address -----------------*/
/* การทำงาน
1 กดปุ่ม1 เพิ่มค่า
2 กดปุ่ม2 ลดค่า
3 กดปุ่ม3 บันทึกข้อมูลลงใน EEPROM ในตำแหน่งที่กำหนด */
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //IC PCF8574T USE 0x27 0x3F
#include <EEPROM.h> //Library บันทึกข้อมูลลใน EEPROM มีมาพร้อม Board Arduino ไม่ต้อง Load เพิ่ม
#define EEPROM_SIZE 1024
//#include <wire.h>
int data_01;
byte led_rec = 13;
unsigned long lt_01 = 0; //Task01: last Time 01 = 0
unsigned long lt_02 = 0; //Task02: last Time 02 = 0
unsigned long lt_03 = 0; //Task03: last Time 03 = 0
//unsigned long lt_04 = 0; //Task04: last Time 04 = 0
//unsigned long lt_05 = 0; //Task05: last Time 05 = 0
unsigned long P_01 = 50; //Task01: Preiod Time 01 = 50 mS
unsigned long P_02 = 60; //Task02: Preiod Time 02 = 60 mS
unsigned long P_03 = 70; //Task03: Preiod Time 03 = 70 mS
//unsigned long P_04 = 0; //Task04: Preiod Time 04 = 0
//unsigned long P_05 = 0; //Task05: Preiod Time 05 = 0
const int up_button = 8; //กำหนดปุ่มเพิ่มค่าเป็นขา 8
const int down_button = 9; //กำหนดปุ่มลดค่าเป็นขา 9
const int rec_button = 10; //กำหนดปุ่มบันทึกเป็นขา 10
int ee_Address_00 = 0;
int counter_01; //ตัวแปรเก็บค่า
int up_state = 1; //สถานะปัจจุบันของปุ่ม UP
int up_lastate = 1; //สถานะก่อนหน้าของปุ่ม UP
int down_state = 0; //สถานะปัจจุบันของปุ่ม DOWN
int down_lastate = 0; //สถานะก่อนหน้าของปุ่ม DOWN
int rec_state = 0; //สถานะปัจจุบันของปุ่ม Record
int rec_lastate = 0; //สถานะก่อนหน้าของปุ่ม Record
void setup(){
Serial.begin(9600);
/*if (!EEPROM.begin(EEPROM_SIZE)) { //เริ่มต้นการทำงานของ EEPROM (Electrically Erasable Programmable Read-Only Memory) และตรวจสอบว่าสามารถใช้งาน EEPROM ได้หรือไม่
Serial.println("Failed init EEPROM");
delay(1000); //หยุดการทำงานเป็นเวลา 1 วินาที */
EEPROM.begin();
pinMode(up_button, INPUT_PULLUP);
pinMode(down_button, INPUT_PULLUP);
pinMode(rec_button, INPUT_PULLUP);
pinMode(led_rec, OUTPUT);
counter_01 = EEPROM.get(ee_Address_00, data_01); //***เมื่อเปิดเครื่องให้อ่านค่าข้อมูลใน EEPROM ขึ้นมาก่อน
Serial.print(counter_01);
lcd.begin(16,2);
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.display();
lcd.print("Prss the button");
lcd.setCursor(2, 1);
lcd.print(counter_01);
}
void loop() {
Serial.print("Counter_01 Val : ");
Serial.print(counter_01);
Serial.print(" Add 0 Data : ");
Serial.println(data_01);
add(); //Function เพิ่มค่า Multi task แบบ millis() ทำงานทุกๆ 50 mS
reduce(); //Function ลดค่า Multi task แบบ millis() ทำงานทุกๆ 60 mS
record_01(); //Function บันทึกค่า Multi task แบบ millis() ทำงานทุกๆ 70 mS
}
/*---------- Function เพิ่มค่าชนิด Multi Task แบบ millis() --------*/
void add() {
if (millis() - lt_01 >= P_01) {
lt_01 = millis();
up_state = digitalRead(up_button); //อ่านค่าการกดปุ่ม UP แล้วมาเก็บค่าไว้ในตัวแปร up_state
if (up_state != up_lastate) { //ถ้าค่า up_state มีค่าไม่เท่ากับ up_lastate
if (up_state == LOW) { //ถ้าค่า down_state มีค่าเป็น LOW มีการกดปุ่ม DOWN
if (counter_01 < 100) { //ถ้าค่า counter_01 มีค่าน้อยกว่า 100 (สูงสุด 254)
counter_01 ++; //ให้ค่า counter_01 บวกขึ้นไปทีละ 1
lcd.setCursor(2, 1); //Set Cursor ของ LCD ไปยังแถวที่ 2 ตำแหน่งที่ 1
lcd.print(" "); //Clear แถวที่ 2 (16 ตัวอักษรให้ว่าง)
lcd.setCursor(2, 1); //Set Cursor ของ LCD ไปยังแถวที่ 2 ตำแหน่งที่ 1
lcd.print(counter_01); //แสดงผล counter_01
} else {
Serial.print("Off");
}
}
up_lastate = up_state; //ให้ค่าสถานะของปุ่มกด up_lastate = up_state
}
}
}
/*---------- Function ลดค่าชนิด Multi Task แบบ millis() --------*/
void reduce() {
if (millis() - lt_02 >= P_02) {
lt_02 = millis();
down_state = digitalRead(down_button); //อ่านค่าการกดปุ่ม DOWN แล้วมาเก็บค่าไว้ในตัวแปร down_state
if (down_state != down_lastate) { //ถ้าค่า down_state มีค่าไม่เท่ากับ down_lastate
if (down_state == LOW) { //ถ้าค่า down_state มีค่าเป็น LOW เมื่อมีการกดปุ่ม DOWN
if (counter_01 > 0) { //ถ้าค่า counter_01 มีค่ามากกว่า 0 ป้องกันการติดลบ
counter_01 --; //ให้ค่า counter_01 ลบลงทีละ 1
lcd.setCursor(2, 1); //Set Cursor ของ LCD ไปยังแถวที่ 2 ตำแหน่งที่ 1
lcd.print(" "); //Clear แถวที่ 2 (16 ตัวอักษรให้ว่าง)
lcd.setCursor(2, 1); //Set Cursor ของ LCD ไปยังแถวที่ 2 ตำแหน่งที่ 1
lcd.print(counter_01); //แสดงผล counter_01
} else {
Serial.print("Off");
}
}
down_lastate = down_state; //ให้ค่าสถานะของปุ่มกด down_lastate = down_state
}
}
}
/*---------- Function บันทึกข้อมูล Record ชนิด Multi Task แบบ millis() --------*/
void record_01() {
if (millis() - lt_03 >= P_03) {
lt_03 = millis();
rec_state = digitalRead(rec_button); //อ่านค่าการกดปุ่ม RECORD แล้วมาเก็บค่าไว้ในตัวแปร rec_state
if (rec_state != rec_lastate) { //ถ้าค่า rec_state มีค่าไม่เท่ากับ rec_lastate
if (rec_state == LOW) { //ถ้าค่า rec_state มีค่าเป็น LOW เมื่อมีการกดปุ่ม Record
EEPROM.put(ee_Address_00, counter_01); //ให้นำค่า counter_01 ไปเก็บไว้ที่ตำแหน่ง 0 ของ EEPROM
digitalWrite(led_rec, HIGH); //ให้ led_rec สว่างเมื่อกดปุ่มบันทึก
lcd.setCursor(2, 1); //Set Cursor ของ LCD ไปยังแถวที่ 2 ตำแหน่งที่ 1
lcd.print(" "); //Clear แถวที่ 2 (16 ตัวอักษรให้ว่าง)
lcd.setCursor(2, 1); //Set Cursor ของ LCD ไปยังแถวที่ 2 ตำแหน่งที่ 1
lcd.print(counter_01); //แสดงผล counter_01
} else {
digitalWrite(led_rec, LOW); //ให้ led_rec ดับเมื่อบันทึก เรียบร้อย
}
}
rec_lastate = rec_state; //ให้ค่าสถานะของปุ่มกด rec_lastate = rec_state
}
}