Hello. Interesting that there isn't an introduction forum on here..
Anyway, for a class project, I am attempting to make a simple VFD or seven segment display clock. I intend on having two buttons to control set the correct time. I'm trying to go about this using as much of my own code as possible, with only a few outside sources here or there. Mind you, I'm still pretty new to programming in general, having only taken a basic course in the past and worked through several tutorials throughout the web.
So before I even get to the challenge of putting it into the displays, I decided it was best to try to make the internals of the clock itself work.
Here's some code for the clock itself.
{
for(byte s = 0; s < 60; s++) //second counter
{
sec = s;
Serial.print(h); [
Serial.print(":");
Serial.print(m);
Serial.print(":");
Serial.println(sec);
delay(1000);
}if(sec == 59) //when 59 seconds pass
{
if(m < 59) //and it hasnt been 59 minutes
{
m++; // add one to the minutes
}
else //once it rolls over to 59, set minutes back to 0
{
m = 0;
}
}if(m == 0) //once 60 minutes pass
{
if(h > 0) //cant have 0 hours. unless using 24 hour time
{
if(h < 12) //check to make sure it hasnt been 12 hours
{
h++; //add to hours
}
else
{
h = 1; //once its been 12 hours, go back to 1:00
}
}
}
}
Now, I'm aware that it is probably not the best or most accurate way to go, but it works for my purposes so far. It works ok, at least for relatively keeping time. Might have to be changed around for the displays, when I get to that point.
So the issue I am having is a lack of ability to make the two buttons change the minutes and hours. So far, I have tried using if statements with small delays to increase the values by one, but this resulted in values only changing once an entire minute had passed.
Next, I tried while loops
{
while(digitalRead(hourButton) == LOW)
{
if(h < 12)
{
h++;
}
else
{
h = 1;
}
delay(100);
}while(digitalRead(minuteButton) == LOW)
{
if(m < 59)
{
m++;
}
else
{
m = 0;
}
delay(100);
}
And these did sort of work when put into the for loop that was counting the seconds, but would halt the seconds. On top of that, they wouldn't reliably change the time. They cause the annoyance of having to hold the button for a second before it even considers changing values, from the delay(1000) I presume.
So at this point, I feel that I have hit the most that I know. I tried googling some things, but didn't seem to come up with much. Only thing I can think of is the delay()'s causing some problems and the fact that my clock algorithm is a little inefficient. Is there a better way to go about making these buttons work?The while loops give some functionality, but not as much as I would like. They make it feel clunky and cumbersome and somewhat hard to set the time. Thanks ahead of time.