Dear All,
I have an issue caused by adding a second “button” in to my code that is causing the initial button to not register correctly anymore.
My initial code using a single momentary button acts as follows…
When the code starts, the pushbutton counter goes to 1. When the button is pressed it goes to 2 and turns on the electronics. When pressed again it goes to 3 and turns electronics off, when pressed again it goes to 4 and turns back on again etc etc:
I then wanted RF control so I didn’t have to be local to the unit to switch the electronics on so I added a 433MHz RF receiver/decoder unit and plugged its output in to a different digital pin to the first push button and called it rfButton. I added a second buttonState variable (buttonState2) and second lastButtonState variable (lastButtonState2) and then I modified the if statements so that they incorporated an OR logic statement. Basically, the idea is that if either the pushButton or the rfButton are pressed, the pushButtonCounter should increment by 1 only. The problem in this code is that when rfButton is pressed, the pushButtonCounter does indeed only increment by 1, but if the pushButton is pressed, it increments by 2, which shouldn’t happen.
As a result, what physically happens is that the electronics switch on and off perfectly when the rfButton is pressed. But only switch on if you hold the momentary button down so it’s effectively manually latched. As soon as you let go it turns off again. I cannot for the life of me understand why!
I’ve tried changing the delay() function for debounce but that’s just made the code slower.
I’ve not yet tried changing my “If – OR” statements to switch-case statements and wonder if that’s what I should be doing? Logically, I don’t think that should make any difference though.
I look forward to any advice you may be able to give me!
My original code working perfectly with JUST the pushButton is below. The newer code with both a pushButton and an rfButton will follow after…
Note... I've removed some of the code that is the electronics instruction that happens as a result of the button press as that is irrelevant to the question and is also sensitive data.
const int buttonPin = 2; // the number of the pushbutton pin
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize serial communication:
//baud rate needs to be high in order for loop to run at correct speed
Serial.begin(500000);
}
void loop()
{
/*The code in this loop will detect if a button is pressed and if so, it will turn on some electronics
if it is pressed again it will turn the electronics off.
*/
//read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from on to off:
buttonPushCounter++;
Serial.println("off");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from off to on:
Serial.println("on");
}
// Delay a little bit to avoid bouncing
delay(200);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the electronics every two button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0) {
while (value not true) {
//read the state of the pushbutton value during the while loop
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state during the while loop
//if button pressed during the while loop, turn off electronics and reset them
buttonState = HIGH;
return(0);
}
//turn on the electronics here
buttonPushCounter++;
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
//turn off the electronics here
return(0);
}
}
const int buttonPin = 2; // the number of the pushbutton pin
const int rfPin = 4; // the number of the rf pin
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the pushbutton
int buttonState2 = 0; // current state of the rfbutton
int lastButtonState1 = 0; // previous state of the pushbutton
int lastButtonState2 = 0; // previous state of the rfbutton
void setup()
{
// initialize the pushbutton and rf pins as inputs:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(rfPin, INPUT);
// initialize serial communication:
//baud rate needs to be high in order for loop speed to be high enough
Serial.begin(500000);
}
void loop()
{
/*The code in this loop will detect if a button is pressed and if so, it will turn on some electronics and if pressed again, will the electronics off.
*/
buttonState1 = digitalRead(buttonPin); //read the state of the pushbutton value
buttonState2 = digitalRead(rfPin); // read the state of the rfbutton value
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1 || buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH || buttonState2 == LOW) {
// if the current state is HIGH then the button went from on to off:
buttonPushCounter++;
Serial.println("off");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
Serial.print("PushButton State: ");
Serial.println(buttonState1);
Serial.print("rfButton State: ");
Serial.println(buttonState2);
} else {
// if the current state is LOW then the button went from off to on:
Serial.println("on");
}
// Delay a little bit to avoid bouncing
delay(200);
}
// save the current state as the last state, for next time through the loop
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
// turns on the electronics every two button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0) {
while (stepCount != stepsToTake) {
buttonState1 = digitalRead(buttonPin); //read the state of the pushbutton value during the while loop
buttonState2 = digitalRead(rfPin); // read the state of the rfbutton value during the while loop
// compare the buttonState to its previous state during the while loop
//if button pressed during the while loop, turn off electronics and reset them
if (buttonState1 != lastButtonState1 || buttonState2 != lastButtonState2) {
//code to turn off electronics here
buttonState1 = HIGH;
buttonState2 = LOW;
return(0);
}
//code to turn on electronics here
}
buttonPushCounter++;
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
Serial.print("PushButton State: ");
Serial.println(buttonState1);
Serial.print("rfButton State: ");
Serial.println(buttonState2);
}
else {
//code to turn off electronics here
return(0);
}
}