RTC polarity reverse every minute on the minute - Master Clock > Slave Clock

Hello

I am making a master clock that drives a slave clock by sending a 500ms pulse with alternating polarity each minute.

I wrote a code to test my L298N H-Bridge and it is as follows

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
void setup()
{

  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}
void demoOne()
{
   digitalWrite(enA, HIGH);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(500);
   digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);

   delay(60000);
  // now switch polarity
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);  
 
  delay(500);
  // now turn off pulse
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW); 
   delay(60000); 
}

void loop()
{
  demoOne();
 
}

okay!

so now i have a Grove RTC (DS1307?)
and I would like to sync this code up with the RTC. Here is where I dont know enough about the arduino or the RTC.

EDIT: Using this code, now syncs with RTC, still trying to get action to happen at each 00 second (like a real clock) and best way to do a polarity reverse loop (each minute switch polarity) like in the above code

#include <DS1307RTC.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h> 

int enA = 10;
int in1 = 9;
int in2 = 8;  



void setup()
{
  Serial.begin(9600);
  Serial.println("In setup....");
  /*
  using rtc, sync system time to rtc
   setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011 //old way *****************
   */

  // following lines added to set time from rtc, took from timeRtcSet example, added jim *****************
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet) 
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");
  // end of setting the time ******************************




  Alarm.timerRepeat(60, Repeats);            // timer for every 15 seconds    
  //Alarm.timerOnce(10, OnceOnly);             // called once after 10 seconds 


  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  digitalClockDisplay();
  Serial.println("Ending setup....");
}

void  loop(){  
  digitalClockDisplay();
  Alarm.delay(1000);
}

void Repeats(){
  Serial.println("PULSE");

  digitalWrite(enA, HIGH);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(500);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  
}


void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

i am interested in finding the most ELEGANT solution

:art:

Well, a phase switch seems unnecessarily complicated. Why do you need it? Necessity is an important component of elegance. :slight_smile:

Hey! Thanks for your answer, its just that i need it to switch the two pins (in1, and in2 in my code) every minute. and i am trying to figure out the best way of switching the polarity every minute.

it seemed to me that and if else for those to pins (if high switch to low, if low switch to high) would be ideal? but not quite sure, just wanted some thoughts.

variable old_minute

if minute not equal to old_minute
{
old_minute = minute
do your once a minute code
}

thanks aargh

i get the idea but dont understand precisely the code

i realize i ended up pasting together your blink led code to do my last project! thats how i knew about the state change pins

Silicon_Salut:
i get the idea but dont understand precisely the code

Do you mean that you don't know how to translate the pseudocode into C/C++? Or you don't understand how it works?

i believe i understand the pseudo code

get the last minute

if the current minute isn't the last minute run the code

but i dont understand how to translate the pseudo code into C++ and thus im not sure if that will solve the problem of running code A at say minute 0 code B at minute 1 code A at minute 2. etc etc.

i was thinking some sort of if then for the pins depending on previous minute would work.

pulsing two pins one high and one low

waiting a minute

pulsing the same two pins one low and one high creating reverse polarity

Silicon_Salut:
im not sure if that will solve the problem of running code A at say minute 0 code B at minute 1 code A at minute 2. etc etc.

It most definitely will. I have several such functions going on in my clock code. It's simple and foolproof. There is no "then" in C. It's implied in the statement block that follows the "if".

Basing it on the previous state of the clock control pins makes no sense at all. I recommend first toggling one of them, and then inverting it to produce the other one (it looks to me like you want them to be complimentary, or opposite).

aarg

thanks so much

this is the code that i have

void Repeats(){
 Serial.println("PULSE");

 digitalWrite(enA, HIGH);
 digitalWrite(in1, HIGH);
 digitalWrite(in2, LOW);
 delay(500);
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);  
 
}

which sends a pulse of half a second then shuts off.

if this runs, the next minutes code would need to be this

void Repeats(){
 Serial.println("PULSE");

 digitalWrite(enA, HIGH);
 digitalWrite(in1, LOW);
 digitalWrite(in2, HIGH);
 delay(500);
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);  
 
}

again, conceptually i understand the if statement (then or no then) but still dont understand the pseudo code > C++ switch. thanks so much

I don't know what to say. I can think of nothing simpler.

Look at the current minute.
If it is different than the one you have stored perform the following:
-store the current minute to look at the next time
-do your once a minute stuff

(this is already in the loop so it repeats)

I'm tempted to be mean! :slight_smile: Please don't say you can't understand this time.

One thing. You should get away from the hard coded values that you are sending and use variables for HIGH and LOW. I think it is trapping you in a procedural mindset.

Your pulsing is extremely redundant. First of all, enA always remains HIGH. Is that a mistake?

Thus, the first minute becomes:

+++++++++++++++++++void Repeats(){
 Serial.println("PULSE");
 digitalWrite(in1, HIGH);
 delay(500);
 digitalWrite(in1, LOW);
}

and the second is:

+++++++++++++++++++void Repeats(){
 Serial.println("PULSE");
 digitalWrite(in2, HIGH);
 delay(500);
 digitalWrite(in2, LOW);
}

... and you can't have two functions with the same name.

What you really should have is a function like:

void Repeats(int pinNumber){
 Serial.println("PULSE");
 digitalWrite(int pinNumber, HIGH);
 delay(500);
 digitalWrite(int pinNumber, LOW);
}

and then call it like:

Repeats(in1);
Repeats(in2);

hey aarg,

thanks for the reply. i dont understand that much C++ thats why im on this board. i realize its frustrating to work with total noobs, and that its hard to teach people not knowing exactly what they are doing.

Your pulsing is extremely redundant.

i know it is, and if you look at my first opening post, i was looking for a way to toggle between the two states of the pin, every minute on the minute.

your code

void Repeats(int pinNumber){
 Serial.println("PULSE");
 digitalWrite(int pinNumber, HIGH);
 delay(500);
 digitalWrite(int pinNumber, LOW);
}

is exactly what i was looking for. however, i dont know how to make it run every minute on the minute. nor exactly how to declare the pin numbers. but i will try.

EDIT: also i am not sure how to link up int pin number to my actual pins.. is it in an array?

thanks for your patience and if anybody else has any advice (or any patience) im working on this at the moment and hope to finish it today.

:blush:

got it working like this

#include <DS1307RTC.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h> 



int enA = 10;
int in1 = 9; 
int in2 = 8; 


void setup()
{
    
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    
  Serial.begin(9600);
  Serial.println("In setup....");
  /*
  using rtc, sync system time to rtc
   setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011 //old way *****************
   */

  // following lines added to set time from rtc, took from timeRtcSet example, added jim *****************
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet) 
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");
  // end of setting the time ******************************


Alarm.timerRepeat(60, Repeat);            // pulse every 60   seconds



  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  digitalClockDisplay();
  Serial.println("Ending setup....");
}


void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void Repeat() {
  digitalWrite(enA, HIGH);
  Serial.println("PULSE");
  digitalWrite(9, !digitalRead(9));  // toggle state
  digitalWrite(8, !digitalRead(8));
  delay(500);
  digitalWrite(enA, LOW);
  
}


void  loop(){  
  digitalClockDisplay();
  Alarm.delay(1000);

}

thanks aarg for making me do the work