Hi everyone,
I want to give an update about what I found out. The baudet clock still gives problems. And yes, it is correct that the simple checking for (pdt.Second() == 0) will cause a big problem leading to switching several times a second.
I also found out that the pulse time is not 2 seconds but 250 ms. I will give you here the updated code:
// Include necessary libraries
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(3, 4, 2); // DAT/IO, CLK/SCLK, RST/CE
RtcDS1302<ThreeWire> Rtc(myWire);
// Initialising global variables.
// Polarity switch clockline 1
int Switch_R1 = 11;
// Polarity switch clockline 2
int Switch_R2 = 10;
// Pulse switch clockline 2
int Switch_R3 = 9;
// Pulse switch clockline 1
int Switch_R4 = 8;
// Polarity tracking (1 = Negative / 2 = Positive)
int Polarity = 1;
void setup() {
// Initialise sketch
// Set ports to "output" mode
pinMode(Switch_R1, OUTPUT);
pinMode(Switch_R2, OUTPUT);
pinMode(Switch_R3, OUTPUT);
pinMode(Switch_R4, OUTPUT);
// Set relays 1 to 4 to low
digitalWrite(Switch_R1, HIGH);
digitalWrite(Switch_R2, HIGH);
digitalWrite(Switch_R3, HIGH);
digitalWrite(Switch_R4, HIGH);
// Initialise RTC and communication
Rtc.Begin();
Serial.begin(9600); // Baudrate 9600
// Set and write the manual <date> and <time> to the RTC
RtcDateTime cdt = RtcDateTime("May 14 2021", "12:23:00");
Rtc.SetDateTime(cdt);
}
void loop() {
// Process loop
// Get the time from the RTC
RtcDateTime pdt = Rtc.GetDateTime();
// When seconds = 1 sent the pulse
if (pdt.Second() == 1)
{
// Sending the pulse to the clock
Sent_Pulse();
}
}
void Sent_Pulse() {
/*
* This function send the pulse to the clock.
*/
// Now, lets switch the polarity
if (Polarity == 1)
{
Polarity = 2;
// Clockline 1 is set to positive
digitalWrite(Switch_R1, HIGH);
// Clockline 2 is set to negative
digitalWrite(Switch_R2, HIGH);
}
else
{
Polarity = 1;
// Clockline 1 is set to negative
digitalWrite(Switch_R1, LOW);
// Clockline 2 is set to positive
digitalWrite(Switch_R2, LOW);
}
// Now the polarity is set, give the 250 ms pulse
digitalWrite(Switch_R3, LOW);
digitalWrite(Switch_R4, LOW);
delay(250);
// Stop the pulse
digitalWrite(Switch_R3, HIGH);
digitalWrite(Switch_R4, HIGH);
// De-activate relays 1 and 2 to save energy
digitalWrite(Switch_R1, HIGH);
digitalWrite(Switch_R2, HIGH);
// Delay for another 750 seconds,
// to prevent Arduino from giving
// several pulses in a second.
delay(750);
}
For some reason "(pdt.Second() == 0)" gave problems and I changed it to "(pdt.Second() == 1)". It doesn't matter if the change takes place at 0 or 1 second as long. To solve the multi switching problem, after ending the pulse there is a "Delay(750)" instruction. This prevents the Arduino from switching more than once in a second.
When the clock is not connected, the Arduino behaves as expected. From the moment the clock is connected, it behaves erratically. I changed the Baudet clock with a 24V/25W lamp and the problems are gone. So we can conclude that the Baudet clock is the cause of the troubles.
I also inspect the wiring and they are all the same length. Also, those wires are located around the Arduino and the relay board. I have no idea why the Baudet clock influences the Arduino because everything is isolated from each other by opto-electronics.
However, I did the following test:
I switched on the Arduino without the lamp or clock connected. So it changed every 60 seconds without problems.
Then I manually switched the Baudet clock by changing the terminals myself every 10 seconds and kept repeating this.
After approximately 20 to 30 seconds, the switching of the Arduino became erratically again. Remember, there is no physical connection between Arduino and the clock.
So I assume that we can conclude that it is noise coming from the clock which disturbs the Arduino.
Therefore, I think that in this case, with this clock, using an Arduino is no option. Even when the distance between Arduino and the clock is more than 2 meters, it still disturbs.
Any ideas if this noise problem can be solved?
Thank you so much for your replies, which are appreciated.
Chris