I have a have project of Up Down counter using Arduino with seven segments, in which when I continuously press the push button, the counter moves upward from 0 to 99. When I release the push button, the counter moves downwards from the previous value.
For example: I continuously press the button counter from 0 to 58, then when I release the button, the counter moves backward from 58 to 0.
The problem is that when I simulate the code in Proteus software, the counter starts from 99 because the button is not pressed. But when I press the button the counter moves upward not accurately.
Also the other big issue is that when the button is released, the counter does not move backwards although the counter moves upwards continuously and the button is not working (whether the button is pressed or not).
Here my code and Proteus simulator circuit image.
int num_array_1[10][7] =
{ { 1,1,1,1,1,1,0 }, // 0
{ 0,1,1,0,0,0,0 }, // 1
{ 1,1,0,1,1,0,1 }, // 2
{ 1,1,1,1,0,0,1 }, // 3
{ 0,1,1,0,0,1,1 }, // 4
{ 1,0,1,1,0,1,1 }, // 5
{ 1,0,1,1,1,1,1 }, // 6
{ 1,1,1,0,0,0,0 }, // 7
{ 1,1,1,1,1,1,1 }, // 8
{ 1,1,1,0,0,1,1 }}; // 9
int num_array_2[10][7] =
{ { 1,1,1,1,1,1,0 }, // 0
{ 0,1,1,0,0,0,0 }, // 1
{ 1,1,0,1,1,0,1 }, // 2
{ 1,1,1,1,0,0,1 }, // 3
{ 0,1,1,0,0,1,1 }, // 4
{ 1,0,1,1,0,1,1 }, // 5
{ 1,0,1,1,1,1,1 }, // 6
{ 1,1,1,0,0,0,0 }, // 7
{ 1,1,1,1,1,1,1 }, // 8
{ 1,1,1,0,0,1,1 }}; // 9
const int button = 35;
const int alarm = 50;
//function header
void Num_Write_1(int);
void Num_Write_2(int);
void setup() {
// set pin modes for Display 1
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(28, OUTPUT);
// set pin modes for Display 2
pinMode(40, OUTPUT);
pinMode(41, OUTPUT);
pinMode(42, OUTPUT);
pinMode(43, OUTPUT);
pinMode(44, OUTPUT);
pinMode(45, OUTPUT);
pinMode(46, OUTPUT);
// set pin for alarm indication
pinMode(alarm,OUTPUT);
// set pin for button input
pinMode(button,INPUT);
int i = 1;
int j = 1;
int counter_1 = 0; // for counter 1
int counter_2 = 0; // for counter 2
}
void loop() {
int counter_1; // for counter 1
int counter_2; // for counter 2
int button_state = digitalRead(button);
// check the button state
//counter loop
if (button_state == HIGH) {
// if button is pressed , counter moves Upward(0,1,2,3,....,99)
boolean start = true;
while (start) {
// when button pressed , loop is continuously running
if (counter_1 == 10) {
// if counter 1 is value 10
counter_1 = 0; // reset counter1 to 0
counter_2 = counter_2 + 1; // counter2 is starting now
Num_Write_1(counter_1); // display value of counter1
Num_Write_2(counter_2); // display value of counter2
button_state = digitalRead(button); // check the button state
if (button_state == LOW) {
// if low then condition of while is false
// button is released, button_state is low
start = !start; // toggle the while condition true to false
// which terminate the while loop
}
} else {
counter_1 = counter_1 + 1; // incr. 1 by 1
Num_Write_1(counter_1); // display value of counter1
delay(200);
}
button_state = digitalRead(button);
// again check the button state
// button is released, button_state is low
while (button_state == LOW) {
// if low then condition of while is false
start = !start;
}
}
} else {
//if button is release , counter moves Downward(56,55,54,...,0)
Num_Write_1(counter_1); // show previous values on both counters
Num_Write_2(counter_2); // b/c decr. starts from incremented values
boolean start = true;
while (start) {
// when button release , loop is continuously running
if (counter_1 == 0) {
//if counter 1 is value 0
counter_1 = 9; // reset counter1 to 9
counter_2 = counter_2 - 1; // counter2 is starting now
Num_Write_1(counter_1); // display on counter1
Num_Write_2(counter_2); // display on counter2
button_state = digitalRead(button); // check the button state
if (button_state == HIGH) {
// if high then condition of while is false
// button is pressed, button_state is high
start = !start; //toggle the condition
}
} else {
counter_1 = counter_1 - 1; // decr. 1 by 1
Num_Write_1(counter_1); // display on counter1
delay(200);
}
button_state = digitalRead(button); // again check the button_state
// if button pressed , button_state goes high
if (button_state == HIGH) {
start = !start;
// toggle the while condition true to false
// which terminate the while loop
}
}
}
}
// this functions writes values to the sev seg pins for counter1
void Num_Write_1(int number) {
int pin= 22;
for (int j=0; j < 7; j++) {
digitalWrite(pin, num_array_1[number][j]);
pin++;
}
}
// this functions writes values to the sev seg pins for counter2
void Num_Write_2(int number) {
int pin= 40;
for (int j=0; j < 7; j++) {
digitalWrite(pin, num_array_2[number][j]);
pin++;
}
}
If button pressed ----------> logic goes HIGH If button release ----------> logic goes LOW
Please resolve my problem. I have tried for 2 weeks solving this problem but still I'm stuck. Any little advice will be important for me.