time setting controls for a simple clock

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.

Advice as follows
Please post all of your code so that what you are doing can be seen in context of variable declarations etc

Use code tags when posting code, highlight then the # button, rather than posting as a quote which can mangle the code display.

Look at the BlinkWithoutDelay example in the IDE to see how to avoid using delay() which will improve the response to button presses.

Read up on debouncing buttons in software to avoid spurious multiple inputs. Again there is an example in the IDE.

Use a resistor, either external or internal, to hold the button pins at a fixed voltage when not pressed. This avoids them picking up spurious inputs. You may already be doing this via INPUT_PULLUP in the pinMode() commands but you have not posted all of your code.

Thanks for the pointers, I'll keep that in mind for next time.

Here's the code.

byte sec = 0;   //timing variables
byte m = 0;
byte h = 12;

const byte hourButton = 8; //time set buttons
const byte minuteButton = 9;
void setup()
{
  pinMode(hourButton, INPUT_PULLUP);
  pinMode(minuteButton, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop()
{
  clock(); 
}
void buttons()
{
  while(digitalRead(hourButton) == LOW)
  {
    if(h < 12)
    {
      h++;
    }
    else
    {
      h = 1;
    }
    delay(100);
    Serial.println(h);
  }
  
  while(digitalRead(minuteButton) == LOW)
  {
    if(m < 59)
    {
      m++;
    }
    else
    {
      m = 0;
    }
    delay(100);
    Serial.println(m);
  }
  
}

void clock()
{
  for(byte s = 0; s < 60; s++)  //second counter
  {
    buttons();
    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
      }
    }
  }
}

I'll have to look over the blink without delay again. I had looked at it before which made me think that the delays were causing the issues. The buttons on their own don't seem to be too bouncy, but just in case, I'll throw in some debouncing. At this point, I am fairly certain that it is likely the delays and I'll look into working this without using them.