Counter 0-99 with pushbutton

I want to make counter up with 7segment and 4511 decoder. I used sketch from https://www.instructables.com/Making-a-7-Segment-Display-Counter-With-BCD-Logic/ that allows running counter from 0 to 99.
My project is when IO-pin connected to button is HIGH >> Start Counting, IO-pin connected to button is LOW >> pause counting and when HIGH again resume counting.

My question is how to modify the code from the sketch that I got?
Here's the code:

//BCD 1

int a1 = 4;
int a2 = 5;
int a3 = 6;
int a4 = 7;

//BCD 2
int b1 = 8;
int b2 = 9;
int b3 = 10;
int b4 = 11;

int n=0; //Setting initial value of n to 0
int d1;
int d2;
int button = 2;
int state = 0;

void setup()
{
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(button,INPUT);
}

void loop(){
 state = digitalRead(button);
 if(state == HIGH)
   n++;
  
 if(n==100)
   n=0;
  
 d1=n%10; // This will divide n by 10 and the remainder will be stored in d1
 d2=n/10; // This will divide n by 10 and the value before decimal will be stored in d2
 disp1(d1);
 disp2(d2);
 delay(100);//100 ms delay to debounce
}

void disp1(int num){
  if(num == 0)//0000
  {
    digitalWrite(a1, LOW); 
    digitalWrite(a2, LOW);
    digitalWrite(a3, LOW);
    digitalWrite(a4, LOW);
  }
  if(num == 1)//0001
  {
    digitalWrite(a1, HIGH);
    digitalWrite(a2, LOW);
    digitalWrite(a3, LOW);
    digitalWrite(a4, LOW);
  }
  if(num == 2)//0010
  {
    digitalWrite(a1, LOW);//0
    digitalWrite(a2, HIGH);//1
    digitalWrite(a3, LOW);//0
    digitalWrite(a4, LOW);//0
  }
  if(num == 3)//0011
  {
    digitalWrite(a1, HIGH);//1
    digitalWrite(a2, HIGH);//1
    digitalWrite(a3, LOW);//0
    digitalWrite(a4, LOW);//0
  }
  if(num == 4)//0100
  {
    digitalWrite(a1, LOW);//0
    digitalWrite(a2, LOW);//0
    digitalWrite(a3, HIGH);//1
    digitalWrite(a4, LOW);//0
  }
  if(num == 5)//0101
  {
    digitalWrite(a1, HIGH);//1
    digitalWrite(a2, LOW);//0
    digitalWrite(a3, HIGH);//1
    digitalWrite(a4, LOW);//0
  }
  if(num == 6)//0110
  {
    digitalWrite(a1, LOW);//0
    digitalWrite(a2, HIGH);//1
    digitalWrite(a3, HIGH);//1
    digitalWrite(a4, LOW);//0
  }
  if(num == 7) //0111
  {
    digitalWrite(a1, HIGH);//1
    digitalWrite(a2, HIGH);//1
    digitalWrite(a3, HIGH);//1
    digitalWrite(a4, LOW);//0
  }
  if(num == 8) //1000
  {
    digitalWrite(a1, LOW);//0
    digitalWrite(a2, LOW);//0
    digitalWrite(a3, LOW);//0
    digitalWrite(a4, HIGH);//1
  }
  if(num == 9)//1001
  {
    digitalWrite(a1, HIGH);//1
    digitalWrite(a2, LOW);//0
    digitalWrite(a3, LOW);//0
    digitalWrite(a4, HIGH);//1
  }
}

void disp2(int num)
{  
  if(num == 0)//0000
  {
    digitalWrite(b1, LOW);//
    digitalWrite(b2, LOW);//0
    digitalWrite(b3, LOW);//0
    digitalWrite(b4, LOW);//0
  }
  if(num == 1)//0001
  {
    digitalWrite(b1, HIGH);//1
    digitalWrite(b2, LOW);//0
    digitalWrite(b3, LOW);//0
    digitalWrite(b4, LOW);//0
  }
  if(num == 2)//0010
  {
    digitalWrite(b1, LOW);//0
    digitalWrite(b2, HIGH);//1
    digitalWrite(b3, LOW);//0
    digitalWrite(b4, LOW);//0
  }
  if(num == 3)//0011
  {
    digitalWrite(b1, HIGH);//1
    digitalWrite(b2, HIGH);//1
    digitalWrite(b3, LOW);//0
    digitalWrite(b4, LOW);//0
  }
  if(num == 4)//0100
  {
    digitalWrite(b1, LOW);//0
    digitalWrite(b2, LOW);//0
    digitalWrite(b3, HIGH);//1
    digitalWrite(b4, LOW);//0
  }
  if(num == 5) //0101
  {
    digitalWrite(b1, HIGH);//1
    digitalWrite(b2, LOW);//0
    digitalWrite(b3, HIGH);//1
    digitalWrite(b4, LOW);//0
  }
  if(num == 6) //0110
  {
    digitalWrite(b1, LOW);//0
    digitalWrite(b2, HIGH);//1
    digitalWrite(b3, HIGH);//1
    digitalWrite(b4, LOW);//0
  }
  if(num == 7) //0111
  {
    digitalWrite(b1, HIGH);//1
    digitalWrite(b2, HIGH);//1
    digitalWrite(b3, HIGH);//1
    digitalWrite(b4, LOW);//0
  }
  if(num == 8) //1000
  {
    digitalWrite(b1, LOW);//0
    digitalWrite(b2, LOW);//0
    digitalWrite(b3, LOW);//0
    digitalWrite(b4, HIGH);//1
  }
  if(num == 9)//1001
  {
    digitalWrite(b1, HIGH);//1
    digitalWrite(b2, LOW);//0
    digitalWrite(b3, LOW);//0
    digitalWrite(b4, HIGH);//1
  }
}

Thanks before

How much of this sketch did you write.


Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

this my schematic:

Can you explain what you want the code to do and in what it actually does?

  pinMode(button,INPUT);

should probably be

  pinMode(button,INPUT_PULLUP);

BTW you can improve disp1 and disp2 with bit operations:

void disp1 (int num)
{
  digitalWrite (a1, num & 1) ;
  digitalWrite (a2, num & 2) ;
  digitalWrite (a3, num & 4) ;
  digitalWrite (a4, num & 8) ;
}

void disp2 (int num)
{
  digitalWrite (b1, num & 1) ;
  digitalWrite (b2, num & 2) ;
  digitalWrite (b3, num & 4) ;
  digitalWrite (b4, num & 8) ;
}

That is your problem right there.

Connect a 1K resistor between pin2 and GND

Good point! :sunglasses:

Which is indeed the better way to detect a switch but requires that the switch is connected between the pin and ground, not between pin and 5 V as shown in the schematic. So you then look for the pin to read LOW when the button is pressed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.