14-07-2023, 21:33
Kod pochodzi z https://starter-kit.nettigo.pl/2018/01/zegar-z-arduino/
Zegar działa, ale nie potrafię ustawić w nim czasu. Sposoby sugerowane na stronie nie wywołują reakcji poza mrugnięciem diody na płytce. Proszę o pomoc. Pozdr. Andrzej.
#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
DS3231 clock;
void setup() {
Wire.begin();
lcd.begin(16, 2);
lcd.setBacklight(true);
lcd.print("Zagar");
lcd.setCursor(0, 1);
lcd.print("Arduino");
delay(1000);
lcd.clear();
Serial.begin(9600);
// Ustawienie czasu w momencie inicjacji układu:
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(00,01,11,7,11,03,17);
}
void loop() {
displayDateTime();
delay(1000);
}
void displayDateTime() {
lcd.clear();
lcd.print(getDateString());
lcd.setCursor(0, 1);
lcd.print(getTimeString());
}
String getDateString() {
bool century;
String day = toStringWithLeadingZeros(clock.getDate());
String month = toStringWithLeadingZeros(clock.getMonth(century));
String year = String(clock.getYear() + 2023);
return day + "." + month + "." + year + "r.";
}
String getTimeString() {
bool h12, pm;
String hour = toStringWithLeadingZeros(clock.getHour(h12, pm));
String minute = toStringWithLeadingZeros(clock.getMinute());
String second = toStringWithLeadingZeros(clock.getSecond());
return hour + ":" + minute + ":" + second;
}
String toStringWithLeadingZeros(byte number) {
String result = String(number);
if(number < 10) {
result = "0" + result;
}
return result;
}
Zegar działa, ale nie potrafię ustawić w nim czasu. Sposoby sugerowane na stronie nie wywołują reakcji poza mrugnięciem diody na płytce. Proszę o pomoc. Pozdr. Andrzej.
#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
DS3231 clock;
void setup() {
Wire.begin();
lcd.begin(16, 2);
lcd.setBacklight(true);
lcd.print("Zagar");
lcd.setCursor(0, 1);
lcd.print("Arduino");
delay(1000);
lcd.clear();
Serial.begin(9600);
// Ustawienie czasu w momencie inicjacji układu:
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(00,01,11,7,11,03,17);
}
void loop() {
displayDateTime();
delay(1000);
}
void displayDateTime() {
lcd.clear();
lcd.print(getDateString());
lcd.setCursor(0, 1);
lcd.print(getTimeString());
}
String getDateString() {
bool century;
String day = toStringWithLeadingZeros(clock.getDate());
String month = toStringWithLeadingZeros(clock.getMonth(century));
String year = String(clock.getYear() + 2023);
return day + "." + month + "." + year + "r.";
}
String getTimeString() {
bool h12, pm;
String hour = toStringWithLeadingZeros(clock.getHour(h12, pm));
String minute = toStringWithLeadingZeros(clock.getMinute());
String second = toStringWithLeadingZeros(clock.getSecond());
return hour + ":" + minute + ":" + second;
}
String toStringWithLeadingZeros(byte number) {
String result = String(number);
if(number < 10) {
result = "0" + result;
}
return result;
}