用mCookie做arduino闹钟的程序代码,元件组成和图片需要哪些
时间: 2024-02-23 17:59:41 浏览: 77
要用mCookie做Arduino闹钟,你需要以下元件:
- mCookie-CoreUSB主控板
- mCookie-Hub扩展板
- mCookie-OLED显示屏
- mCookie-RTC实时时钟模块
- mCookie-Buzzer蜂鸣器模块
- mCookie-Button按钮模块
下面是一个简单的Arduino闹钟程序代码示例,你可以根据需要进行修改和优化:
```c++
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS3231.h>
#include <SimpleTimer.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DS3231 rtc(SDA, SCL);
SimpleTimer timer;
bool alarmEnabled = false;
bool alarmRinging = false;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(10, 10);
display.println("Alarm Clock");
display.display();
rtc.begin();
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), snoozeAlarm, FALLING);
timer.setInterval(1000, updateTime);
timer.setInterval(500, updateAlarm);
}
void loop() {
timer.run();
}
void updateTime() {
DateTime now = rtc.now();
display.clearDisplay();
display.setCursor(10, 10);
display.print(now.hour(), DEC);
display.print(":");
display.print(now.minute(), DEC);
display.print(":");
display.println(now.second(), DEC);
if (alarmEnabled) {
display.setCursor(10, 40);
display.print("Alarm On");
}
display.display();
if (alarmEnabled && now.hour() == alarmHour && now.minute() == alarmMinute && now.second() == 0) {
alarmRinging = true;
}
if (alarmRinging) {
tone(3, 1000, 500);
delay(500);
}
}
void updateAlarm() {
if (digitalRead(2) == LOW) {
snoozeAlarm();
}
}
void snoozeAlarm() {
alarmRinging = false;
delay(5000);
alarmRinging = true;
}
void setAlarm(int hour, int minute) {
alarmHour = hour;
alarmMinute = minute;
alarmEnabled = true;
}
```
这个程序使用了mCookie-CoreUSB主控板、mCookie-Hub扩展板、mCookie-OLED显示屏、mCookie-RTC实时时钟模块、mCookie-Buzzer蜂鸣器模块和mCookie-Button按钮模块。当程序运行时,它将显示当前时间。如果启用了闹钟,它还将显示“Alarm On”。如果闹钟时间到了,蜂鸣器将响起。你可以使用按钮模块来打盹,延迟闹钟响铃。
阅读全文