[Advice needed] Building a small BCD clock.

Best way to explain is the code :slight_smile:

The setuploop is called in the loop() code, when we are setting the clock (initiated by a long press of the button, as a short press was used to do something else), ie. it is called repeatedy and often (until the SetClock is false)

Instead of LEDs I have a meter, you just do some visual feedback on your LEDs instead at the SetNeedle()

void setuploop() {
/* This is a small state machine, ensuring we can enter current HH:MI with one button.
  The needle is waved a bit, then wait for number button pushes that sets a single digit. */

  static byte Entry = 0 ; // State machine
  static byte N ; static byte H = 0 ; static byte T = 0 ; static byte M = 0 ;

  switch (Entry) {
    case 0: case 2: case 4: case 6: wiggle() ; Entry++ ; break ;
    case 1: if (ButtonUpd(&N)) Entry++ ; if (N>2) N=0 ; break ;
    case 3: if (ButtonUpd(&H)) Entry++ ; if (H>9 || (N==2&&H>3)) H=0 ; break ;
    case 5: if (ButtonUpd(&T)) Entry++ ; if (T>5) T=0 ; break ;
    case 7: if (ButtonUpd(&M)) Entry++ ; if (M>9) M=0 ; break ;
    case 8: SetClock = false ;
            //Store a new value in the RTC chip. Date is ignored (random)
            RTC.stopClock();
            RTC.fillByHMS(N*10+H,T*10+M,0);
            RTC.setTime();
            RTC.startClock();
            break ;
  }
}

boolean ButtonUpd(byte *Pdig) {
/* return true when no button change for several seconds. Increment argument for every button push
  put needle at value for feedback. (Wrap is handled by calling function) */

  static byte PrvBtn = HIGH ;
  byte Button ;

  SetNeedle( *Pdig, 10 ) ;
  if ( millis() - Timer > 5 && (Button=digitalRead(BUTN1)) != PrvBtn) {
    // button change, increment digit if push
    if (Button==LOW) (*Pdig)++ ;
    Timer = millis() ; PrvBtn = Button ;
  }
  return millis() - Timer >5000L ;
}