Hello All,
Please help to debug the attached code, as it not working while decrementing through pushbutton2.
Thanks in advance.
sketch_apr28a.ino (1.02 KB)
Hello All,
Please help to debug the attached code, as it not working while decrementing through pushbutton2.
Thanks in advance.
sketch_apr28a.ino (1.02 KB)
Please post your code properly, in line, using code tags.
Describe exactly how you have the buttons wired (post a hand drawn diagram).
Define "not working". Describe what goes wrong.
jremington:
Define "not working". Describe what goes wrong.
not able to decrease the value with push button2.
const int ButtonPin1 = 7; //connected button1 to pin 7
const int ButtonPin2 = 2; //connected button2 to pin 2
//Variable that will be change
int buttonstate1 = 0; //inital state of button1 set to zero
int buttonstate2 = 0; //inital state of button2 set to zero
int buttonpushcounter = 0; //initial state of counter to zero
int lastbuttonstate = 0; //previous state of counter
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //open port for serial monitor
pinMode (ButtonPin1, INPUT); //set buttonpin1 as input
pinMode (ButtonPin2, INPUT); //set buttonpin2 as input
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate1 = digitalRead(ButtonPin1); //read button1 status
if (buttonstate1 != lastbuttonstate) { //compare the previous value of button with current value
if (buttonstate1 == HIGH){ //condition if button1 is high
buttonpushcounter++; //increment the value of push button counter
if (buttonpushcounter >= 10){ //condition for push button countern that should not exceed the value greater than 10
buttonpushcounter = 10; //if exceeding the value of 10 then set pushbutton counter to 10 only
Serial.println("Maximum Value Reached"); //print to serial monitor
}
Serial.println("Button1 Was Pressed"); //print to serial monitor if button1 was pressed
Serial.println(buttonpushcounter); //print value of button push counter on serial monitor
}
lastbuttonstate = buttonstate1; //store to previous for further loop read
buttonstate2 = digitalRead(ButtonPin2); //read button2 status
if (buttonstate2 != lastbuttonstate) { //compare the current value with previous button value
if (buttonstate2 == HIGH) { //condition if button2 is high
buttonpushcounter-=1; //decrement the value of push button counter
if (buttonpushcounter < 0){ //condition if button push counter is less than zero
buttonpushcounter = 0; //set push button counter to zero
Serial.println("Turning all OFF"); //print to serial monitor
}
Serial.println("Button2 Was Pressed"); //print to serial monitor if button2 was pressed
Serial.println(buttonpushcounter); //print the push counter value to serial monitor
}
delay(500); //wait or delay the program 500ms
}
}
lastbuttonstate = buttonstate2; //store the prevoius value for further loop read
}
If you indent your code (use the auto format tool in the IDE) you may see what your problem is
lastbuttonstate = buttonstate1; //store to previous for further loop read
lastbuttonstate = buttonstate2; //store the prevoius value for further loop read
One variable to store states of two different buttons?
const int ButtonPin1 = 7; //connected button1 to pin 7
const int ButtonPin2 = 2; //connected button2 to pin 2
//Variable that will be change
int buttonstate1 = 0; //inital state of button1 set to zero
int buttonstate2 = 0; //inital state of button2 set to zero
int buttonpushcounter = 0; //initial state of counter to zero
int lastbuttonstate1 = 0; //previous state of button1 counter
int lastbuttonstate2 = 0; //previous state of button2 counter
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //open port for serial monitor
pinMode (ButtonPin1, INPUT); //set buttonpin1 as input
pinMode (ButtonPin2, INPUT); //set buttonpin2 as input
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate1 = digitalRead(ButtonPin1); //read button1 status
if (buttonstate1 != lastbuttonstate1) { //compare the previous value of button with current value
if (buttonstate1 == HIGH) { //condition if button1 is high
buttonpushcounter++; //increment the value of push button counter
if (buttonpushcounter >= 10) { //condition for push button countern that should not exceed the value greater than 10
buttonpushcounter = 10; //if exceeding the value of 10 then set pushbutton counter to 10 only
Serial.println("Maximum Value Reached"); //print to serial monitor
}
Serial.println("Button1 Was Pressed"); //print to serial monitor if button1 was pressed
Serial.println(buttonpushcounter); //print value of button push counter on serial monitor
}
lastbuttonstate1 = buttonstate1; //store to previous for further loop read
buttonstate2 = digitalRead(ButtonPin2); //read button2 status
if (buttonstate2 != lastbuttonstate2) { //compare the current value with previous button value
if (buttonstate2 == HIGH) { //condition if button2 is high
buttonpushcounter -= 1; //decrement the value of push button counter
if (buttonpushcounter < 0) { //condition if button push counter is less than zero
buttonpushcounter = 0; //set push button counter to zero
Serial.println("Turning all OFF"); //print to serial monitor
}
Serial.println("Button2 Was Pressed"); //print to serial monitor if button2 was pressed
Serial.println(buttonpushcounter); //print the push counter value to serial monitor
}
delay(500); //wait or delay the program 500ms
}
}
lastbuttonstate2 = buttonstate2; //store the prevoius value for further loop read
}
[quote author=jremington link=msg=3709978 date=1524938042]
[code] lastbuttonstate = buttonstate1; //store to previous for further loop read
lastbuttonstate = buttonstate2; //store the prevoius value for further loop read
One variable to store states of two different buttons?
[/quote]
If you look at your code now, you'll see that you only read the second switch if the first has already been pressed.
TolpuddleSartre:
If you look at your code now, you'll see that you only read the second switch if the first has already been pressed.
Thanks a lot TolpuddleSartre for helping to make my code working properly. Debugging done and code working fine.
const int ButtonPin1 = 7; //connected button1 to pin 7
const int ButtonPin2 = 2; //connected button2 to pin 2
//Variable that will be change
int buttonstate1 = 0; //inital state of button1 set to zero
int buttonstate2 = 0; //inital state of button2 set to zero
int buttonpushcounter = 0; //initial state of counter to zero
int lastbuttonstate1 = 0; //previous state of button1 counter
int lastbuttonstate2 = 0; //previous state of button2 counter
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //open port for serial monitor
pinMode (ButtonPin1, INPUT); //set buttonpin1 as input
pinMode (ButtonPin2, INPUT); //set buttonpin2 as input
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate1 = digitalRead(ButtonPin1); //read button1 status
if (buttonstate1 != lastbuttonstate1) { //compare the previous value of button with current value
if (buttonstate1 == HIGH) { //condition if button1 is high
buttonpushcounter++; //increment the value of push button counter
if (buttonpushcounter >= 10) { //condition for push button countern that should not exceed the value greater than 10
buttonpushcounter = 10; //if exceeding the value of 10 then set pushbutton counter to 10 only
Serial.println("Maximum Value Reached"); //print to serial monitor
}
Serial.println("Button1 Was Pressed"); //print to serial monitor if button1 was pressed
Serial.println(buttonpushcounter); //print value of button push counter on serial monitor
}
}
lastbuttonstate1 = buttonstate1; //store to previous for further loop read
buttonstate2 = digitalRead(ButtonPin2); //read button2 status
if (buttonstate2 != lastbuttonstate2) { //compare the current value with previous button value
if (buttonstate2 == HIGH) { //condition if button2 is high
buttonpushcounter -= 1; //decrement the value of push button counter
if (buttonpushcounter < 0) { //condition if button push counter is less than zero
buttonpushcounter = 0; //set push button counter to zero
Serial.println("Turning all OFF"); //print to serial monitor
}
Serial.println("Button2 Was Pressed"); //print to serial monitor if button2 was pressed
Serial.println(buttonpushcounter); //print the push counter value to serial monitor
}
delay(500); //wait or delay the program 500ms
}
lastbuttonstate2 = buttonstate2; //store the prevoius value for further loop read
}