Increment of hours on alarm clock not working more than once

Hi all,

I am writing a code for a project using somewhat of an alarm clock. The user should be able to increase the number of hours every time the “up” button is clicked. What is happening is that it is only increasing one time and then nothing more.

What I thought would happen is that " v = v+1" would increase the value by one every time, and that each time this occurs I could assign the value of v to f using " f = v" and then I could print “f”.

(I am using multiple variables because I wish to do this for more than one time in the day)

I would appreciate if someone could tell me why the loop isn’t working properly. Thank you!

uint8_t buttons = lcd.readButtons();

    if (accept == 0){
      v = a;
    }
    if (accept == 1){
      v = c;
    }

    if (accept == 0){
      w = b;
    }
    if (accept == 1){
      w = d;
    }
    
    if (buttons) {
    lcd.clear();
    lcd.setCursor(0,0);
    
    if (buttons & BUTTON_UP) {
      lcd.print("Morning Time Set");
      lcd.setCursor(0,1);
      if(f < 10) printzero();    
      lcd.print(f);
      lcd.print(":");
      lcd.print(g);
      
        if (buttons & BUTTON_UP){
          if (accept = 0){
          v = v + 1;
          f = v;
          delay(500);
           if(f > 23) f-=24;                 // adjust for over 23 hour
            else if(f < 0) f+=24;            // or under 0 hours
  
        }
        else if (accept = 1){
          v = v + 1;
          g = v;
        }
        }
        else if (accept = 1){

How come, on this line, you switched from == to =?

that is a typo! thank you.

uint8_t buttons = lcd.readButtons();

    if (accept == 0){
      v = a;
    }
    if (accept == 1){
      v = c;
    }

    if (accept == 0){
      w = b;
    }
    if (accept == 1){
      w = d;
    }
    
    if (buttons) {
    lcd.clear();
    lcd.setCursor(0,0);
    
    if (buttons & BUTTON_UP) {
      lcd.print("Morning Time Set");
      lcd.setCursor(0,1);
      if(f < 10) printzero();    
      lcd.print(f);
      lcd.print(":");
      lcd.print(g);
      
        if (buttons & BUTTON_UP){
          if (accept == 0){
          v = v + 1;
          f = v;
          delay(500);
           if(f > 23) f-=24;                 // adjust for over 23 hour
            else if(f < 0) f+=24;            // or under 0 hours
  
        }
        else if (accept == 1){
          v = v + 1;
          g = v;
        }
        }

I unfortunately still run into the same problem here.

    if (buttons & BUTTON_UP) {
      lcd.print("Morning Time Set");
      lcd.setCursor(0,1);
      if(f < 10) printzero();   
      lcd.print(f);
      lcd.print(":");
      lcd.print(g);
     
        if (buttons & BUTTON_UP){

Why is this test done twice?

I think it would be worth the effort to put every { on a line by itself. I think it would be worth the effort to use Tools + Auto Format to fix the indenting. I think it would be worth the effort to use names that are longer than one character. I think it would be worth the effort to post ALL of your code. I think it would be worth the effort to add more Serial.print() statements, to find out what is really happening.

PaulS:
I think it would be worth the effort to put every { on a line by itself.

I know this is extremely common practice for some people, but I personally don't do this. I write my code the way the OP did, at least when it comes to opening {}. Why do you say it would be worth the effort to put them on lines by themselves? This does not affect the way his code runs or its ability to compile. I'm just curious the purpose other than following common practice. I have seen a few people write code the way I do. Just seems to be personal preference unless there is a reason other than preference to put them on solo lines.

Just seems to be personal preference unless there is a reason other than preference to put them on solo lines.

I can see a block of code better when the curly braces line up:

   if(someCondition)
   {
      // Do some stuff
   }

While it is trivial to see the block when the statement is this small, it is not trivial when the block is 50 lines long and contains nested if statements.

Quite often, when I've recommended that people do this, they have come back and said "Hey, I see what my problem was. I thought xxx was inside the statement, when it wasn't.". So, it is sometimes worthwhile making braces line up (always for me).