Hi everyone!
How can I debounce a push button when the "ledPin" only need to be HIGH just a 1 ms? I try a lot of things but nothing works.
Thank you.
Hi everyone!
How can I debounce a push button when the "ledPin" only need to be HIGH just a 1 ms? I try a lot of things but nothing works.
Thank you.
Debouncing refers to ignoring changes in the state of a switch for a short duration after a state change is detected. It has NOTHING to do with what you do with the state of the switch.
This means that there are not any way to debounce a push button that works a only 1ms when is press?
This means that there are not any way to debounce a push button that works a only 1ms when is press?
You can debounce a switch, regardless of how long it is pressed.
Instead of how, you need to explain what. We have no clue what you are trying to do or why you think that switch bouncing is happening.
PaulS:
Debouncing refers to ignoring changes in the state of a switch for a short duration after a state change is detected. It has NOTHING to do with what you do with the state of the switch.
AND, if the code that is executed runs longer than the switch bounce, you do NOT need "debounce". Your code effectively negates the need to debounce the switch.
Paul
PaulS:
You can debounce a switch, regardless of how long it is pressed.Instead of how, you need to explain what. We have no clue what you are trying to do or why you think that switch bouncing is happening.
I need to make that a led turn on just 1 ms although the push button is still pressed. Then I release the push button and the led still off. And repeat the process any time I want. Is that possible?
That had nothing to do with debounce. But more with state change. Simply only act when the button changed state from not pressed to pressed. But because the very small time (what on earth do you want to do with a led blink of 1ms???) you might need debounce on the button because the bounces may take more then the 1ms.
Do you want to learn or just code? For the first, open the Debounce example in the IDE. For the last, grab a library like Bounce2.
septillion:
That had nothing to do with debounce. But more with state change. Simply only act when the button changed state from not pressed to pressed. But because the very small time (what on earth do you want to do with a led blink of 1ms???) you might need debounce on the button because the bounces may take more then the 1ms.Do you want to learn or just code? For the first, open the Debounce example in the IDE. For the last, grab a library like Bounce2.
I want to learn
Okey, I put the debounce code and add the library Bounce2 to the sketch. But now if I change the buttonSttate to LOW from the loop nothing works well:
#include <Bounce2.h>
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 11; // the number of the LED pin
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
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
}
I need to make that a led turn on just 1 ms although the push button is still pressed. Then I release the push button and the led still off. And repeat the process any time I want. Is that possible?
None of the code in the next posts, with unnecessary complexity, is needed.
The state change detection example shows you how to determine that a switch HAS BECOME pressed.
When the switch has become pressed, turn the LED and record the time.
Independently, on every pass through loop(), see if the LED is on (the on time is not 0) and has been on long enough. If that is the case, turn it off and set the on time to 0.
Hi again,
I try everything in this 3 days but nothing works... I understand all the code that should be in the sketch but I am very noob programing.
Could be possible to teach me with an example sketch with descriptions about what do every line of code? Not all the lines, only the important things.
Thank you.
I try everything in this 3 days but nothing works
Pick some code. Post it. Explain what it actually does. Explain what you expect. Explain how those two differ. Explain how the switch is wired.
Could be possible to teach me with an example sketch
The state change example does that, in a very straight-forward manner, with comments.
I use Mr. Gammon's Switch Manager library.
If you look at it you will be able to figure out what has to be done.
Here is an example how to use it:
/*SwitchManager skeleton
This sketch is to introduce new people to the SwitchManager library written by Nick Gammon
The library handles switch de-bouncing and provides timing and state change information in your sketch.
The SwitchManager.h file should be placed in your libraries folder, i.e.
C:\Users\YourName\Documents\Arduino\libraries\SwitchManager\SwitchManager.h
You can download the library at:
http://gammon.com.au/Arduino/SwitchManager.zip Thank you Nick!
In this example we have 2 normally open (N.O.) switches connected to the Arduino - increment and decrement.
The increment switch will also be used as a "Reset" switch if pressed for more than two seconds.
The two switches are connected between GND (0 volts) and an Arduino input pin.
The library enables pull-up resistors for your switch inputs.
Pushing a switch makes its pin LOW. Releasing a switch makes its pin HIGH.
The SwitchManager library provides 10ms de-bounce for switches.
i.e. enum { debounceTime = 10, noSwitch = -1 };
If you need more time, edit the SwitchManager.h file
i.e. enum { debounceTime = 50, noSwitch = -1 }; //here it is changed to 50ms
*/
#include <SwitchManager.h>
//object instantiations
SwitchManager myIncSwitch;
SwitchManager myDecSwitch;
unsigned long currentMillis;
unsigned long heartBeatMillis;
unsigned long heartFlashRate = 500UL; // time the led will change state
unsigned long incShortPress = 500UL; // 1/2 second
unsigned long incLongPress = 2000UL;// 2 seconds
unsigned long decShortPress = 500UL; // 1/2 second
const byte heartBeatLED = 13;
const byte incSwitch = 4; //increment switch is on Arduino pin 4
const byte decSwitch = 5; //decrement switch is on Arduino pin 5
int myCounter;
//======================================================================
void setup()
{
Serial.begin(9600);
//gives a visual indication if the sketch is blocking
pinMode(heartBeatLED, OUTPUT);
myIncSwitch.begin (incSwitch, handleSwitchPresses);
myDecSwitch.begin (decSwitch, handleSwitchPresses);
//the handleSwitchPresses() function is called when a switch changes state
} // E N D O F s e t u p ( )
//======================================================================
void loop()
{
//leave this line of code at the top of loop()
currentMillis = millis();
//***************************
//some code to see if the sketch is blocking
if (CheckTime(heartBeatMillis, heartFlashRate, true))
{
//toggle the heartBeatLED
digitalWrite(heartBeatLED,!digitalRead(heartBeatLED));
}
//***************************
//check to see what's happening with the switches
//"Do not use delay()s" in your sketch as it will make switch changes unresponsive
//Use BlinkWithoutDelay (BWD) techniques instead.
myIncSwitch.check ();
myDecSwitch.check ();
//***************************
//put other non-blocking stuff here
} // E N D O F l o o p ( )
//======================================================================
// F U N C T I O N S
//======================================================================
// C h e c k T i m e ( )
//**********************************************************************
//Delay time expired function
//parameters:
//lastMillis = time we started
//wait = delay in ms
//restart = do we start again
boolean CheckTime(unsigned long & lastMillis, unsigned long wait, boolean restart)
{
//has time expired for this task?
if (currentMillis - lastMillis >= wait)
{
//should this start again?
if(restart)
{
//yes, get ready for the next iteration
lastMillis = millis();
}
return true;
}
return false;
} // E N D o f C h e c k T i m e ( )
// h a n d l e S w i t c h P r e s s e s( )
//**********************************************************************
void handleSwitchPresses(const byte newState, const unsigned long interval, const byte whichPin)
{
// You get here "ONLY" if there has been a change in a switches state.
//When a switch has changed state, SwitchManager passes this function 3 arguments:
//"newState" this will be HIGH or LOW. This is the state the switch is in now.
//"interval" the number of milliseconds the switch stayed in the previous state
//"whichPin" is the switch pin that we are examining
switch (whichPin)
{
//***************************
//are we dealing with this switch?
case incSwitch:
//has this switch gone from LOW to HIGH (gone from pressed to not pressed)
//this happens with normally open switches wired as mentioned at the top of this sketch
if (newState == HIGH)
{
//The incSwitch was just released
//was this a short press followed by a switch release
if(interval <= incShortPress)
{
Serial.print("My counter value is = ");
myCounter++;
if(myCounter > 1000)
{
//limit the counter to a maximum of 1000
myCounter = 1000;
}
Serial.println(myCounter);
}
//was this a long press followed by a switch release
else if(interval >= incLongPress)
//we could also have an upper limit
//if incLongMillis was 2000UL; we could then have a window between 2-3 seconds
//else if(interval >= incLongMillis && interval <= incLongMillis + 1000UL)
{
//this could be used to change states in a StateMachine
//in this example however, we will just reset myCounter
myCounter = 0;
Serial.print("My counter value is = ");
Serial.println(myCounter);
}
}
//if the switch is a normally closed (N.C.) and opens on a press this section would be used
//the switch must have gone from HIGH to LOW
else
{
Serial.println("The incSwitch was just pushed");
}
break; //End of case incSwitch
//***************************
//are we dealing with this switch?
case decSwitch:
//has this switch gone from LOW to HIGH (gone from pressed to not pressed)
//this happens with normally open switches wired as mentioned at the top of this sketch
if (newState == HIGH)
{
//The decSwitch was just released
//was this a short press followed by a switch release
if(interval <= decShortPress)
{
Serial.print("My counter value is = ");
myCounter--;
if(myCounter < 0)
{
//don't go below zero
myCounter = 0;
}
Serial.println(myCounter);
}
}
//if the switch is a normally closed (N.C.) and opens on a press this section would be used
//the switch must have gone from HIGH to LOW
else
{
Serial.println("The decSwitch switch was just pushed");
}
break; //End of case decSwitch
//***************************
//Put default stuff here
//default:
//break; //END of default
} //End switch (whichPin)
} // E n d o f h a n d l e S w i t c h P r e s s e s ( )
//======================================================================
// E N D O F C O D E
//======================================================================
.
PedrinbeepCNC:
I want to learn
Great
PedrinbeepCNC:
Okey, I put the debounce code and add the library Bounce2 to the sketch. But now if I change the buttonSttate to LOW from the loop nothing works well:
Not so great
Remove the bounce2 include from your code; it's not used in your code so a useless include.
If you understand the code, you will see that the ledState will only change on a low-to-high transition. If you need it different, you can modify the if condition after the only comment that you did not remove from the debounce example.
Next point is if you use the correct pins or not and if they are correctly wired? And if you used a resistor with the led. For the led, you can initially use pin 13. You also need a pull-down resistor on the switch.
Hi,
if this is what I understand that you want. You want to push a switch turn on an led for 10 ms after the push button it is releases. You can do something like this:
if (digitalRead(swt_pin)==0){digitalWrite(ledpin,HIGH);delay(1000);digitalWrite(ledpin,LOW) ;}
How it works : As soon that you turn on the switch it will turn ON the led for few seconds and then turn it off.
Why use micros() if you try to do something in the range of ms?
And what's up with the shitload of brackets?
En where is 100ms coming from?
And you still don't check for a state change..
And you still didn't explain why on earth you need a 1ms puls...