What is this flashing clock? Well, it's just an arduino uno, with three LED's, one each for the seconds, minutes and hours. They don't show the time, they just flash as each period they're counting passes.
So, we're going to count the seconds and flash just the second LED. Then each 60th flash, we reset the seconds counter, increment the minute counter and have the minute LED flash. Ditto for the hour counter each 60th minute.
I've got the LED's VCC legs connected to digital pins 8, 9 and 10, with the other connected to the three grounds. When I come to make this an actual board I can have a single ground pin being used up, rather then using all three on the board.
An important part of the code to focus on is how to turn on each light in turn...
If you want to turn on the second, the minute and the hour LED all at the same time, you could have some horrid "if" statement controlling them, but much more clean looking is this:
// Turn lights on
switch(CycleCount)
{
case 2: digitalWrite(HourPin, HIGH);
case 1: digitalWrite(MinutePin, HIGH);
case 0: digitalWrite(SecondPin, HIGH);
break;
}
So, the count is reset to zero each cycle it is incremented if we add to the minutes, and incremented if we add to the hours.
The switch statement then drops through, notice that there are no "breaks" on the 2 and 1 (hours & minutes) cases. So, if we have incremented the hour, we have a count of two and so we set the HourPin High, drop through to the MinutePin High and finally the Second Pin High before we break.
Likewise, if we only set the minutes this cycle, then the count will be one, so the switch statement will drop down to the Minute Pin high, skipping the Hour pin totally.
The effect of this is to seemingly instantly turn on all the LED's each cycle as they are needed together, there is no humanly noticeable staggering of the lights coming on, as one might get with an if statement, or once might get if one were to set the LED as we increment the values. Doing Lights on when we increment actually clearly shows the LED's going on and off in a staggered pattern.
Our task after turning all of them on is to just wait a short time (100 milliseconds) and then turn them all off with another switch statement. And with them all off again simply wait for the remainder of the second (i.e. 900 ms).
Keep this in mind as you read the following code:
// The pins
int SecondPin = 8;
int MinutePin = 9;
int HourPin = 10;
// The counts
int CycleCount = 0;
int SecondCount = 0;
int MinuteCount = 0;
int HourCount = 0;
void setup()
{
// Setup the pins
pinMode (SecondPin, OUTPUT);
pinMode (MinutePin, OUTPUT);
pinMode (HourPin, OUTPUT);
}
void loop()
{
// Increment the counts
CycleCount = 0;
SecondCount = SecondCount + 1;
if ( SecondCount > 59 )
{
SecondCount = 0;
++MinuteCount;
++CycleCount;
if ( MinuteCount > 59 )
{
++CycleCount;
MinuteCount = 0;
++HourCount;
if ( HourCount > 23 )
{
HourCount = 0;
}
}
}
// Turn lights on
switch(CycleCount)
{
case 2:
digitalWrite(HourPin, HIGH);
case 1:
digitalWrite(MinutePin, HIGH);
case 0:
digitalWrite(SecondPin, HIGH);
break;
}
// Light are on for 1/10 of a second
delay(100);
// Tuen lights off
switch(CycleCount)
{
case 2:
digitalWrite(HourPin, LOW);
case 1:
digitalWrite(MinutePin, LOW);
case 0:
digitalWrite(SecondPin, LOW);
break;
}
// Lights all off for 9/10 of a second
delay (900);
}
If you found anything of use here, please return again, and don't forget to check out my other YouTube videos.
No comments:
Post a Comment