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;
readout:
unsigned char button_read = digitalRead(button);
unsigned char last_read = LOW;
//------counting of no. of pulse--------------------------------------------------
if (button_read == HIGH && last_read == LOW){
set_delay;
if (last_read == LOW && button_read == HIGH){
button_count++;
}
else {
button_count = button_count;
}
}
last_read = button_read;
//-------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;
}
goto readout;
}
//-------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;
}
goto readout;
}
//-------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;
}
goto readout;
}
//-------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;
}
goto readout;
}
//-------for condition 4--------------------------------------------------------------
else{
button_count = min_val;
goto readout;
}
}