# Counting number of pulse HELP~

Hello everyone, so i got this activity on our school, i have to count the number of pulse whenever a push button is pressed. here is the sequence.

no. of seven segment sequence
pulse
0 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 repeat
1 1 - 3 - 5 - 7 - 9 repeat
2 9 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1 - 0 repeat
3 0 - 2 - 4 - 6 - 8 repeat
other set pulse to 0

at the start it runs smooth and then when i pressed the button once it go to the second sequence and continue to the 3rd sequence without even pressing the button and then the 7 segment goes giving random digits. here is my code. i am wondering what is wrong with my code

``````#define PIN_CONFIG (0xFF)
#define set_delay delay(100)
#define button (8)
#define max_val (10)
#define min_val (0)
#define one_pulse (1)
#define two_pulse (2)
#define three_pulse (3)

void setup() {
DDRD = PIN_CONFIG;
pinMode(button,INPUT);
}

void loop() {
unsigned char LED[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
unsigned char button_count = min_val;
//------counting of no. of pulse--------------------------------------------------
set_delay;
button_count++;
}
else {
button_count = button_count;
}
}
//-------for condition 1 ----------------------------------------------------------
if (button_count == min_val)
{
unsigned char count = min_val;
for (count = min_val; count < max_val; ++count){
PORTD = LED[count];
set_delay;
}
}
//-------for condition 2------------------------------------------------------------
else if (button_count == one_pulse){
unsigned char count = one_pulse;
for (count = one_pulse; count < max_val; count = count + two_pulse){
PORTD = LED [count];
set_delay;
}
}
//-------for condition 3--------------------------------------------------------------
else if (button_count == two_pulse){
unsigned char count = max_val;
for (count = max_val; count >= min_val; count = count - one_pulse){
PORTD = LED [count];
set_delay;
}
}
//-------for condition 4--------------------------------------------------------------
else if (button_count == three_pulse){
unsigned char count = min_val;
for (count = min_val; count < max_val; count = count + two_pulse){
PORTD = LED [count];
set_delay;
}
}
//-------for condition 4--------------------------------------------------------------
else{
button_count = min_val;
}
}
``````

Welcome. You are off to a pretty good start, anyway.

So you have a hardware BCD decoder on port D? If you don't document it in the code, you should at least mention it here. In general you need more comments in your code.

The very first thing you need to do is lose this:

``````   goto readout;
``````

Goto statements are taboo. If I were your instructor, I would refuse to mark it until it's removed.

For starters, this does absolutly nothing:

``````    else {
button_count = button_count;
}
``````

And then, just ditch the goto's, they are evil and you don't even use it. The loop will restart anyways.

But I think the problem is because you save last_read in a local variable. Every time the loop starts it will be set to LOW. So when you press the button you don't a change but just a push because last_read is always LOW.

To make your life easier, just use the Bounce2 library for the button. And in contrast to your code is non blocking (because you use the also evil delay() ).

To explain that last statement a little, delay() causes the program to do nothing. What happens when buttons are then pushed? Nothing.

Get rid of that stupid # define statement that defines an alias for delay. Call functions properly.