• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Synchronizowanie przekaźnika z wyświetlaczem LCD i modułem RTC
#1
Dzień dobry,

Niedawno zbudowałam swój pierwszy projekt z Arduino UNO, jakim jest zegar z intencjonalnym glitchem. Dołączyłam do niego przekaźnik w celu wydawania dźwięku klikania. Chciałabym go zsynchronizować z zegarem w taki sposób, żeby klikał razem z sekundami, poza na sekundy z końcówką 9, czyli np. gdy jest godzina 22:36:29. Wtedy miałby być cicho. 
Schemat projektu:
   

Oraz kod:

Kod:
#include "virtuabotixRTC.h"//Library used
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
virtuabotixRTC myRTC(2, 3, 4); //The order here is DAT,CLK,RST. You can change this depending on the wiring

char timeChar [8]; //number of digits for the format
uint8_t prevSeconds = 0; //

void setup()
{
 
  Serial.begin(9600);
 
  lcd.init();
  lcd.backlight();


 
  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year
  //myRTC.setDS1302Time(30, 21, 00, 5, 07, 01, 2021); //Here you write your actual time/date as shown above
  //but remember to "comment/remove" this function once you're done
  //The setup is done only one time and the module will continue counting it automatically
}
void loop()
{

 
  myRTC.updateTime();
 
  if(myRTC.seconds != prevSeconds)  // Only update display if seconds have changed
  {
    prevSeconds = myRTC.seconds;
   
    // Start printing elements as individuals
   
    if(myRTC.seconds %10)
    {
      sprintf(timeChar, "%02d:%02d:%02d",myRTC.hours, myRTC.minutes, myRTC.seconds);
      if(myRTC.seconds == 9 || myRTC.seconds == 19 || myRTC.seconds == 29 || myRTC.seconds == 39 || myRTC.seconds == 49 || myRTC.seconds == 59)
      {
        // timeChar[0] = 'Z';
        // timeChar[4] = 'X';
        timeChar[0] = random("32, 128");
        timeChar[4] = random("32, 128");
      }
    }
    else
    {
      sprintf(timeChar, "D0:0M:%02d", myRTC.seconds);
    }
   
    lcd.print(timeChar);
  }
}
Na próbę wpisałam niezależnie od sekund:
Kod:
#include "virtuabotixRTC.h"//Library used
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
virtuabotixRTC myRTC(2, 3, 4); //The order here is DAT,CLK,RST. You can change this depending on the wiring

char timeChar [8]; //number of digits for the format
uint8_t prevSeconds = 0; //

int Relaypin= 12;

void setup()
{
 
  Serial.begin(9600);
 
  lcd.init();
  lcd.backlight();


 
  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year
  //myRTC.setDS1302Time(30, 21, 00, 5, 07, 01, 2021); //Here you write your actual time/date as shown above
  //but remember to "comment/remove" this function once you're done
  //The setup is done only one time and the module will continue counting it automatically
  pinMode(Relaypin, OUTPUT); // Define the Relaypin as output pin

}
void loop()
{
digitalWrite(Relaypin, HIGH); // Sends high signal
delay(1000); // Waits for 1 second
digitalWrite(Relaypin, LOW); // Makes the signal low
delay(1000); // Waits for 1 second

 
  myRTC.updateTime();
 
  if(myRTC.seconds != prevSeconds)  // Only update display if seconds have changed
  {
    prevSeconds = myRTC.seconds;
   
    // Start printing elements as individuals
   
    if(myRTC.seconds %10)
    {
      sprintf(timeChar, "%02d:%02d:%02d",myRTC.hours, myRTC.minutes, myRTC.seconds);
      if(myRTC.seconds == 9 || myRTC.seconds == 19 || myRTC.seconds == 29 || myRTC.seconds == 39 || myRTC.seconds == 49 || myRTC.seconds == 59)
      {
        // timeChar[0] = 'Z';
        // timeChar[4] = 'X';
        timeChar[0] = random("32, 128");
        timeChar[4] = random("32, 128");
      }
    }
    else
    {
      sprintf(timeChar, "D0:0M:%02d", myRTC.seconds);
    }
   
    lcd.print(timeChar);
  }
}

Jednak to wchodzi w interakcję z pracą zegara, tworzy dziwaczny błąd. Zgaduję, że rozwiązanie byłoby z funkcją 
prevSeconds = myRTC.seconds
jako nowicjuszka niestety nie mam pojęcia jak ją zaadaptować do kodu. Byłabym niezmiernie wdzięczna za pomoc.
 
Odpowiedź
  


Wiadomości w tym wątku
Synchronizowanie przekaźnika z wyświetlaczem LCD i modułem RTC - przez Lepl39 - 03-02-2022, 23:41

Skocz do:


Przeglądający: 1 gości