อยากตั้งเป็นช่วงเวลา 2 ช่วงเวลาให้ทำงสาน 2 ครั้งต้องเขียนยังไงครับ พอดีเขียนแล้วได้ครั้งเดียวครับ
#include "Wire.h"
#include "SPI.h"
#include "RTClib.h"
#include <Servo.h>
Servo myservo;
RTC_DS3231 RTC;
void setup () {
myservo.attach(4);
Serial.begin(9600);
Wire.begin();
RTC.begin();
//RTC.adjust(DateTime(__DATE__, __TIME__));
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
DateTime now = RTC.now();
/* ตั้งเวลา ในตัวอย่างนี้ เซตค่าเป็นเวลา 23:09 ถ้าถึงเวลานี้จะให้ทำงานที่ฟังก์ชัน
RTC.setAlarm1Simple(23, 9);
if (RTC.checkIfAlarm(1)) {
Serial.println("Alarm Triggered");
}*/
RTC.setAlarm1Simple(00, 00);
RTC.turnOnAlarm(1);
if (RTC.checkAlarmEnabled(1)) {
Serial.println("Alarm Enabled");
}
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
//ตั้งเวลา ถึงเลาServoทำงาน//
if (RTC.checkIfAlarm(1)) {
myservo.write(0);
delay(1000);
myservo.write(360);
}
Serial.println();
delay(1000);
}
ช่วงสีแดงผมตั้งให้เที่ยงคืนทำงาน 1 ครั้ง แต่ลองตั้งอีเว้นเพิ่มมันทำแต่อันที่ 2 แทนอันที่ 1 ไม่ทำครับ รบกวนช่วยหน่อยครับ
จากไลบารี การใช้งานคล้ายกัน
อ้างอิงจากไลบารี https://github.com/danielknox/rtclib/blob/master/RTClib.h
เรียกใช้ฟังก์ชันให้ถูกต้อง void setAlarm2Simple(byte hour, byte minute);
แต่จากข้อมูลพบว่า setAlarm2Simple อาจจะไม่ทำงาน
ให้ลองเปลี่ยนมาใช้ไลบาลีตัวนี้แทน
https://github.com/FabioCuomo/FabioCuomo-DS3231
อีกวิธีคือ ใช้แค่ setAlarm1Simple() โดยเมื่อทำงานเวลาแรกเสร็จ ให้ตั้งเวลาใหม่เป็นครั้งต่อไปครับ
มีโคดตัวอย่างในไลบารีหลายแบบลองแกะตัวอย่าง
ตามที่แนะนำไปแล้ว คือ
ถ้าตั้งครั้งแรกทำงานตอน 12.00 เมื่อถึงเวลา 12.00 ให้เปลี่ยนเป็นตั้วเวลาใหม่คือ 24.00
เมื่อถึงเวลา 24.00 ก็ตั้งใหม่เป็น 12.00 สลับกันแบบนี้ครับ