• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

question-closed Zegar arduino z modułem DS1302

Mały hosting, OGROMNE możliwości
0 głosów
1,117 wizyt
pytanie zadane 26 grudnia 2019 w Mikrokontrolery przez Marak123 Stary wyjadacz (11,190 p.)
zamknięte 26 grudnia 2019 przez Marak123

Chcę zrobić zegarek wyświetlany na wyświetlaczu za pomocą modułu DS1302 ale biblioteka do tego modułu gdy sekundy lub minuty lub godziny wynosi od 1 do 9 to jest to jedna cyfra a ja chce zrobić żeby gdy będzie np. sekundy od 1 do 9 to przed ta cyfra będzie jeszcze zero żeby to były 2 cyfry a nie jedna i zrobiłem to za pomocą if ale nie działa. Poniżej jest ten skrypt. Wie ktoś dlaczego nie działa albo jak to zrobić inaczej?

#include <LiquidCrystal_I2C.h>
#include <stdio.h>
#include <DS1302.h>

LiquidCrystal_I2C  lcd = LiquidCrystal_I2C(0x27,16, 2);
namespace {
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);
}

void setup() {
  Serial.begin(9600);

  lcd.init();
  lcd.backlight();
  rtc.writeProtect(false);
  rtc.halt(false);
  // Sunday, September 22, 2013 at 01:38:50.
  Time t(2019, 12, 26, 23, 59, 00, Time::kSunday);
  // Set the time and date on the chip.
  rtc.time(t);
  int r;
}
void loop() {
 Time t = rtc.time();
 if(t.sec>=0 and t.sec<=9 and t.min>9 and t.hr>9);{
   lcd.setCursor(0,0);
   lcd.print(t.hr);
   lcd.print(":");
   lcd.print(t.min);
   lcd.print(":");
   lcd.print("0");
   lcd.print(t.sec);
 }
 if(t.sec>9 and t.min>=0 and t.min<=9 and t.hr>9);{
   lcd.setCursor(0,0);
   lcd.print(t.hr);
   lcd.print(":");
   lcd.print("0");
   lcd.print(t.min);
   lcd.print(":");
   lcd.print(t.sec);
 }
 if(t.sec>9 and t.min>9 and t.hr>=0  and t.hr<=9);{
   lcd.setCursor(0,0);
   lcd.print("0");
   lcd.print(t.hr);
   lcd.print(":");
   lcd.print(t.min);
   lcd.print(":");
   lcd.print(t.sec);
 }
 if(t.sec>=0 and t.sec<=9 and t.min>=0 and t.min<=9 and t.hr>9);{
   lcd.setCursor(0,0);
   lcd.print(t.hr);
   lcd.print(":");
   lcd.print("0");
   lcd.print(t.min);
   lcd.print(":");
   lcd.print("0");
   lcd.print(t.sec);
 }
 if(t.sec>=0 and t.sec<=9 and t.min>9 and t.hr>=0 and t.hr<=9);{
   lcd.setCursor(0,0);
   lcd.print("0");
   lcd.print(t.hr);
   lcd.print(":");
   lcd.print(t.min);
   lcd.print(":");
   lcd.print("0");
   lcd.print(t.sec);
 }
 if(t.sec>9 and t.min>=0 and t.min<=9 and t.hr>=0 and t.hr<=9);{
   lcd.setCursor(0,0);
   lcd.print("0");
   lcd.print(t.hr);
   lcd.print(":");
   lcd.print("0");
   lcd.print(t.min);
   lcd.print(":");
   lcd.print(t.sec);
 }
 if(t.sec>=0 and t.sec<=9 and t.min>=0 and t.min<=9 and t.hr>=0 and t.hr<=9);{
   lcd.setCursor(0,0);
   lcd.print("0");
   lcd.print(t.hr);
   lcd.print(":");
   lcd.print("0");
   lcd.print(t.min);
   lcd.print(":");
   lcd.print("0");
   lcd.print(t.sec);
 }
 if(t.sec>9 and t.min>9 and t.hr>9);{
  lcd.setCursor(0,0);
  lcd.print(t.hr);
  lcd.print(":");
  lcd.print(t.min);
  lcd.print(":");
  lcd.print(t.sec);
 }
 delay(999);
}
komentarz zamknięcia: Problem rozwiązany
komentarz 26 grudnia 2019 przez Marak123 Stary wyjadacz (11,190 p.)

Pokombinowałem trochę i wymyśliłem jak to zrobić. Nie wiem czy da się jakoś inaczej pewnie się da ale ja to tak zrobiłem.

#include <LiquidCrystal_I2C.h>
#include <stdio.h>
#include <DS1302.h>

LiquidCrystal_I2C  lcd = LiquidCrystal_I2C(0x27,16, 2);
namespace {
const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);
}

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  rtc.writeProtect(false);
  rtc.halt(false);
  // Sunday, September 22, 2013 at 01:38:50.  
  Time t(2019, 12, 26, 9, 59, 50, Time::kSunday);
  // Set the time and date on the chip.
  rtc.time(t);
}
void sekunda(){
  Time t = rtc.time();
  if(t.sec>=0 and t.sec<=9){
    lcd.print("0");
    lcd.print(t.sec);
  }
  else{
    lcd.print(t.sec);
  }
}
void minuta(){
  Time t = rtc.time();
  if(t.min>=0 and t.min<=9){
    lcd.print("0");
    lcd.print(t.min);
  }
  else{
    lcd.print(t.min);
  }
}
void godzina(){
  Time t = rtc.time();
  if(t.hr>=0 and t.hr<=9){
    lcd.print("0");
    lcd.print(t.hr);
  }
  else{
    lcd.print(t.hr);
  }
}
void loop() {
  lcd.setCursor(0,0);
  godzina();
  lcd.print(":");
  minuta();
  lcd.print(":");
  sekunda();
  delay(999);
  lcd.setCursor(0,0);
}

 

Podobne pytania

0 głosów
0 odpowiedzi 719 wizyt
pytanie zadane 18 maja 2022 w Mikrokontrolery przez lucasio99 Początkujący (320 p.)
0 głosów
1 odpowiedź 1,195 wizyt
pytanie zadane 8 kwietnia 2018 w Mikrokontrolery przez niezalogowany
0 głosów
1 odpowiedź 315 wizyt
pytanie zadane 20 grudnia 2019 w Mikrokontrolery przez Marak123 Stary wyjadacz (11,190 p.)

93,715 zapytań

142,629 odpowiedzi

323,260 komentarzy

63,257 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

Twierdza Linux. Bezpieczeństwo dla dociekliwych

Aby uzyskać rabat -10%, użyjcie kodu pasja-linux, wpisując go w specjalne pole w koszyku.

...