How do you pause a bunch of 'for loops' with a button?

hello i am trying to make a clock on a 7 segment 4 digit led. i have the clock working but i want to add a adjust button that pauses the loop, so i adjust the clock to the right time, and then keep going from where i adjusted it to. I’ve tried to do this but it doesn’t do anything, and I have tried moving the digitalRead() code to other places in the code but it still won’t work. Here is the code below:

#define ontime 3

#define A 11
#define B 7
#define C 4
#define D 2
#define E 13
#define f 10
#define G 5
#define DP 3

#define CC1 12
#define CC2 9
#define CC3 8
#define CC4 6

#define Button3 A3

int B4 = 0;

#define numbersegments { \
 {1,1,1,1,1,1,0,0},\
 {0,1,1,0,0,0,0,0},\
 {1,1,0,1,1,0,1,0},\
 {1,1,1,1,0,0,1,0},\
 {0,1,1,0,0,1,1,0},\
 {1,0,1,1,0,1,1,0},\
 {0,0,1,1,1,1,1,0},\
 {1,1,1,0,0,0,0,0},\
 {1,1,1,1,1,1,1,0},\
 {1,1,1,0,0,1,1,0} }
 
byte numbers[10][8] = numbersegments;

const int segments [8] = { 
  A, B, C, D, E, f, G, DP };

void setup(){
  pinMode(A, OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(C,OUTPUT);
  pinMode(D,OUTPUT);
  pinMode(E,OUTPUT);
  pinMode(f,OUTPUT);
  pinMode(G,OUTPUT);
  pinMode(DP,OUTPUT);

  pinMode(CC1,OUTPUT);
  pinMode(CC2,OUTPUT);
  pinMode(CC3,OUTPUT);
  pinMode(CC4,OUTPUT);
}

void loop(){
  
  B4 = digitalRead(Button3);
  if(B4 == LOW) {

  for(int digit1 = 0; digit1 < 3; digit1 ++){
    if(digit1 == 2)
    {
      for(int digit2 = 0; digit2 < 4; digit2 ++) {
        for(int digit3 = 0; digit3 < 6; digit3 ++)
          for(int digit4 = 0; digit4 < 10; digit4 ++)
            for(int t = 0; t < 4900; t++){// time it takes to change number.

              setsegments(digit1,CC1,ontime);
              setsegments(digit2,CC2,ontime);
              setsegments(digit3,CC3,ontime);
              setsegments(digit4,CC4,ontime);
            }
      }
    }
    else { 
      for(int digit2 = 0; digit2 < 10; digit2 ++) {
        for(int digit3 = 0; digit3 < 6; digit3 ++)
          for(int digit4 = 0; digit4 < 10; digit4 ++)
            for(int t = 0; t < 4900; t++){// time it takes to change number.

              setsegments(digit1,CC1,ontime);
              setsegments(digit2,CC2,ontime);
              setsegments(digit3,CC3,ontime);
              setsegments(digit4,CC4,ontime);
            }
      }
    }
  }
  }
}

void setsegments(int number, int digit, int time){
  for (int seg = 0; seg < 8; seg++){
    if (numbers[number][seg]==1) {
      digitalWrite(segments[seg],HIGH);
    }
    else {
      digitalWrite(segments[seg],LOW);
    }
  }
  digitalWrite(digit,HIGH);
  delay(time);
  digitalWrite(digit,LOW);
}

Any solutions?

Hi,

Try adding

pinMode(Button3, INPUT_PULLUP);

to your setup() function.

Paul

I will give it a try but just so i learn what does that do??

i added it but it still will not pause, it just keeps going.

Still peddling that bad code, you were told it was the wrong way to do things here http://forum.arduino.cc/index.php?topic=300895.msg2096111#msg2096111

Anyway the answer is still the same as we gave then.

No it will not pause unless you write code to make it pause.

I was concerned that your button input was floating. You need either an external pullup resistor, or that command I suggested to stop it floating. A floating input can't be relied upon to read either high or low, or change when the button is pressed.

But reading your sketch again, I can see the button will only ever get checked at "midnight". You need to check the button in the centre of the for loops, just before you call setsegments(). Like this for example:

          do {
              setsegments(digit1,CC1,ontime);
              setsegments(digit2,CC2,ontime);
              setsegments(digit3,CC3,ontime);
              setsegments(digit4,CC4,ontime);
            } while (digitalRead(Button3) == LOW);

that has got it working thanks PaulRB

No problem.

But Mike is right, its a crazy way to write a clock sketch. Each time it starts, it goes back to midnight. The only way to set it to the correct time is to switch it on at midnight. Your button doesn't really help much, you would have to press it for hours...

that is just the start of it i will be making an adjust button.