Hi
Actually I'm a beginner.i want to write a code to detect debouncing input signal. i tried With debouncing program. But it's not working for my purpose.
My purpose is simple
If input signal is "High" Turn ON output LED.
If input signal is "Low" Turn OFF output LED.
If input signal is " bouncing" Turn OFF output LED until the bouncing signal becomes "High".
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin = 10; // 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 unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
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);
// set initial LED state
digitalWrite(ledPin, ledState);
}
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:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
I’d suggest that rather than millis(), which is quite suitable for the actual requirement of debouncing, you might want to use micros().
The only practical purpose of what you’re trying to measure is to characterise different types of switches or contacts as they age, and their contact bounce characteristics ’might’ change.
An interesting question, but not much use in normal project work.
If your purpose is to detect 3 states, HIGH, LOW or BOUNCING then please explain why you want to do it rather than just ignoring the input until it has stopped changing rapidly because of contact bouncing
This is really confusing language. Also your answer about the purpose is unhelpful. It's like,
"Why are you taking the car?"
"Because the engine makes the wheels turn and cause it to move".
Let me try to rephrase your need, see if you agree...
"I want to monitor a rectified AC signal, and detect whether the signal is full wave rectified, or half wave rectified".
What is the difference? Mainly if you detect a half wave pulse, with full wave another one immediately follows, with half wave the next pulse is delayed by a half wave interval.
Both situations will have pulse transitions, so it's not good enough to just look for the presence/absence of pulses.
That is what you should design your program around. Don't ever mention "bouncing" again, it will confuse the snot out of people because your application has nothing to do with it.
Along with a schematic, it would be very helpful to explain the fundamental need behind this project.
That is not a purpose. That is a definition or requirement.
Also it is not simple, because "pulsating" has an overlapping definition with "High" and "Low" at small time scales. The conditions of 1,2, and 3 are conceptual not physical conditions. "Pulsating DC" consists of "High" and "Low" states.
At what point does a "High" become a "Low" and not a "Pulsating"? Obviously it is based on timing, but you are gaslighting yourself and others, without a solid definition of that.
Also we're still waiting for a schematic...
Most full wave detection circuits would not produce a continuous "High" value.
Where have you been? Muddled / confusing thinking and descriptions are the standard around here ... along with a good dose of XY Problem.
I'd low pass filter the signal with a simple RC circuit and connect it to an analog input. Then, calibrate thresholds for the 3 cases of interest. Be sure to put some hysteresis around the thresholds.
Here is a sketch that will stretch any changes in the input to 1/4 second. If the input is steady HIGH, any small gitch to LOW will cause the LED to turn off for 1/4 second. If the input is steady LOW, any small glitch to HIGH will cause the LED to light up for 1/4 second. I think that might be what you were going for.
const byte ButtonPin = 8; // the number of the pushbutton pin
const byte LEDPin = 10; // the number of the LED pin
// Variables will change:
int LastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long LastStateChangeTime = 0; // the last time the output pin was toggled
const unsigned long MinimumBounceTime = 250; // the debounce time; increase if the output flickers
void setup()
{
pinMode(ButtonPin, INPUT);
pinMode(LEDPin, OUTPUT);
// set initial LED state
digitalWrite(LEDPin, digitalRead(ButtonPin));
}
void loop()
{
unsigned long currentMillis = millis();
int buttonState = digitalRead(ButtonPin);
// If the switch changed, due to noise or pressing:
if (buttonState != LastButtonState && currentMillis - LastStateChangeTime > MinimumBounceTime)
{
LastStateChangeTime = currentMillis;
LastButtonState = buttonState;
digitalWrite(LEDPin, buttonState);
}
}
I would use a timing approach. The output should be high, if and only if, a certain time has elapsed after it has been low. That's very easy to do with millis().
Every time you sample the input and it's Low, record a time stamp.
Separately, test the difference between now, and the time stamp (subtract them)
Any time that difference is greater than (in your case) a half wave interval, set the output High.
It's easy, similar but not quite the same logic as for debouncing...
This form of DC detection has the definition, "the input has been HIGH for at least time t". It's a fairly foolproof definition. Adjust time 't' to suit your desired definition.
Keep in mind, the difference between AC and DC can not be determined immediately after a LOW to HIGH input transition. Only after some user defined time can it be considered DC, because as we all know, AC is alternating DC.