I am in need to synchronize two separate circuit boards working with Arduino nanoNano. The need is to activate one relay using Arduino-1 after 45 minminutes, and another relay using Arduino-2 after 45.36 seconds. The time starts from the point of reception of input from an external switch common to both.
withWith ref to This question, and from the advice of edgar-bonet I have used one temperature-compensated RTC for timing the Arduino sArduinos externally (DS3231). I am using itsit's 32kHz pin from the chip for counting the ticks and in the Arduino using the interrupt to count the ticks.
float _sec = 1;
float secTime = _sec * 1000UL; // second->millis
uint32_t coilTime = 5 * 1000UL; // second->millis
volatile uint32_t count = 0;
/*
* 32768 ticks in 1 sec
* 0.032768 tick in 1 us
*/
float _multiplier = 0.032768; // 32768 Hz
uint32_t _delay = secTime * _multiplier * 1000UL ;
volatile bool onFlag = false;
ISR(INT1_vect, ISR_NOBLOCK) {
count = count + 1UL;
if (_delay == count) {
PORTC |= (1 << 5);
onFlag = true;
EIMSK = 0;
}
}
ISR(INT0_vect) {
/* INTERRUPT re-initalization */
EIMSK = 0;
EICRA = B00001100; // INT0 - clear ; INT1 - set
EIFR = 0;
PORTB ^= (1 << 0) | (1 << 1); // setting Green off and red on
count = 0;
EIMSK = B00000010; // INT0 - inactive ; INT1 - active
}
void setup() {
Serial.begin(115200);
/* PIN initialization */
DDRB |= (1 << 0) | (1 << 1); // red led and green led
pinMode(3, INPUT_PULLUP); // timer unit
pinMode(2, INPUT); // interrupt unit
DDRC |= (1 << 3) | (1 << 4) | (1 << 5); // relay, OUT_1, OUT_2
/* LEDs state update */
PORTB &= ~(1 << 1); // red off.
PORTB |= (1 << 0); // green on.
PORTC &= ~(1 << 3); // A3-off
/* INTERRUPT initalization */
EICRA = B00000011; // INT0 - set ; INT1 - not set
EIMSK = B00000001; // INT0 - active ; INT1 - inactive
EIFR = 0;
}
void loop() {
if (onFlag) {
PORTB |= (1 << 0); // setting green on
delay(500);
PORTC ^= (1 << 5);
// PORTB ^= (1 << 0) | (1 << 1);
PORTB ^= (1 << 1); // setting red off
while (true) {
TCCR2B = 0;
count = 0;
PORTB ^= (1 << 0);
delay(500);
}
}
}
float _sec = 1;
float secTime = _sec * 1000UL; // second->millis
uint32_t coilTime = 5 * 1000UL; // second->millis
volatile uint32_t count = 0;
/*
* 32768 ticks in 1 sec
* 0.032768 tick in 1 us
*/
float _multiplier = 0.032768; // 32768 Hz
uint32_t _delay = secTime * _multiplier * 1000UL ;
volatile bool onFlag = false;
ISR(INT1_vect, ISR_NOBLOCK) {
count = count + 1UL;
if (_delay == count) {
PORTC |= (1 << 5);
onFlag = true;
EIMSK = 0;
}
}
ISR(INT0_vect) {
/* INTERRUPT re-initalization */
EIMSK = 0;
EICRA = B00001100; // INT0 - clear ; INT1 - set
EIFR = 0;
PORTB ^= (1 << 0) | (1 << 1); // setting Green off and red on
count = 0;
EIMSK = B00000010; // INT0 - inactive ; INT1 - active
}
void setup() {
Serial.begin(115200);
/* PIN initialization */
DDRB |= (1 << 0) | (1 << 1); // red led and green led
pinMode(3, INPUT_PULLUP); // timer unit
pinMode(2, INPUT); // interrupt unit
DDRC |= (1 << 3) | (1 << 4) | (1 << 5); // relay, OUT_1, OUT_2
/* LEDs state update */
PORTB &= ~(1 << 1); // red off.
PORTB |= (1 << 0); // green on.
PORTC &= ~(1 << 3); // A3-off
/* INTERRUPT initalization */
EICRA = B00000011; // INT0 - set ; INT1 - not set
EIMSK = B00000001; // INT0 - active ; INT1 - inactive
EIFR = 0;
}
void loop() {
if (onFlag) {
PORTB |= (1 << 0); // setting green on
delay(500);
PORTC ^= (1 << 5);
// PORTB ^= (1 << 0) | (1 << 1);
PORTB ^= (1 << 1); // setting red off
while (true) {
TCCR2B = 0;
count = 0;
PORTB ^= (1 << 0);
delay(500);
}
}
}
whenWhen running the program The, the result I am expecting is the accuracy of microseconds but
but for input -> Measured value 1 sec -> 1.000387792 2 sec -> 2.000784208 50 sec -> 50.020926833 250 sec -> 250.104557792:
input -> Measured value 1 sec -> 1.000387792 2 sec -> 2.000784208 50 sec -> 50.020926833 250 sec -> 250.104557792
How todo I make the clock more precise,? In the datasheetdata sheet it is said that the clock is accurate to +- 2ppm.