ใบงานที่12 arduino หาเอง
ใบรายงานผลการปฏิบัติงาน
/* DigitalClockRTC1307 By IOXhop.com */
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
unsigned long previousMillis;
bool showdot = false;
String SerialGET = "";
TM1637Display display(CLK, DIO);
void setup() {
Serial.begin(9600);
}
void loop() {
tmElements_t tm;
if (!RTC.read(tm)) {
tm.Hour = 0;
tm.Minute = 0;
tm.Second = 0;
RTC.write(tm);
}
display.write(tm.Hour/10, 0);
display.write(tm.Hour%10, 1);
display.write(tm.Minute/10, 2);
display.write(tm.Minute%10, 3);
// Dot blink
unsigned long currentMillis = millis();
if (currentMillis-previousMillis>500){
previousMillis = currentMillis;
display.dotShow(showdot=!showdot);
}
while (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
int Hour, Min, Sec;
if (sscanf(SerialGET.c_str(), "%d:%d:%d", &Hour, &Min, &Sec) == 3) {
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
RTC.write(tm);
Serial.print("Set to ");
Serial.print(Hour);
Serial.print(":");
Serial.print(Min);
Serial.print(":");
Serial.print(Sec);
Serial.println();
}else{
Serial.println("Invalid format, Please enter Hour:Min:Sec");
}
SerialGET = "";
}
else
SerialGET += c;
}
}
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
unsigned long previousMillis;
bool showdot = false;
String SerialGET = "";
TM1637Display display(CLK, DIO);
void setup() {
Serial.begin(9600);
}
void loop() {
tmElements_t tm;
if (!RTC.read(tm)) {
tm.Hour = 0;
tm.Minute = 0;
tm.Second = 0;
RTC.write(tm);
}
display.write(tm.Hour/10, 0);
display.write(tm.Hour%10, 1);
display.write(tm.Minute/10, 2);
display.write(tm.Minute%10, 3);
// Dot blink
unsigned long currentMillis = millis();
if (currentMillis-previousMillis>500){
previousMillis = currentMillis;
display.dotShow(showdot=!showdot);
}
while (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
int Hour, Min, Sec;
if (sscanf(SerialGET.c_str(), "%d:%d:%d", &Hour, &Min, &Sec) == 3) {
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
RTC.write(tm);
Serial.print("Set to ");
Serial.print(Hour);
Serial.print(":");
Serial.print(Min);
Serial.print(":");
Serial.print(Sec);
Serial.println();
}else{
Serial.println("Invalid format, Please enter Hour:Min:Sec");
}
SerialGET = "";
}
else
SerialGET += c;
}
}
}
3. อธิบายการทำงานของโปรแกรม
นาฬิกา 24 ชม
4.อ้างอิง
https://www.ioxhop.com/article/24/%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87%E0%B8%99%E0%B8%B2%E0%B8%AC%E0%B8%B4%E0%B8%81%E0%B8%B2%E0%B8%94%E0%B8%B4%E0%B8%88%E0%B8%B4%E0%B8%95%E0%B8%AD%E0%B8%A5%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B8%9A%E0%B8%AD%E0%B8%A3%E0%B9%8C%E0%B8%94-arduino
ความคิดเห็น
แสดงความคิดเห็น