7 Segment counter (Up and down counting)

Hello, so i’ve made a 7 segment counter that counts up with one button and down with another button, i wanted to make the button that starts counting up to stop at the current count on a second push, i’ve tried finding out how to do it but no result, i’m pretty new to programming and would like some guidance on how to make “BUTTON” in my code able to “stop” the count at the current count when pushed.

Code:

// Seven-segment LED Display
// Common Catode pins 3 and 8

//   G F - A B
//   | | | | |   -> pins and segments they control
//   ---------
//  F|   A   |B
//   |---G---|   -> segments
//  E|   D   |C
//   ---------
//   | | | | |   -> pins and segments they control
//   E D - C DP

// Segments that make each number when lit:
// 0 => ABCDEF
// 1 => BC
// 2 => ABDEG
// 3 => ABCDG
// 4 => BCFG
// 5 => ACDFG
// 6 => ACDEFG
// 7 => ABC
// 8 => ABCDEFG
// 9 => ABCDFG

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 3
#define B 4
#define C 6
#define D 7
#define E 8
#define F 9
#define G 10

// Pushbutton connected to pin 9
#define BUTTON 13
#define BUTTON2 2

// Common catode;
// on when pin is low
// and off when pin is high
#define ON HIGH
#define OFF LOW

int count = 0; // current display count
int val = LOW;   // digital input from button
int val2 = LOW;
bool flag1 = false;
bool flag2 = false;
const int Wait = 500;
void setup() 
{
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BUTTON, INPUT);
  pinMode(BUTTON2, INPUT);
  zero();
}


void loop() 
{
  if (digitalRead(BUTTON) == HIGH || flag1 == true || digitalRead(BUTTON) == HIGH && flag2 == true) 
  {
    flag1 = true;
    flag2 = false;
    delay(Wait);
    count++;
    stuff();
  }
  if (digitalRead(BUTTON2) == HIGH || flag2 == true || digitalRead(BUTTON2) == HIGH && flag1 == true) 
    {
    flag1 = false;
    flag2 = true;
    delay(Wait);
    count--;
    stuff();
  }
  else if (count < 0)
  {
  count = 0;
  }
  else if (count > 9)
  {
  count = 0;
  }

}


void stuff()
{
 switch (count) {
      case 0:
        zero();
        break;
      case 1:
        one();
        break;
      case 2:
        two();
        break;
      case 3:
        three();
        break;
      case 4:
        four();
        break;
      case 5:
        five();
        break;
      case 6:
        six();
        break;
      case 7:
        seven();
        break;
      case 8:
        eight();
        break;
      case 9: 
        nine();
        count = -1;
        break;
 }
}

// 0 => ABCDEF
void zero() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, OFF);
}

// 1 => BC
void one() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
}

// 2 => ABDEG
void two() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
}

// 3 => ABCDG
void three() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, ON);
}

// 4 => BCFG
void four() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 5 => ACDFG
void five() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 6 => ACDEFG
void six() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 7 => ABC
void seven() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F, OFF);
  digitalWrite(G, OFF);
}

// 8 => ABCDEFG
void eight() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

// 9 => ABCDFG
void nine() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F, ON);
  digitalWrite(G, ON);
}

First thing, if you don’t care about variable names, don’t use variables… But I can tell you, variables with good names are a damn handy tool!

Aka, what is ‘stuff’ and what does ‘flag’…

Next, delay()'s make it a bit tricky to spot button presses…

Simple code (using Bounce2) to show a start/stop thingy.

#include <Bounce2.h>

const byte ButtonPin = 9; // button to GND
const byte DebugLedPin = 13;

Bounce button;

bool started;

void setup(){
  button.attach(ButtonPin, INPUT_PULLUP);
  pinMode(DebugLedPin, OUTPUT);
}

void loop(){
  button.update();
  if(button.fell()){
    started = !started; //flips started true <-> false
  }
  
  //led to show if we're started or not
  digitalWrite(DebugLedPin, started);
}

Thanks for the reply, i will implement this in my code and you have a good point on my names on the variables. will change those aswell, before seeing this i did not know that you could use bool in that way. i haven't used it for more than "true/false" so thanks alot! :) will post working code when it's done!