1 Digit (7 Segment) Up and down counter (With Pause on up counter)

Hello, i am pretty new to Arduino programming and i was programming a 1 digit, 7 Segment counter that counts up from 0-9 (Repeat) when “BUTTON” is pressed and counts down from current count to 0 when “BUTTON2” is pushed, i’ve been trying for days now to make it so when i push the first button a second time, the count pauses at current count, i’ve tried bouncing, INPUT_PULLUP and none has worked.

Could anyone please help me or give me some advice? it would be very much appreciated.

Code:

#define A 3
#define B 4
#define C 6
#define D 7
#define E 8
#define F 9
#define G 10


#define BUTTON2 12
#define BUTTON 2
const int Wait = 500;


// 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 Last_State = false;
bool Last_State2 = false;

void setup() 
{
    
  pinMode(BUTTON,INPUT); // Attach the debouncer to a pin with INPUT_PULLUP mode
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BUTTON2, INPUT);
  zero();  
}


void loop() 
{
   val = digitalRead(12);
   val2 = digitalRead(2);
  // Turn on the LED if either button is pressed :
  if (val == HIGH && Last_State == true)
  {
  Last_State = false;
  Last_State2= false;
  }
  if (val == HIGH && Last_State == false || val == HIGH && Last_State2 == true) 
  {
    Last_State = true;
    Last_State2 = false;
    count++;
    delay(Wait);
    Casey();
   }
    if (val2 == HIGH || Last_State2 == true || val2 == HIGH && Last_State == true) 
    {
    Last_State = false;
    Last_State2 = true;
    delay(Wait);
    count--;
    Casey();
    }
  

  else if (count < 0)
  {
  count = 0;
  }
  else if (count > 9)
  {
  count = 0;
  zero();
  }


}

void Casey()
{
 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();
        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);
}

Please post a hand drawn diagram (not Fritzing) showing how everything is wired, especially the buttons.

I would look at your project like this.

Check for Button A being pressed
Check for Button B being pressed
Check update time has past, if so call update function.
Loop

Now just need for buttons to set a “countdirection” variable to one of three modes, up, down, stay. and call update function if pressed.

Update function increments/decrements displaynumber variable based off countdirection,updates leds shown, sets time variable to be used before auto updating again.

jremington:
Please post a hand drawn diagram (not Fritzing) showing how everything is wired, especially the buttons.

Hold on, i will draw it on a paper and then upload

jremington:
Please post a hand drawn diagram (not Fritzing) showing how everything is wired, especially the buttons.

Just posted in the top post a drawing of the buttons,

OK on the wiring, INPUT_PULLUP is not required, as you have a pulldown resistor.

The program logic is very confusing. To which “if” statement are these “else ifs” supposed to be connected?

  else if (count < 0)
  {
  count = 0;
  }
  else if (count > 9)
  {
  count = 0;
  zero();
  }

Each time you increment or decrement count, you should check for and correct illegal values.

I suggest to forget the counting and display process for the time being, and work out how to reliably read the buttons. You may need to include the bounce library. Just use an LED or Serial.print to indicate button status changes.

jremington:
OK on the wiring, INPUT_PULLUP is not required, as you have a pulldown resistor.

The program logic is very confusing. To which “if” statement are these “else ifs” supposed to be connected?

  else if (count < 0)

{
  count = 0;
  }
  else if (count > 9)
  {
  count = 0;
  zero();
  }



Each time you increment or decrement count, you should check for and correct illegal values.

I suggest to forget the counting and display process for the time being, and work out how to reliably read the buttons. You may need to include the bounce library. Just use an LED or Serial.print to indicate button status changes.

Those statements were for the counter, otherwise it didn’t stop going -1, -2 etc, and 10, 11 etc. i tried the boune library but i didn’t get it to work :S

Latest code here, i got it to reset the number to 0 but the counter keeps going. any ideas?

#define A 3
#define B 4
#define C 6
#define D 7
#define E 8
#define F 9
#define G 10


#define BUTTON2 12
#define BUTTON 2
const int Wait = 500;


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

int Husk = 0;

int count = 0; // current display count
int val = LOW;   // digital input from button
int val2 = LOW;

bool Last_State = false;
bool Last_State2 = false;

void setup() 
{
    
  pinMode(BUTTON,INPUT); // Attach the debouncer to a pin with INPUT_PULLUP mode
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BUTTON2, INPUT);
  zero();  
}


void loop() 
{
   val = digitalRead(12);
   val2 = digitalRead(2);
  // Turn on the LED if either button is pressed :
  if (val == HIGH && Last_State == false) //*****************************THOMAS "endret fra "true" til "false""
  {
  Last_State = true;  //*****************************THOMAS "endret fra "false" til "true""
  Last_State2= false;
  }
  if (val == HIGH || Last_State == true || val == HIGH && Last_State2 == true) //*****************************THOMAS "erstattet "&&" med "||" og endret fra "false" til "true""
  {
    Last_State = true;
    Last_State2 = false;
    count++;
    delay(Wait);
    Casey();
   }
    if (val2 == HIGH || Last_State2 == true || val2 == HIGH && Last_State == true) 
    {
    Last_State = false;
    Last_State2 = true;
    delay(Wait);
    count--;
    Casey();
    }
  

  else if (count < 0)
  {
  count = 10;
  }
  else if (count > 10)
  {
  count = 0;
  zero();
  }

  //*****************************THOMAS
  if(val == HIGH)
  Husk++;

  if (Husk == 1)
  {
   zero();
  }
if(Husk == 2)
{
  Husk =0;
}

  //*****************************THOMAS
}

void Casey()
{
   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();
        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);
}

Managed to get it to stop!, will add in debounce to make the buttons smoother and more accurate tommorow, here is the code so far without debounce:

#define A 3
#define B 4
#define C 6
#define D 7
#define E 8
#define F 9
#define G 10


#define BUTTON2 12
#define BUTTON 2
const int Wait = 500;


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

int Husk = 0;

int count = 0; // current display count
int val = LOW;   // digital input from button
int val2 = LOW;

bool Last_State = false;
bool Last_State2 = false;

void setup() 
{
    
  pinMode(BUTTON,INPUT); // Attach the debouncer to a pin with INPUT_PULLUP mode
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BUTTON2, INPUT);
  zero();  
}


void loop() 
{
   val = digitalRead(12);
   val2 = digitalRead(2);
  if (val == HIGH && Last_State == false)
  {
  Last_State = true;
  Last_State2= false;
  }
  if (val == HIGH || Last_State == true || val == HIGH && Last_State2 == true)
  {
    Last_State = true;
    Last_State2 = false;
    count++;
    delay(Wait);
    Casey();
   }
    if (val2 == HIGH || Last_State2 == true || val2 == HIGH && Last_State == true) 
    {
    Last_State = false;
    Last_State2 = true;
    delay(Wait);
    count--;
    Casey();
    }
  

  else if (count < 0)
  {
  count = 10;
  }
  else if (count > 9)
  {
  count = 0;
  zero();
  }

  if(val == HIGH)
  Husk++;

  if (Husk == 1)
  {
  Last_State = true;
  }
if(Husk == 2 || val2 == HIGH)
{
  Husk =0;
  Last_State = false;
}



}

void Casey()
{
   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();
        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);
}

How about a nice State Machine:

const byte  A = 3;
const byte  B = 4;
const byte  C = 6;
const byte  D = 7;
const byte  E = 8;
const byte  F = 9;
const byte  G = 10;




const byte Button2Pin = 12;
const byte Button1Pin = 2;


const unsigned long Wait = 500;
const unsigned long DebounceTime = 30;


unsigned long DebounceStartTime = 0;
unsigned long TimerStartTime = 0;


enum States {Stopped, CountingUp, CountingDown} State = Stopped;


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


int Count = 0; // current display count


bool Last_State1 = false;
bool Last_State2 = false;


void setup()
{
  pinMode(Button1Pin, INPUT);
  pinMode(Button2Pin, INPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);


  zero();
}




void loop()
{
  unsigned long currentTime = millis();


  boolean button1State = digitalRead(Button1Pin);
  boolean button2State = digitalRead(Button2Pin);


  switch (State)
  {
    case Stopped:


      // If button1 is pressed, count up
      if (button1State != Last_State1 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button1State)  // Just pressed, start counting
        {
          Count = 0;
          State = CountingUp;
          TimerStartTime = currentTime;
        }
      }


      // If button2 is pressed, count down
      if (Count > 0 && button2State != Last_State2 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button2State)  // Just pressed button2, Count down
        {
          State = CountingDown;
          TimerStartTime = currentTime;
        }
      }


      break;


    case CountingUp:
      if (currentTime - TimerStartTime >= Wait)
      {
        Count = (Count + 1) % 10;  // Modulo 10: 0,1,2,3,4,5,6,7,8,9,0,1,2,3...
        Casey();
        TimerStartTime += Wait;
      }


      // If button1 is pressed, Stop
      if (button1State != Last_State1 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button1State)  // Just pressed button1, Stop counting
        {
          State = Stopped;
          TimerStartTime = currentTime;
        }
      }


      // If button2 is pressed, CountDown
      if (button2State != Last_State2 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button2State)  // Just pressed button2, Count down
        {
          State = CountingDown;
          TimerStartTime = currentTime;
        }
      }


      break;


    case CountingDown:
      if (Count > 0 && currentTime - TimerStartTime >= Wait)
      {
        Count--;
        Casey();
        TimerStartTime += Wait;
      }


      // Stop when you reach zero
      if (Count == 0)
      {
        State = Stopped;
      }


      break;
  }
}


void Casey()
{
  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();
      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);
}

johnwasser:
How about a nice State Machine:

const byte  A = 3;

const byte  B = 4;
const byte  C = 6;
const byte  D = 7;
const byte  E = 8;
const byte  F = 9;
const byte  G = 10;

const byte Button2Pin = 12;
const byte Button1Pin = 2;

const unsigned long Wait = 500;
const unsigned long DebounceTime = 30;

unsigned long DebounceStartTime = 0;
unsigned long TimerStartTime = 0;

enum States {Stopped, CountingUp, CountingDown} State = Stopped;

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

int Count = 0; // current display count

bool Last_State1 = false;
bool Last_State2 = false;

void setup()
{
  pinMode(Button1Pin, INPUT);
  pinMode(Button2Pin, INPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);

zero();
}

void loop()
{
  unsigned long currentTime = millis();

boolean button1State = digitalRead(Button1Pin);
  boolean button2State = digitalRead(Button2Pin);

switch (State)
  {
    case Stopped:

// If button1 is pressed, count up
      if (button1State != Last_State1 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button1State)  // Just pressed, start counting
        {
          Count = 0;
          State = CountingUp;
          TimerStartTime = currentTime;
        }
      }

// If button2 is pressed, count down
      if (Count > 0 && button2State != Last_State2 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button2State)  // Just pressed button2, Count down
        {
          State = CountingDown;
          TimerStartTime = currentTime;
        }
      }

break;

case CountingUp:
      if (currentTime - TimerStartTime >= Wait)
      {
        Count = (Count + 1) % 10;  // Modulo 10: 0,1,2,3,4,5,6,7,8,9,0,1,2,3...
        Casey();
        TimerStartTime += Wait;
      }

// If button1 is pressed, Stop
      if (button1State != Last_State1 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button1State)  // Just pressed button1, Stop counting
        {
          State = Stopped;
          TimerStartTime = currentTime;
        }
      }

// If button2 is pressed, CountDown
      if (button2State != Last_State2 && currentTime - DebounceStartTime > DebounceTime)
      {
        DebounceStartTime = currentTime;
        if (button2State)  // Just pressed button2, Count down
        {
          State = CountingDown;
          TimerStartTime = currentTime;
        }
      }

break;

case CountingDown:
      if (Count > 0 && currentTime - TimerStartTime >= Wait)
      {
        Count--;
        Casey();
        TimerStartTime += Wait;
      }

// Stop when you reach zero
      if (Count == 0)
      {
        State = Stopped;
      }

break;
  }
}

void Casey()
{
  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();
      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);
}

That's a really nice millis version of the code:o will save this one, nice!

Darkredeemer:
That's a really nice millis version of the code:o will save this one, nice!

If your sketch has more than two states, it's time for a State Machine. :slight_smile: