Good day,
I am a newbie and a bit stuck with my basic code.
The code works fine, but debounceDelay does not have any effect. If i put long debounceDelay = 1000; the code does not wait 1000ms , but executes instantly. What am I missing ?
Many thanks in advance guys !
const int buttonPin = 2;
const int ledPin = 13;
int currState = 0;
int preState = 0;
int reading = 0;
long lastDebounceTime = 0;
long debounceDelay = 150;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
reading = digitalRead(buttonPin);
if (reading != preState) {
if ((millis() - lastDebounceTime) > debounceDelay){
Serial.println("delay finished");
currState = reading;
}
lastDebounceTime = millis();
}
if (currState != preState){
if(preState == LOW){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW); }
preState = currState;
}
}
Using names that appear to go together, like currState and prevState makes it a lot easier to follow the code than names like reading and lastButtonState.
There are two recognized styles for where the { goes. Some people like it on the same line as the statement that it goes with. They're weird. Some people prefer it on the next line, indented the same amount as the statement that it goes with. They're smart.
There is exactly one recognized style for there the } goes. On a line all by itself, properly indented.
I suggest that you adopt a recognized style for your code.
Thanks ,
button is wired like that with 10k resistor. (also modified the code to look good)
Is it something to do with code sequence ?
The deBounceDelay comparison goes before it resets lastDebounceTime ? So basically lastDebounceTime = millis(); should be written before the comparison, although it cannot.
before i did a debounce and state change post.... so if you did serch you would find the answer to your question very easily....
but since i am in a very good mood since all that help me understand what the hell is debounce and state change does is in this post i ask them if they dont mind me sending you all the code that they help me finish?
I don't know where I am doing the mistake ,but I cannot get it to work
Goal is to make the led blink only when "Debounced" NO contact goes from OPEN -> CLOSED.
If contact goes from CLOSED -> OPEN , led remains LOW.
Like simple automation.
why do you use "complicated code" for a simple task.
If you need to "debounce" you can simply read the value, wait and read again (thats what you do anyway).
If the value is the same both times use it otherwise discard it.
illbatt yes you could, btw is that from the ladyada 5th lesson tutorial?
anyway what this code did is that it will reach almost instantaneously so it wont make you wait in your brief window of opening for you to be able to control your circuit
delay() does what it says. It delays execution of the next instruction. That may or may not be important, but in some circumstances it is important not to do block loop() from executing as often as possible such as when reading a sensor, hence the 'Blink without delay' technique which allows loop() to run continuously and not be blocked which gives the effect of doing 2 (or more) things at once.