Hi, I am trying to create a toggle switch with a pushbutton.
I have three variables:
int buttonOld=1;
int buttonNew;
String motState;
The input data (digitalRead(buttonPin)) for the pushbutton pin looks roughly like this:
111100011111000011110000....
where "0" is when I'm pressing the button and "1" is when the button is not being pushed.
I want motState to change each time I press the button.
For example going from "OFF" to "ON" or going from "ON" to "OFF".
So now I have tried to create a toggle switch with the following code:
What happens with the above code is that once I let go of the button
(buttonOld=0 and buttonNew=1)
then
motState="OFF"
and nothing changes if I press the button again.
I understand other ways of creating a toggle switch, however I would really want to know why THIS method does NOT work as this might help me gain some insight I'm clearly missing about the programming logic.
Hi, sorry for the lack of complete info, this is my first time posting on the forum...
I am using the Arduino Uno board.
Button is connected as follows:
GND --> Button --> Pin 2.
Here is the complete code:
int buttonPin=2;
int buttonNew;
int buttonOld=1;
int DT=125;
String msg1="buttonOld: ";
String msg2=", buttonNew: ";
String msg3=", motState: ";
String motState;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin,INPUT);
digitalWrite(buttonPin,HIGH);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(msg1);
Serial.print(buttonOld);
buttonNew=digitalRead(buttonPin);
Serial.print(msg2);
Serial.print(buttonNew);
delay(DT);
if(buttonOld==0 && buttonNew==1){
motState="ON";
}
if(buttonOld==0 && buttonNew==1 && motState=="ON"){
motState="OFF";
}
Serial.print(msg3),
Serial.println(motState);
buttonOld=buttonNew;
}
Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
// by C Goulding aka groundFungus
const byte buttonPin = 4; // the pin that the pushbutton is attached to
const byte ledPin = 13; // the pin that the LED is attached to
bool buttonState = 0; // current state of the button
bool lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input with internal pullup enabled
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 50; // check switch 20 times per second
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the new buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is LOW then the button went from off to on:
digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
Hi groundFungus,
Thank you for the help, but as I mentioned I am able to achieve my goal through different code but still want understand why the code I posted does not work for learning purposes.
-Einar