Indice
Descrizione
In questo video scoprirai come misurare il tempo nei progetti con Arduino tramite il modulo RTC (Real Time clock).
Vedremo quali sono i collegamenti da fare e come scrivere lo sketch per visualizzare e modificare la data e l'ora sul display LCD
Inoltre imparerai come sfruttare gli allarmi messo a disposizione dal modulo per realizzare delle sveglie personalizzate.
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup () {
rtc.begin();
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop () {
DateTime now = rtc.now();
printDateTime(now);
delay(1000);
}
void printDateTime(DateTime dt) {
char dateBuffer[] = " DD/MM/YYYY ";
char timeBuffer[] = " hh:mm:ss ";
lcd.setCursor(0, 0);
lcd.print(dt.toString(dateBuffer));
lcd.setCursor(0, 1);
lcd.print(dt.toString(timeBuffer));
}
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup () {
Serial.begin(9600);
rtc.begin();
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop () {
if (Serial.available()) {
String buffer = Serial.readStringUntil('\n');
setDateTime(buffer);
}
DateTime now = rtc.now();
printDateTime(now);
delay(1000);
}
void printDateTime(DateTime dt) {
char dateBuffer[] = " DD/MM/YYYY ";
char timeBuffer[] = " hh:mm:ss ";
lcd.setCursor(0, 0);
lcd.print(dt.toString(dateBuffer));
lcd.setCursor(0, 1);
lcd.print(dt.toString(timeBuffer));
}
void setDateTime(String value) {
int d, m, y, h, min, s;
int n = sscanf(value.c_str(), "%d %d %d %d %d %d", &d, &m, &y, &h, &min, &s);
if (n == 6) {
rtc.adjust(DateTime(y, m, d, h, min, s));
}
}
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#define LED_PIN 7
#define BUZZER_PIN 8
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup () {
Serial.begin(9600);
rtc.begin();
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
rtc.disableAlarm(1);
rtc.disableAlarm(2);
rtc.clearAlarm(1);
rtc.clearAlarm(2);
rtc.writeSqwPinMode(DS3231_OFF);
rtc.setAlarm1(DateTime("2000-01-01T00:00:10"), DS3231_A1_Second);
rtc.setAlarm2(DateTime("2000-01-01T08:00:00"), DS3231_A2_Hour);
}
void loop () {
if (Serial.available()) {
String buffer = Serial.readStringUntil('\n');
setDateTime(buffer);
}
DateTime now = rtc.now();
printDateTime(now);
if (rtc.alarmFired(1)) {
ledAlarm();
rtc.clearAlarm(1);
} else if (rtc.alarmFired(2)) {
buzzerAlarm();
rtc.clearAlarm(2);
} else {
delay(1000);
}
}
void printDateTime(DateTime dt) {
char dateBuffer[] = " DD/MM/YYYY ";
char timeBuffer[] = " hh:mm:ss ";
lcd.setCursor(0, 0);
lcd.print(dt.toString(dateBuffer));
lcd.setCursor(0, 1);
lcd.print(dt.toString(timeBuffer));
}
void setDateTime(String value) {
int d, m, y, h, min, s;
int n = sscanf(value.c_str(), "%d %d %d %d %d %d", &d, &m, &y, &h, &min, &s);
if (n == 6) {
rtc.adjust(DateTime(y, m, d, h, min, s));
}
}
void ledAlarm() {
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
}
}
void buzzerAlarm() {
for (int i = 0; i < 5; i++) {
tone(BUZZER_PIN, 1047);
delay(100);
noTone(BUZZER_PIN);
delay(100);
}
}