ผมต้องการให้บอร์ด MEGA 2560 ที่ต่อกับ
1. DS3231 Module
2. DHT22 / AM2302
นำค่าที่อ่านได้จากข้อ 1 และ 2 บันทึกลง Micro SD Card Module
ผมได้ลองประยุกต์ใช้ code ตามตัวอย่าง ในเวปแล้วตอนนี้สามารถอ่านค่าจากข้อ 1 และ 2 ได้แล้ว แต่ยังไม่สามารถเอาข้อมูลที่อ่านได้ ไปบันทึกลง Micro SD Card Module ได้
รบกวนช่วยดู Code และแนะนำหน่อยครับ
#include "Wire.h"
#include "SPI.h" // not used here, but needed to prevent a RTClib compile error
#include "RTClib.h"
#include "DHT.h"
RTC_DS3231 RTC;
DHT dht;
void setup () {
Serial.begin(9600);
Serial.println();
Serial.println("Date\t\tTime\t\tRT-Temp(C)\tStatus\tHumidity (%)\tTemperature (C)\t(F)");
dht.setup(2); // data pin 2
Wire.begin();
RTC.begin();
// RTC.adjust(DateTime(__DATE__, __TIME__));
DateTime now = RTC.now();
/* ตั้งเวลา ในตัวอย่างนี้ เซตค่าเป็นเวลา 23:09 ถ้าถึงเวลานี้จะให้ทำงานที่ฟังก์ชัน
RTC.setAlarm1Simple(23, 9);
if (RTC.checkIfAlarm(1)) {
Serial.println("Alarm Triggered");
}*/
// Read data from DHT22 and RT-Time
}
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("\t");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print("\t\t");
Serial.print(RTC.getTemperature()); // คำสั่งดึงอุณหภูมิออกมาแสดง
//Serial.print(" C");
//delay(dht.getMinimumSamplingPeriod());
Serial.print("\t\t");
Serial.print(dht.getStatusString());
float humidity = dht.getHumidity(); // ดึงค่าความชื้น
float temperature = dht.getTemperature(); // ดึงค่าอุณหภูมิ
Serial.print("\t");
Serial.print(humidity, 1);
Serial.print("\t\t");
Serial.print(temperature, 1);
Serial.print("\t\t");
Serial.println(dht.toFahrenheit(temperature), 1);
//Serial.println("By Gorn");
Serial.println();
delay(2000);
}
ตัวอย่างโคดเป็นของ arduino unoสำหรับ arduno mega จะขาคนละขากันครับ
ลองดูที่ลิงค์นี้ครับ http://www.arduinoall.com/webboard/viewtopic/18
และลิงค์นี้ครับ http://www.arduinoall.com/article/16
ขอบคุณครับ ตอนนี้แก้ไขได้แล้งครับ แต่เจอปัญหาใหม่คือ
1. เจ้าโมดูลนาฬิกา DS3231 ที่บันทึกค่าไว้เดินช้ากว่าปกติมากเลยครับ ผมไม่รู้ว่ามันเป็นเพราะเวลาที่สั่ง Delay ในโปรแกรม หรือว่าเป้นที่อะไร รบกวนช่วยแนะนำวิธีแก้ไขด้วยครับ
2. รบกวนช่วยแนะนำตัวอย่าง Code สำหรับทำ Timer สำหรับ UNO ที่สามารถตั้งเวลาได้มากกว่า 3 ครั้งให้หน่อยครับ
ขอบคุณครับ
1. เจ้าโมดูลนาฬิกา DS3231 ที่บันทึกค่าไว้เดินช้ากว่าปกติมากเลยครับ ผมไม่รู้ว่ามันเป็นเพราะเวลาที่สั่ง Delay ในโปรแกรม หรือว่าเป้นที่อะไร รบกวนช่วยแนะนำวิธีแก้ไขด้วยครับ
- realtime clock จะเดินเป็นอิสระ ลองเช็คที่โคดโปรแกรมที่ใช้อีกทีครับ
2. รบกวนช่วยแนะนำตัวอย่าง Code สำหรับทำ Timer สำหรับ UNO ที่สามารถตั้งเวลาได้มากกว่า 3 ครั้งให้หน่อยครับ
- ทำได้ ครับ เขียนโปรแกรมเพิ่มเพื่อเช็คเวลาได้กี่ครั้งก็ได้ ลองศึกษาดูนะครับ
ใช้วิธีดึงค่าเวลามาเปรียบเทียบ if เอาก็ได้ครับ
หรือจะลองดัดแปลงโคดฟังก์ชัน setAlarm1Simple จากไลบารี ที่ไพล์ RTClib.cpp เป็นตัวอย่างก็ได้
void RTC_DS3231::setAlarm1Simple(byte hour, byte minute) {
setA1Time(0, hour, minute, 00, 0b00001000, false, false, false);
}
void RTC_DS3231::setA1Time(byte A1Day, byte A1Hour, byte A1Minute, byte A1Second, byte AlarmBits, bool A1Dy, bool A1h12, bool A1PM) {
// Sets the alarm-1 date and time on the DS3231, using A1* information
byte temp_buffer;
Wire.beginTransmission(DS3231_ADDRESS);
Wire.SEND(0x07); // A1 starts at 07h
// Send A1 second and A1M1
Wire.SEND(bin2bcd(A1Second) | ((AlarmBits & 0b00000001) << 7));
// Send A1 Minute and A1M2
Wire.SEND(bin2bcd(A1Minute) | ((AlarmBits & 0b00000010) << 6));
// Figure out A1 hour
if (A1h12) {
// Start by converting existing time to h12 if it was given in 24h.
if (A1Hour > 12) {
// well, then, this obviously isn't a h12 time, is it?
A1Hour = A1Hour - 12;
A1PM = true;
}
if (A1PM) {
// Afternoon
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A1Hour) | 0b01100000;
} else {
// Morning
// Convert the hour to BCD and add appropriate flags.
temp_buffer = bin2bcd(A1Hour) | 0b01000000;
}
} else {
// Now for 24h
temp_buffer = bin2bcd(A1Hour);
}
temp_buffer = temp_buffer | ((AlarmBits & 0b00000100)<<5);
// A1 hour is figured out, send it
Wire.SEND(temp_buffer);
// Figure out A1 day/date and A1M4
temp_buffer = ((AlarmBits & 0b00001000)<<4) | bin2bcd(A1Day);
if (A1Dy) {
// Set A1 Day/Date flag (Otherwise it's zero)
temp_buffer = temp_buffer | 0b01000000;
}
Wire.SEND(temp_buffer);
// All done!
Wire.endTransmission();
}