Jump to content

SkySurveyBanner.jpg.21855908fce40597655603b6c9af720d.jpg

DIY Moon Phase Dial


Gina

Recommended Posts

While I sort that out I'll reset the working sketch to a million microseconds and see if the timekeeping is better now I've cleared the binding of the gears.

Link to comment
Share on other sites

Looking at that more recent RTC library, it seems that setting up the square wave for a one second interrupt is already provided in the library avoiding the need to get into the nitty-gritty of setting registers directly :)

56b62c1c206a7_RTCSquareWave01.thumb.JPG.

Link to comment
Share on other sites

It's now about 2 hours since I set the clock to the correct time and it's still showing the right time within 5s so I reckon the previous error in timekeeping was friction between the gears overloading the stepper motor.  Since I wont be using the current timing source of the internal crystal oscillator, I think any further testing would be a waste of time.  I think I'm about ready to change over to using the RTC so I'm going to unplug the Nano from the current control unit and put it in the new board.

Link to comment
Share on other sites

Tried the example sketch from the RTC library and it wouldn't compile.  Tried a few things then checked the Arduino IDE version and it was a bit old so I downloaded and installed the latest version.  First try failed so I closed everything, rebooted the heap and tried again, this time successfuly :)  The example sketch now compiles, uploads and runs :)  The RTC date and time is wrong so I'll need to run the setting up code - no problem.  Won't need that until I do the calendar or use the date/time to detect time to "change the clocks".  I've done it before for my weather station data logger which also uses an RTC.

Edited by Gina
Link to comment
Share on other sites

Added

RTC.squareWave(SQWAVE_1_HZ);    //1 Hz square wave

Plus INPUT_PULLUP to D2 and digitalRead > digtalWrite to pin D13 the internal LED in the loop and the LED is flashing once a second as desired :)

This is the full test sketch.

/*
 * Filename MoonClock_RTC_01.ino  2016-02-06 2020
 * Testing the 1Hz square wave output from the RTC
 */

#include <DS3232RTC.h>    //http://github.com/JChristensen/DS3232RTC
#include <Time.h>         //http://www.arduino.cc/playground/Code/Time  
#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)

int timingPin = 2;       // RTC square wave fed to pin 2
int ledPin = 13;          // LED connected to digital pin 13


void setup(void)
{
    pinMode(ledPin, OUTPUT);      // sets the digital pin as output
    pinMode(timingPin,INPUT_PULLUP);     
    Serial.begin(9600);
    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");
     
    RTC.squareWave(SQWAVE_1_HZ);    //1 Hz square wave            
}

void loop(void)
{
    digitalWrite(ledPin, digitalRead(timingPin));
    delay(100);
}

Now to combine the RTC code with the clock code to produce the combined sketch.

Edited by Gina
Link to comment
Share on other sites

I have now combined the two sketches and arranged the built in LED to change state for each interrupt.  So now instead of flashing with a 1Hz rate the rate is now 0.5Hz - 1s ON and 1s OFF.  This is working correctly and shows the RTC is producing an interrupt once every second Q.E.D. :)

Next job will be to connect the circuit to the +12v PSU and the stepper motors on the clock.  That's a job for tomorrow I think :)  I'm quitting now while I'm ahead :D

Here's the test sketch.

// Filename :- Moon_Clock_with_RTC_02 - 2016-02-07 0920
// Arduino test sketch for moon clock with RTC v02
// Hardware interrupt from RTC on pin 2
// LED state changes on each interrupt so should be 1s on and 1s off
// Code included to turn stepper off between ticks
// Timing uses RTC interrupt
// Second stepper motor drive added to drive seconds hand. 
// Minutes driven every 5 interrupts (5s) on first stepper motor.
//
#include <DS3232RTC.h>    //http://github.com/JChristensen/DS3232RTC
#include <Time.h>         //http://www.arduino.cc/playground/Code/Time  
#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
//
int ledPin = 13;          // LED connected to digital pin 13
bool ledON = false;       // LED off iitially
int motorPins[] = {3, 4, 5, 6};  // motor pins for the minutes stepper
int motorPins2[] = {A0, A1, A2, A3};  // motor pins for the seconds stepper
int stepSize = 32;  // motor steps for minutes
int stepSize2 = 64;  // motor steps for seconds
int count = 0;
int mask = 0; //  binary mask for motor pins to select phase
int mask2 = 0; //  binary mask for motor pins to select phase
int intCount = 0; // Count interrupts for minutes stepper
//
void setup() {
  for (count = 0; count < 4; count++) {
    pinMode(motorPins[count], OUTPUT);  // set the motor pins for output
    pinMode(motorPins2[count], OUTPUT);  // set the motor pins for output
  }
    pinMode(2,INPUT_PULLUP);     
    Serial.begin(9600);
    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");
     
    RTC.squareWave(SQWAVE_1_HZ);    //1 Hz square wave            
  
    attachInterrupt(digitalPinToInterrupt(2), timerIsr, FALLING);
    pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}
//
void moveForward() {  //  move one stepper motor phase forwards
  if ((mask == 0) || (mask == 1)) {
    mask = 16;  // set/reset phase mask  10000
  }
  mask>>=1;  // binary shift phase mask one position right  1000, 0100, 0010, 0001                                      
  for (count = 3; count >= 0; count--) {
    digitalWrite(motorPins[3 - count], mask>>count&0x01);  // 
  }
  delay(150);
}
//
void moveForwardSecs() {  //  move one stepper motor phase forwards
  if ((mask2 == 0) || (mask2 == 1)) {
    mask2 = 16;  // set/reset phase mask  10000
  }
  mask2>>=1;  // binary shift phase mask one position right  1000, 0100, 0010, 0001                                      
  for (count = 3; count >= 0; count--) {
    digitalWrite(motorPins2[3 - count], mask2>>count&0x01);  // 
  }
  delay(150);
}
//
void moveBackward() {  //  move one stepper motor phase backwards - for use later for time correction
  if ((mask == 0) || (mask == 1)) {
    mask = 16;
  }
  mask>>=1;
  for (count = 3; count >= 0; count--) {
    digitalWrite(motorPins[count], mask>>count&0x01);
  }
}
//
void timerIsr() {  //  Ths was previously the loop - now called by the interrupt
  ledON = !ledON;  // toggle LED ON/OFF
  digitalWrite(ledPin, ledON);
  int c = 0;
  for (c = 0; c < stepSize2; c++) {
    moveForwardSecs();
  }
  intCount ++; // Increment interrupt count
  if (intCount > 4) 
  {
    intCount = 0; 
    for (c = 0; c < stepSize; c++) 
    { moveForward(); }
  } // move minuter forward
//  Disable all motor pins to turn current off 
//  mask is not touched and phase shift continues where it left off on next tick
  for (count = 3; count >= 0; count--) {
    digitalWrite(motorPins[count], 0);  
    digitalWrite(motorPins2[count], 0);  
  } 
}
//
void loop()
{
  // Main code loop
  // TODO: Put regular (non-ISR) logic here
}
// End

 

Edited by Gina
Posted wrong sketch
  • Like 1
Link to comment
Share on other sites

Mostly connected up now and seconds stepper is working fine but the minutes stepper is not.  Checked output from board with DMM and it seems alright but the stepper motor is just twitching :(  This is a puzzle!!!  Rechecked the sketch - looks fine, hasn't changed anyway except the pin numbers have moved up one from the previous board and the outputs are showing pulses every five seconds as they should.

Link to comment
Share on other sites

I have some Hall effect devices on order - should come tomorrow or Tuesday, then I can attach two to the clock and connect to the A6 and A7 inputs.  Once that's done I should be able to add code to the sketch to automatically set the clock to the right time.

The RTC will be set to the compile/upload computer system time by this code :-

RTC.set(now());

Then some "run once" code will advance the clock until it's set to 12 o'clock by stopping when the hour and minute hands are both on the 12.  Next the RTC is read and the time change calculated followed by advancing or retarding the time the appropriate amount.  During this time the interrupts will not control the clock but will be counted so that the time taken to update the clock can be taken into account.  Coding this lot is going to be fun :D

Link to comment
Share on other sites

I have had the clock running now for over two hours and all seems well.  It's keeping time.  I set the minute hand right and go by that - the hour hand is not right and won't be until I get the automatic time setting code done and working and that needs the Hall effect devices.  I could advance or retard the clock manually but it's not worth the bother :D

The initial setting up will go in "void loop()" with the interrupt disconnected from the stepper motor drive (though I guess I could leave the seconds running).  A boolean flag will tell the interrupt service routine (ISR) that the clock is being set up then once the time has been set, the flag will tell the ISR to drive the stepper motors.  For both homing to 12 o'clock and then setting the time the full speed routines will be used so that the setting up time is reduced to the minimum.

Link to comment
Share on other sites

Just a recap on a few calculations regarding the rate at which the clock can be set up...  The "fast mode" for the minute stepper motor can change the time displayed by an hour in 70s.  The worst case scenario would be that the hands would need to turn just under 12 hours for the clock to reach the preset time (I said 12 o'clock above but it might be 3 o'clock depending on where I can put the hour sensor).  This would take 12 x 70s = 840s = 14m.  The time to then set the clock to the right time could take up to 7m (for plus or minus 6 hours).  The correction needed to allow for this time could be calculated from these figures and applied.  I think this may be accurate enough and is probably easier than counting seconds and applying a second correction, but it's still quite complicated.

Link to comment
Share on other sites

Well done Gina looks like you are just about there now.  Here's a thought,  I can manually move the hands on my radio controlled clock (the funny German time keeping Lidl one)  to set the correct time.  In fact that's how I fool it to keep UK time.  I take it that there must be some sort of rotary encoder built into its control circuit so that it has a reference to where the hands are, or would it not be as complex  as that? 

Now that you are just about there would you do any of this differently  - would it have been possible to say use an off-the-shelf clock and just hack it to give the extra functionality that you wanted.  I know that this would not have been as much fun but I'd be interested to hear your thoughts.

 

Jim

Link to comment
Share on other sites

Thank you Jim :)  I'm sure they must have some way to detect where the hands are but I haven't had one apart to find out (yet).

I did consider modifying a radio controlled clock but wasn't sure if it would have enough power to drive the moon phase mechanism.  And also, yes, I wanted to build as much as possible myself.  I have already changed the design a few times, so I guess you could say I've already done it differently :D

And I still have a complete grandfather clock on the books but shelved for the time being. :D

Link to comment
Share on other sites

I'll have to contact the supplier but meantime I've found some Hall effect sensors and will be attaching two of these these to the clock and wiring them up - maybe tomorrow.

Link to comment
Share on other sites

I sense your frustration Gina, it's horrible when you expect something and it doesn't arrive.  Have we really got so use to one day deliveries (the Amazon effect).  Can you imagine what it was like waiting sometimes a month for things to arrive - madness.  I must admit I am terrible at waiting once I've ordered something - when do I want it - why yesterday of course:hmh:

 

Jim 

  • Like 1
Link to comment
Share on other sites

Thank you Jim :)

I've now attached the two Hall sensors to the dial - minute on the 12 o'clock wide mark on the dial and the hour on the 3 o'clock position right next to one of the mounting pillars.  Once I'm sure the glue has set I'll attach wires.

Edited by Gina
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.