I've been trying to turn a light on and off using the same button (i.e. Press button light turns on press button again light turns off). i found this code on another page and have tried to make it work, Does anyone know where i'm going wrong?
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
// set the LED using the state of the button:
digitalWrite(ledPin, buttonState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
I have a push button attached to pin2, and a led attached to pin13. when the push button is pushed the light comes on then goes off as soon as you let go of the switch.
Thanks
It looks too complicated for me. How about this? ...
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
byte oldPinState = HIGH;
void setup ()
{
pinMode (buttonPin, INPUT_PULLUP);
pinMode (ledPin, OUTPUT);
} // end of setup
void loop ()
{
byte pinState = digitalRead (buttonPin);
if (pinState != oldPinState)
{
delay (10); // debounce
if (pinState == LOW)
digitalWrite (ledPin, !digitalRead (ledPin)); // toggle pin
oldPinState = pinState; // remember new state
} // end of if pin state change
} // end of loop
Hey all, newby here. So, I'm trying to do this and it's not working. I wrote the code out myself hoping to learn it, but it doesn't work. It works once, the LED will start off, then turn on when I press the button, but won't toggle back off. I keep trying different thing, but to no avail. Any ideas what I'm doing wrong?
Thanks
const int SWpin = 2;
const int LedPin = 13;
int oldState = HIGH;
void setup()
{
pinMode(SWpin, INPUT_PULLUP);
pinMode(LedPin, OUTPUT);
int swState = digitalRead(SWpin);
digitalWrite(SWpin, HIGH);
Serial.begin(9600);
}
void loop(){
int swState = digitalRead(SWpin);
if(swState != oldState)
{
if (swState == LOW)
{
delay(50);
digitalWrite(LedPin, !digitalRead(LedPin));
oldState = swState;
}
}
}
Follow the logic for a second. If swState != oldState and swState == LOW, then oldState == HIGH, right? So you're only ever updating oldState when it is HIGH. oldState should be updated during every iteration of the loop.
cosmos275:
Now to stare at until I can see why....
If you follow the three lines I quoted, and use some simple logic, you'll see why oldState is only ever assigned as HIGH which would make the code functionally equivalent (after the first press) to this:
if (swState == LOW)
{
delay(50);
digitalWrite(LedPin, !digitalRead(LedPin));
}
Any idea why I have to declare "int swState" twice? It won't compile when I omit it from the loop, but I thought the void setup was where you set those up.
So, in Nick's code, storing the new state is inside the ifs, but in mine it doesn't work. I don't see why his works..
You added the curly braces in the wrong place.
if (pinState == LOW)
{ <-- This one is correct
digitalWrite (ledPin, !digitalRead (ledPin)); // toggle pin
} <-- The close goes here
oldPinState = pinState; // remember new state
} <-- not here
cosmos275:
Any idea why I have to declare "int swState" twice? It won't compile when I omit it from the loop, but I thought the void setup was where you set those up.
Scope (Google Keyword). Declared variables inside curly braces are only valid within those, or any nested curly braces. If you want both setup and loop to use the same variable, it needs to be declared in the global scope (outside of both functions).
I see, I was misreading Nick's code. Apparently, when you have an IF followed by one line of code, you don't need brackets at all, hence me thinking the bracket past the state change write was the end of the IF, but it wasn't.
Speaking of minutes > 59, do you know how to make a 24 hour clock easily? I'd like to have scheduled events happen throughout a 24 hour day maybe based on dip switch inputs for which hour for them to occur. Is there an easy way to make a reliable clock from millis() or something?