Introduction: Conta-giorni Arduino - Progetto Arduino Per San Valentino ❤️ Conta I Giorni Insieme Con Un Display a 7 Segmenti - Tutorial
Stai cercando un regalo di San Valentino originale e romantico per la tua dolce metà? In questo video ti mostrerò come realizzare un conta-giorni personalizzato con Arduino e un display a 7 segmenti. Un regalo tech e fatto a mano che sorprenderà la tua metà e le dimostrerà il tuo amore!
Supplies
- Modulo RTC ds1307
- Arduino (qualsiasi)
- Display 7 segmenti
Step 1: 0:32 - Collegamento RTC
Collegamenti da effettuare tra il modulo RTC e Arduino
Step 2: 0:44 - Impostare L'ora Sull' RTC
Link alla libreria da scaricare: https://www.arduino.cc/reference/en/libraries/ds1307rtc/
Sketch da caricare:
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
void setup() {
bool parse=false;
bool config=false;
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.print("DS1307 configured Time=");
Serial.print(__TIME__);
Serial.print(", Date=");
Serial.println(__DATE__);
} else if (parse) {
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
} else {
Serial.print("Could not parse info from the compiler, Time=\"");
Serial.print(__TIME__);
Serial.print("\", Date=\"");
Serial.print(__DATE__);
Serial.println("\"");
}
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
Step 3: 2:29 - Collegamento Display
Link alla libreria da usare: https://www.arduino.cc/reference/en/libraries/sevseg/
Step 4: 2:49 - Codice
Codice da caricare per ultimo:
#include "SevSeg.h"
SevSeg sevseg;
#include <TimeLib.h>
#include <DS1307RTC.h>
void setup() {
byte numDigits = 1;
byte digitPins[] = {};
byte segmentPins[] = {6, 5, 2, 3, 4, 7, 8, 9};
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
Serial.begin(9600);
}
void loop() {
// Ottieni la data e l'ora corrente dal modulo RTC
tmElements_t now;
RTC.read(now);
// Imposta la data di riferimento
tmElements_t targetDate;
targetDate.Second = 0;
targetDate.Minute = 0;
targetDate.Hour = 0;
targetDate.Day = 13;
targetDate.Month = 2;
targetDate.Year = 2023 - 1970;
// Calcola la differenza di tempo in secondi tra la data attuale e la data di riferimento
time_t nowEpoch = makeTime(now);
time_t targetEpoch = makeTime(targetDate);
int32_t diffSeconds = nowEpoch - targetEpoch;
// Converti i secondi in giorni
int32_t daysPassed = diffSeconds / 86400; // 86400 secondi in un giorno
// Estrai le cifre individuali dei giorni passati
int digit1 = daysPassed / 1000;
int digit2 = (daysPassed / 100) % 10;
int digit3 = (daysPassed / 10) % 10;
int digit4 = daysPassed % 10;
// Stampa le cifre individuali dei giorni passati
Serial.println("Cifre individuali dei giorni passati:");
Serial.print("Prima cifra: ");
Serial.println(digit1);
Serial.print("Seconda cifra: ");
Serial.println(digit2);
Serial.print("Terza cifra: ");
Serial.println(digit3);
Serial.print("Quarta cifra: ");
Serial.println(digit4);
if (digit1 != 0) {
sevseg.setNumber(digit1);
sevseg.refreshDisplay();
delay(500);
}
sevseg.setNumber(digit2);
sevseg.refreshDisplay();
delay(500);
sevseg.setNumber(digit3);
sevseg.refreshDisplay();
delay(500);
sevseg.setNumber(digit4);
sevseg.refreshDisplay();
delay(500);
sevseg.blank();
}
Step 5: Fine
Guarda Il Video Per Maggiori Info: