RTC + Scheduler 使用 Arduino mini

RTC + Scheduler using Arduino mini

我正在为我的项目和 RTC - 实时时钟模块 DS1307 使用 Arduino mini 5V 我想在特定时间唤醒电路板并运行一个功能。 (蜂鸣器接D3)

当我单独使用 TimeAlarm 并手动设置时间时,一切正常:

#include "Time.h"
#include "TimeAlarms.h"

void setup(){
  setTime(22,29,55,12,31,14); // set time to Saturday 8:29:00am Jan 1 2011

  Alarm.alarmRepeat(10,30,0,buzz);  // 10:30am every day
  Alarm.alarmRepeat(16,30,0,buzz);  // 4:30pm every day 
  Alarm.alarmRepeat(22,30,0,buzz);  // 10:30pm every day 

  Serial.begin(9600);
}

void loop(){
  digitalClockDisplay();
  Alarm.delay(1000);
}

void buzz(){
   tone(3, 220, 1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits){
  Serial.print(":");
  if(digits < 10)
  Serial.print('0');
  Serial.print(digits);
}

然而,当我使用 RTC 时,蜂鸣器功能不会被调用,它仍然会打印时间

#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
#include "TimeAlarms.h"

RTC_Millis rtc;

void setup(){
  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));

  Alarm.alarmRepeat(10,30,0,buzz);  // 10:30am every day
  Alarm.alarmRepeat(16,30,0,buzz);  // 4:30pm every day 
  Alarm.alarmRepeat(22,30,0,buzz);  // 10:30pm every day -- modify this to your current time when running the example

  Serial.begin(9600);
}

void loop(){
  //printing the current time
  DateTime now = rtc.now();

  Serial.print(now.year());
  Serial.print('/');
  Serial.print(now.month());
  Serial.print('/');
  Serial.print(now.day());
  Serial.print(' ');
  Serial.print(now.hour());
  Serial.print(':');
  Serial.print(now.minute());
  Serial.print(':');
  Serial.print(now.second());
  Serial.println();
  Alarm.delay(1000); // wait one second between clock display
}

void buzz(){
  tone(3, 220, 1000);
}

好的,所以我找到了答案:

第一个问题是:使用 RTC_DS1307 而不是 RTC_Millis

RTC ds1307 指的是引脚 12C,在 Arduino mini 中它们位于 A3 和 VCC 之上。他们需要焊接。一旦完成,它们应该连接到 SDA 和 SCL,使用 M/F 电线

SDA -> to the pin above A3
SCL -> to the pin above VCC

然后我把代码改成了下面这样:

#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
#include "TimeAlarms.h"

RTC_DS1307 rtc;
const int output = 3;

uint32_t syncProvider()//function which sets up the RTC as the source of external time{
  return rtc.now().unixtime();
}


void setup(){
  Wire.begin();
  rtc.begin();
  rtc.adjust(DateTime(__DATE__, __TIME__));//comment this out when the RTC has been set
  setSyncProvider(syncProvider);   // the function to get the time from the RTC


  Alarm.alarmRepeat(10,30,0,buzzer);  // 10:30am every day
  Alarm.alarmRepeat(16,30,0,buzzer);  // 4:30pm every day 
  Alarm.alarmRepeat(22,30,00,buzzer);  // 10:30pm every day 

  pinMode(output , OUTPUT);//new line
  Serial.begin(9600);
}

void loop(){

  //printing the current time
  DateTime now = rtc.now();

  Serial.print(now.year());
  Serial.print('/');
  Serial.print(now.month());
  Serial.print('/');
  Serial.print(now.day());
  Serial.print(' ');
  Serial.print(now.hour());
  Serial.print(':');
  Serial.print(now.minute());
  Serial.print(':');
  Serial.print(now.second());
  Serial.println();
  Alarm.delay(1000); // wait one second between clock display
}

void buzzer(){
 //Do Stuff
}

只是为了帮助像我这样使用复制粘贴尝试过此代码并出现错误的菜鸟

named return values are no longer supported Error compiling.

通过网络搜索后发现臭名昭著的{大括号是评论的一部分所以请将其更改为

uint32_t syncProvider() { //function which sets up the RTC as the source of external time       
       return rtc . now() . unixtime(); 
   }