Hello,
Recently, I've built my first project which includes Arduino UNO, RTC1302 and LCD display, it works as a sort of clock with glitch. After advice on project that I got here, I've added relay module just for clicking/ticking purpose, so it would be on for one second and off for the other. I also would like it to be silent at 9 second (eg. :09, :29, :59 etc.). As a newbie, I have some hard time with coding.
Here is the sketch of my project:
And here is my code:
#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);
}
}
Firstly, I tried to run the relay independently from the clock with this code:
#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);
}
}
But it turned out to be a failure, as it interrupts the work of clock and LCD - it was running for some seconds and others not, and the relay didn't make the click (I've checked the relay on a separate basic code for relay and it worked properly). Would be really grateful for help or advice.
Best regards