This tutorial follows on from an earlier one which created a simple pulse counter in an Arduino, using just software.
The page you are reading is complete in itself, but if you find the bits of this I am NOT discussing here interesting, visit the earlier tutorial.
(Much fancier "counter" solutions are possible, of course. I've written a guide to using the counter in the ATmega chip, too.)
At the end of the previous tutorial, we had a pulse counter hooked up to a pendulum that swings as long as the (other) Arduino giving it timely nudges continues to have power.
The program then counted the swings, keeping that count in ulPulsesFrmPendulum.
There is a webpage with a "user's guide" for the clock. Skimming that may help you get to grips with the code.
The loop() subroutine was quite concise. Careful examination will show you where things can be added, and where the basic "count the swings" mechanism must be left undisturbed.
In this tutorial, we are going to make more sophisticated use of the niceSparkfun serially driven 4 digit 7 segment LED display which previously merely displayed the count of pendulum swings.
And we are going to make that display do "triple duty". It will either display the number of swings, OR it will display an hh:mm time, or it will be part of changing the time shown on the clock.
(It wouldn't be hard, by the way, to revise the code to use a different output device, or to send the output to the Arduino IDE's serial monitor.)
In addition to what we were using before, we are going to make use of two more push buttons. (Momentary SPST switches) and a potentiometer.
The device will count swings continuously, even though what the count has reached will not always be on display. We'll "do everything else" in the moments when the pendulum is not passing the swing counting sensor.
The potentiometer will be used to select between...
1) Display the count of swings
2) Display the time, hh:mm
3) Display the current hour. When thus, the buttons will let you change it up or down, and...
4) Display the current minutes. As with "display hours", the buttons will allow changing the current minutes.
I added some global variables to the program:
byte bHrT=1; byte bHrU=8; byte bMiT=5; byte bMiU=9; byte bSeT=0; byte bSeU=0;
Declared like that, the clock starts running with the "hands" set to 18:59:00.... one minute (exactly) before 19:00, 7pm in the evening.
Note that two variables are used for the hour, two for the minute... and that two are available for the seconds past the minute.
(I don't think the variables for the seconds are used in the current code! Nor could they be added in full function, easily, to a clock with such a low frequency timebase. But ways could be found to add "seconds past the hour" in a crude way... restarting them at zero when the clock's "minute hand" is advanced, and regulating the advance of seconds differently from how the other "hands" are controlled. Do-able... if a little messy.)
The code to manage what is displayed, and to allow for changing the values in bHrT and BHrU/ bMiT and bMiU "clutters" the picture somewhat.
But, if you look, you will find...
if ((ulPulsesFrmPendulum % 30)==0){IncAMinute();};
... on the main tab for the app, in the loop() routine.
"IncAMinute()" is defined in the "Support_app_specific" tab. It merely changes the values in bHrT, bHrU, bMiT, and bMiU. And the rest of the code is written to always go there for anything to do with the time.
The effect of...
if ((ulPulsesFrmPendulum % 30)==0){IncAMinute();};
... is to add a minute to the current time once every 30 swings of the pendulum.
If the frequency of the pendulum were very nearly 30 swings per minute, then we'd be done!
However, the pendulum is just a tiny bit slower than that.
To fix the problem, something very like a leap year was added, next to the previous, similar line, to fine tune the clock:
if ((ulPulsesFrmPendulum % 2742)==0){IncAMinute();};
Why 2742? That, on top of the "do one every 30 swings too" was the number that "worked" for the particular pendulum I am using. Every 30 swings, the clock adds a minute to the displayed time. And for the next 2742 swings it falls slowly behind, until it is a full minute behind. At which time it adds TWO minutes after the next minute passes. One minute, as usual, because another 30 swings have happened. And an extra one, to bring the clock back to on time, because we are also at the "anniversary" of 2724 swings having happened.
The pendulum is about 6m long, and swings endlessly because of another Arduino, a magnet, an electromagnet, and two reed switches.
The full sourcecode is available for the pulse counter clock discussed above. It is simply implemented, using software alone, not the internal hardware counters inside the chip at the heart of the Arduino.
As already mentioned, there is also a "user's guide" for the clock. Skimming that may help you learn from the code.
(See my other tutorial, if you want to use the hardware counters.)
If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service.
Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.. Material on this page © TK Boyd 12/18
Click here to visit the homepage of my biggest site.
Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?FrmAht" I added to that link causes your browser problems. Please let me know, if so?
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
....... P a g e . . . E n d s .....