我使用的是Arduino Mega兼容板(SainSmart Mega 2560 R3 ATmega2560-16AU),它附带了一个rtc模块(v1.1 ds1307rtc)并使用附加的代码进行处理。我已经使用了TimeAlarms库(从http://www.pjrc.com/teensy/td_libs_TimeAlarms.html下载它)来每小时发出一次警报。警报应该每小时在不同的时间发生,但是为了进行测试,我将它们全部设置为第12分钟。 此代码正在等待正确的时间,我可以通过usb,串行接口设置。代码在大多数情况下工作正常。但有时警报没有激活,我的LED也没有闪烁。我不知道为什么,当这种情况发生时,我在工作闪烁和不工作之间没有任何变化。此外,我无法看到它没有正常工作的任何时间,但是它工作正常,但有时会失败。如果它失效,看起来失败小时后的所有小时也都失败并且没有触发警报。 我知道TimeAlarms.h中6个报警的限制,并将此文件中的变量dtNBR_ALARMS设置为25。
正如你所看到的,我已经实现了printTime函数,它打印出rtc和系统时间,并且两者都是正确的。
有谁知道我做错了什么或为什么有时会失败?
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h>
tmElements_t tm;
int pwm_2 = 2;
int pwm_3 = 3;
int pwm_4 = 4;
int pwm_5 = 5;
int pwm_6 = 6;
int pwm_7 = 7;
int pwm_8 = 8;
int pwm_9 = 9;
int pwm_10 = 10;
int pwm_11 = 11;
int pwm_12 = 12;
int pwm_13 = 13;
//delay in the for loops
int dly = 120;
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
setSyncProvider(RTC.get); // the function to get the time from the RTC
RTC.read(tm);
setTime(tm.Hour,tm.Minute,tm.Second,tm.Day,tm.Month,tm.Year);
printTime();
//wait for time input via serial, time must be set at every system boot
while(tmYearToCalendar(tm.Year) < 2014) {
Serial.println("wait in time loop, you have to set a time, current time is not correct:");
printTime();
if (Serial.available()) {
time_t t = processSyncMessage();
Serial.println("wait in time loop");
if (t != 0) {
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
Serial.println("time is succesfully set to");
printTime();
break;
}
}
delay(1000);
}
Serial.println("Time is already set to:");
printTime();
//set alarms for all hours in UTC, not in CET
Alarm.alarmRepeat(0, 12, 0, shotAlarm);
Alarm.alarmRepeat(1, 12, 0, shotAlarm);
Alarm.alarmRepeat(2, 12, 0, shotAlarm);
Alarm.alarmRepeat(3, 12, 0, shotAlarm);
Alarm.alarmRepeat(4, 12, 0, shotAlarm);
Alarm.alarmRepeat(5, 12, 0, shotAlarm);
Alarm.alarmRepeat(6, 12, 0, shotAlarm);
Alarm.alarmRepeat(7, 12, 0, shotAlarm);
Alarm.alarmRepeat(8, 12, 0, shotAlarm);
Alarm.alarmRepeat(9, 12, 0, shotAlarm);
Alarm.alarmRepeat(10, 12, 0, shotAlarm);
Alarm.alarmRepeat(11, 12, 0, shotAlarm);
Alarm.alarmRepeat(12, 12, 0, shotAlarm);
Alarm.alarmRepeat(13, 12, 0, shotAlarm);
Alarm.alarmRepeat(14, 12, 0, shotAlarm);
Alarm.alarmRepeat(15, 12, 0, shotAlarm);
Alarm.alarmRepeat(16, 12, 0, shotAlarm);
Alarm.alarmRepeat(17, 12, 0, shotAlarm);
Alarm.alarmRepeat(18, 12, 0, shotAlarm);
Alarm.alarmRepeat(19, 12, 0, shotAlarm);
Alarm.alarmRepeat(20, 12, 0, shotAlarm);
Alarm.alarmRepeat(21, 12, 0, shotAlarm);
Alarm.alarmRepeat(22, 12, 0, shotAlarm);
Alarm.alarmRepeat(23, 12, 0, shotAlarm);
// declare pin 2-13 to be an output:
pinMode(pwm_2, OUTPUT); //red
pinMode(pwm_3, OUTPUT); //blue
pinMode(pwm_4, OUTPUT); //green
pinMode(pwm_5, OUTPUT); //red
pinMode(pwm_6, OUTPUT); //blue
pinMode(pwm_7, OUTPUT); //green
pinMode(pwm_8, OUTPUT); //red
pinMode(pwm_9, OUTPUT); //blue
pinMode(pwm_10, OUTPUT); //green
pinMode(pwm_11, OUTPUT); //red
pinMode(pwm_12, OUTPUT); //blue
pinMode(pwm_13, OUTPUT); //green
}
void shotAlarm() {
Serial.println("SHOTALARM");
analogWrite(pwm_2, 255);
analogWrite(pwm_5, 255);
analogWrite(pwm_8, 255);
analogWrite(pwm_11, 255);
analogWrite(pwm_3, 255);
analogWrite(pwm_4, 255);
analogWrite(pwm_6, 255);
analogWrite(pwm_7, 255);
analogWrite(pwm_9, 255);
analogWrite(pwm_10, 255);
analogWrite(pwm_12, 255);
analogWrite(pwm_13, 255);
for(int a = 0; a < 60; a = a+1) {
for (int i = 0; i < 255; i = i + 1) {
analogWrite(pwm_2, i); //red
analogWrite(pwm_5, i); //red
analogWrite(pwm_8, i); //red
analogWrite(pwm_11, i); //red
delay (5);
}
for (int i = 255; i > 0; i = i - 1) {
analogWrite(pwm_2, i); //red
analogWrite(pwm_5, i); //red
analogWrite(pwm_8, i); //red
analogWrite(pwm_11, i); //red
delay (5);
}
}
}
void loop() {
Alarm.delay(0);
Serial.println("new loop");
printTime();
analogWrite(pwm_2, 0);
analogWrite(pwm_5, 0);
analogWrite(pwm_8, 0);
analogWrite(pwm_11, 0);
analogWrite(pwm_3, 255);
analogWrite(pwm_4, 255);
analogWrite(pwm_6, 255);
analogWrite(pwm_7, 255);
analogWrite(pwm_9, 255);
analogWrite(pwm_10, 255);
analogWrite(pwm_12, 255);
analogWrite(pwm_13, 255);
for (int i = 255; i > 0; i = i - 1) {
analogWrite(pwm_4, i); //green
analogWrite(pwm_7, i); //green
analogWrite(pwm_10, i); //green
analogWrite(pwm_13, i); //green
Alarm.delay (dly);
}
for (int i = 0; i < 255; i = i + 1) {
analogWrite(pwm_2, i); //red
analogWrite(pwm_5, i); //red
analogWrite(pwm_8, i); //red
analogWrite(pwm_11, i); //red
Alarm.delay (dly);
}
for (int i = 255; i > 0; i = i - 1) {
analogWrite(pwm_3, i); //blue
analogWrite(pwm_6, i); //blue
analogWrite(pwm_9, i); //blue
analogWrite(pwm_12, i); //blue
Alarm.delay (dly);
}
for (int i = 0; i < 255; i = i + 1) {
analogWrite(pwm_4, i); //green
analogWrite(pwm_7, i); //green
analogWrite(pwm_10, i); //green
analogWrite(pwm_13, i); //green
Alarm.delay (dly);
}
for (int i = 255; i > 0; i = i - 1) {
analogWrite(pwm_2, i); //red
analogWrite(pwm_5, i); //red
analogWrite(pwm_8, i); //red
analogWrite(pwm_11, i); //red
Alarm.delay (dly);
}
for (int i = 0; i < 255; i = i + 1) {
analogWrite(pwm_3, i); //blue
analogWrite(pwm_6, i); //blue
analogWrite(pwm_9, i); //blue
analogWrite(pwm_12, i); //blue
Alarm.delay (dly);
}
Alarm.delay(0);
}
void printTime() {
if (RTC.read(tm)) {
Serial.print("Ok, RTC Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
}
else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
}
else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
}
Serial.print("Ok, System Time = ");
print2digits(hour());
Serial.write(':');
print2digits(minute());
Serial.write(':');
print2digits(second());
Serial.print(", Date (D/M/Y) = ");
Serial.print(day());
Serial.write('/');
Serial.print(month());
Serial.write('/');
Serial.print(year());
Serial.println();
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
return pctime;
if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
pctime = 0L; // return 0 to indicate that the time is not valid
}
}
return pctime;
}
答案 0 :(得分:1)
对于以后阅读此帖子的所有人: 我在代码中发现了错误。以下行错了:
setTime(tm.Hour,tm.Minute,tm.Second,tm.Day,tm.Month,tm.Year);
正确的版本是:
setTime(tm.Hour,tm.Minute,tm.Second,tm.Day,tm.Month,tmYearToCalendar(tm.Year));
不幸的是,我忘了将年份值转换为预期的格式。上面的代码对这种修正非常好。我检查了警报超过一周,它的工作非常稳定。
感谢所有人的帮助。
答案 1 :(得分:0)
有一点需要注意的是,在TimerAlarm
库的阅读中,它表示您最多只能定义6个警报,但可以通过dtNBR_ALARMS
字段在库内更改。 ram限制了数量,但你可以很容易地补充说,因为Arduino mega有一个非常大的SRAM。因此,如果您遇到警报的其他问题,这可能值得一看。