Using button to control 7-segment display

hi, so i wanted to make my 7-segment display show numbers from 0 to 9 by pressing a button. for the 1st press it goes on fine. but after i press it the second time, the variable recording number of button presses increases but the display doesnt change. need help pls.

Heres the code:
int outstart = 2;
int outend = 10;

int bpin = 11;
int bstate = 0;

int p= 0;

void setup() {
for (int i=outstart; i<outend; i++){
pinMode (i, OUTPUT);
}
pinMode (bpin, INPUT);
Serial.begin (9600);

}

void loop() {
bstate = digitalRead (bpin);

//Serial.println (p);
delay (100);

if (bstate == HIGH) {
p++;
Serial.println (p);
delay (1000);
}

func();

}

void func (){
if (p==1){
for (int i=3; i<9; i++){
digitalWrite (i, 1);
}

}

if (p==2){
digitalWrite (5, 1);
digitalWrite (8, 1);
}

}

Heres the circuit:

You are doing a lot of digitalWrite( < pin > , HIGH ) equivalent statements but never doing a digitalWrite( < pin > , LOW )
At the end, if it gets that far, all the segments will be on forming the digit 8.

1 Like

Try to post a real schematic, this looks like a spider web and no labels.

…also, when testing the button state, check for a change of state, not just if it’s pressed.
Most switches will ‘chatter’ or bounce several times, leading to odd count values.

Have a look at other seven segment examples to see ow they set/clear the digit segments using an array[] of pins to match the segments for each character.

delay() isn’t your friend… in this case, you can only press the button once a second at best.

Letters are free, you can name your variables more generously.

Thank You, i totally forgot that...

the delay was the problem, thanks

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