Adding Pause button using if statement or while statement.

Hi all :slight_smile: :slight_smile: :slight_smile:

im new here in this community and this is my first post, im having a problem on how to stop the loop in the for loop, i wanted to implement some features to this project, like the pause button, the problem is when i pressed the button, the output seems to run but it never stops, how would i implement the pause button using the while or if statement and what is viable to use?

thank you in advance for the feed back :D.

const int a = 0;
const int b = 1;
const int c = 2;
const int d = 3;
const int e = 4;
const int f = 5;
const int g = 6;
const int tensseg = 7;
const int onesseg = 8;
int tens = 0;
int ones = 0;
int sec = 0;

//push button pins & function
const int button1=9;
const int button2=10;
int lastbuttonstart = 0;
int lastbuttonpause=0;
int buttonstart =0;
int buttonpause =0;

void setup() {
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(tensseg,OUTPUT);
pinMode(onesseg,OUTPUT);

}

void loop() {

buttonstart = digitalRead(button1);
buttonpause = digitalRead(button2);

display(ones);
digitalWrite(tensseg, HIGH);
digitalWrite(onesseg,LOW);
delay (1);
ones = -1;

display(tens);
digitalWrite(tensseg, LOW);
digitalWrite (onesseg, HIGH);
delay(1);
tens= -1;

if (buttonstart == HIGH && buttonpause==LOW){
if (buttonstart != lastbuttonstart){
delay(50);
}
start();
}
if (buttonpause==HIGH && buttonstart==LOW){
if (buttonpause !=lastbuttonpause){
delay(50);
}
pause();
}
}

void display (int num){
switch (num){

case 0:
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
break;

case 1:
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
break;

case 2:
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
break;

case 3:
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
break;

case 4:
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
break;

case 5:
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
break;

case 6:
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
break;

case 7:
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
break;

case 8:
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
break;

case 9:
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
break;
}
}

void start(){
for (tens=0; tens<=9;tens++){
for (ones=0; ones<=9; ones++){
for (sec= 0 ; sec<=100; sec++){

display(ones);
digitalWrite(tensseg, HIGH);
digitalWrite(onesseg,LOW);
delay (1);

display(tens);
digitalWrite(tensseg, LOW);
digitalWrite (onesseg, HIGH);
delay(1);
}

}

}
}

void pause(){
if (tens==5 || ones == 5){
for (tens=5; tens<=9;tens++){
for (ones=5; ones<=9; ones++){
for (sec= 0 ; sec<=100; sec++){

display(ones);
digitalWrite(tensseg, HIGH);
digitalWrite(onesseg,LOW);
delay (1);

display(tens);
digitalWrite(tensseg, LOW);
digitalWrite (onesseg, HIGH);
delay(1);
return;
}
}
}
}
}

You don't declare the mode of the pins that the switches (NOT buttons) are connected to, so they default to INPUT. That means that you need external pullup or pulldown resistors. How ARE your switches wired?

Why does all of your code start in column 1 ?

Why is your code NOT in code tags?

reduchiha22:
im new here in this community and this is my first post, im having a problem on how to stop the loop in the for loop, i wanted to implement some features to this project, like the pause button, the problem is when i pressed the button, the output seems to run but it never stops, how would i implement the pause button using the while or if statement and what is viable to use?

thank you in advance for the feed back :D.

First, your sketch posted the way it should have been:

const int a = 0;
const int b = 1;
const int c = 2;
const int d = 3;
const int e = 4;
const int f = 5;
const int g = 6;
const int tensseg = 7;
const int onesseg = 8;
int tens = 0;
int ones = 0;
int sec = 0;
//push button pins & function
const int button1 = 9;
const int button2 = 10;
int lastbuttonstart = 0;
int lastbuttonpause = 0;
int buttonstart = 0;
int buttonpause = 0;


void setup()
{
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(tensseg, OUTPUT);
  pinMode(onesseg, OUTPUT);
}


void loop()
{
  buttonstart = digitalRead(button1);
  buttonpause = digitalRead(button2);


  display(ones);
  digitalWrite(tensseg, HIGH);
  digitalWrite(onesseg, LOW);
  delay (1);
  ones = -1;


  display(tens);
  digitalWrite(tensseg, LOW);
  digitalWrite (onesseg, HIGH);
  delay(1);
  tens = -1;


  if (buttonstart == HIGH && buttonpause == LOW)
  {
    if (buttonstart != lastbuttonstart)
    {
      delay(50);
    }
    start();
  }
  if (buttonpause == HIGH && buttonstart == LOW)
  {
    if (buttonpause != lastbuttonpause)
    {
      delay(50);
    }
    pause();
  }
}


void display (int num)
{
  switch (num)
  {
    case 0:
      digitalWrite(a, LOW);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, HIGH);
      break;


    case 1:
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      break;


    case 2:
      digitalWrite(a, LOW);
      digitalWrite(b, LOW);
      digitalWrite(c, HIGH);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, HIGH);
      digitalWrite(g, LOW);
      break;


    case 3:
      digitalWrite(a, LOW);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, LOW);
      break;


    case 4:
      digitalWrite(a, HIGH);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      break;


    case 5:
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, HIGH);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      break;


    case 6:
      digitalWrite(a, LOW);
      digitalWrite(b, HIGH);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      break;


    case 7:
      digitalWrite(a, LOW);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      digitalWrite(g, HIGH);
      break;


    case 8:
      digitalWrite(a, LOW);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      break;


    case 9:
      digitalWrite(a, LOW);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, LOW);
      digitalWrite(g, LOW);
      break;
  }
}


void start()
{
  for (tens = 0; tens <= 9; tens++)
  {
    for (ones = 0; ones <= 9; ones++)
    {
      for (sec = 0 ; sec <= 100; sec++)
      {


        display(ones);
        digitalWrite(tensseg, HIGH);
        digitalWrite(onesseg, LOW);
        delay (1);


        display(tens);
        digitalWrite(tensseg, LOW);
        digitalWrite (onesseg, HIGH);
        delay(1);
      }
    }
  }
}


void pause()
{
  if (tens == 5 || ones == 5)
  {
    for (tens = 5; tens <= 9; tens++)
    {
      for (ones = 5; ones <= 9; ones++)
      {
        for (sec = 0 ; sec <= 100; sec++)
        {


          display(ones);
          digitalWrite(tensseg, HIGH);
          digitalWrite(onesseg, LOW);
          delay (1);


          display(tens);
          digitalWrite(tensseg, LOW);
          digitalWrite (onesseg, HIGH);
          delay(1);
          return;
        }
      }
    }
  }
}

You forgot to set the pinMode() for your input pins.

It is very hard to tell from the sketch what you want the sketch to do. Could you explain? What should the display do when you start up the Arduino? What should it do when you press the ‚Äėstart‚Äô button? What should it do when you press the ‚Äėpause‚Äô button?

Why all those for-loops in the pause function; the return statement in the inner loop makes that the code will do exactly one iteration (tens equals 5, ones equals 5, sec equals 0) and after that return to the calling function.

Rather than writing seven lines of code per each digit value it is easy to store 8 segment value in a single byte:

// Bit maps for the seven segment display
const unsigned char Segments[] =
{
  0b11000000, // 0
  0b11001111, // 1
  0b10100100, // 2
  0b10000110, // 3
  0b10001011, // 4
  0b10010010, // 5
  0b10010000, // 6
  0b11000111, // 7
  0b10000000, // 8
  0b10000011, // 9
};

Then, rather than naming the pins for each segment, you can use an array of pin numbers:

const unsigned char SegmentPins[] = {5,6,7,8,9,10,11,12};  // Cathodes (LOW for on, HIGH for off)

Given the array you can set the pin mode with a loop:

void setup()
{
  for (int i=0; i<8; i++)
  {
    pinMode(SegmentPins[i],OUTPUT);
    digitalWrite(SegmentPins[i],HIGH);
  }
}

Then, when it comes time to display a digit, look up the pattern in the array and copy each bit to the appropriate segment:

      // Display the digit on the seven segments
      byte segments = Segments[digit];
      for (int s=0; s<8; s++)
      {
        digitalWrite(SegmentPins[s], segments & 1);
        segments >>= 1;
      }