Can someone help me understand this code of arduino??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone help me understand this code of arduino???

int switchPin = 8; int ledPin = 13; boolean lastButton = LOW; boolean currentButton = LOW; boolean ledOn = false; void setup() {  pinMode(switchPin, INPUT);  pinMode(ledPin, OUTPUT); } boolean debounce(boolean last) {  boolean current = digitalRead(switchPin);  if (last != current)  {    delay(5);    current= digitalRead(switchPin);  }  return current; } void loop() {   currentButton = debounce(lastButton);   if (lastButton == LOW && currentButton == HIGH)   {    ledOn = !ledOn;   }  lastButton = currentButton;  digitalWrite(ledPin, ledOn); } As in this program when the loop runs first time and we pressed the button, current button is set as high and it satisfy the if condition , so ledOn is set to high and then last button will also set to high and led will be on , but in the second loop if I press the button again then how it will able to enter the if block because current button is high and lastbutton is also high so how the led will be off????? please help me out

17th Jun 2017, 5:25 PM
Gagan
Gagan - avatar
8 Answers
0
No, I mean in the process of depressing the button. This is when electrical noise occurs. Electrical noise is when the voltage/current/resistance is in a state of flux or transition between one constant state and another constant state. During this time the voltage may drop and spike several times over the course of a few nano seconds. This will correlate to the electrical contacts inside the switch as they move along each other and the microscopic air gaps between them "bounce" around increasing and decreasing the distance between the components until they settle together and the connection is constant creating the new constant state. With the exception of the delay(5) which lasts 5 milliseconds and is only valid if that if block is entered, the rest of the program could loop through several times in the course of a millisecond or two. So lets say that the entire time it took you to press the button took 75 milliseconds. This means that the delay function could potentially run 15 times (if the rest of the loop took 0 time). The if loop is only entered if there has been a state change though. This is where the noise comes in. The noise results in state change via the voltage spikes and drops that occur. This is why we have a debounce function. If you see flickering while pressing the switch, you can think of each time the light flickers on or off as, the loop has ran, detected a state change, the delay has occurred and the led has been turned on/off. If the button type you are using is the toggle type. It clicks and remains on (HIGH) (the button not the light) and then clicks and remains off (LOW). Then with this code you'd probably see the led initially off then turn on when the button is pressed with maybe some flickering while pressing the button, remaining on when the button is released. Then when the button is pressed again the led may flicker and still remain on when released. When pressed a third time it may flicker and then turn off when released. Alternating on/off every 2 presses.
17th Jun 2017, 8:40 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
okh got it thanks
18th Jun 2017, 1:50 AM
Gagan
Gagan - avatar
+ 1
so basically the loop is running so fast that it does not necessary that we hit the button again in continuous loops it could be possible that we skip some loops?
17th Jun 2017, 7:27 PM
Gagan
Gagan - avatar
0
EXAMPLE CODE for debounce with pushbutton: The circuit: * LED attached from pin 13 to ground or onboard led * pushbutton attached from pin 2 to +5V // note pushbutton not toggle button * 10K resistor attached from pin 2 to ground const int buttonPin = 2; const int ledPin = 13; int ledState = HIGH; int buttonState; int lastButtonState = LOW; // 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); digitalWrite(ledPin, ledState); } void loop() { int reading = digitalRead(buttonPin); if (reading != lastButtonState) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) { ledState = !ledState; } } } digitalWrite(ledPin, ledState); lastButtonState = reading; }
17th Jun 2017, 7:13 PM
ChaoticDawg
ChaoticDawg - avatar
0
When the arduino starts up the current state is led off (boolean ledOn = false;) and button not pressed (boolean lastButton = LOW; boolean currentButton = LOW;). The loop is entered and the first line: currentButton = debounce(lastButton); leads us to the debounce function with lastButton = LOW (false) in the debounce function the first line checks to see if the voltage at the switchPin (8) is high (button has been pressed) since the last time the loop was ran. let's say it has and current is now set to HIGH (true) then the if statement in the debounce function checks and sees the change in the comparison if (last != current) (true) and the if statement is entered (this is the debounce part) there is a 5 millisecond delay before rechecking the switchPin this is done to account for voltage noise while the switch being in the process of being pressed and transitioning between states. again lets say that it's still HIGH (true) the if statement exits and true is returned to currentButton now lastButton = LOW (false) and currentButton = HIGH (true) Now the if statement in the loop is checked if (lastButton == LOW && currentButton == HIGH) (true) the if statement is entered the state of the ledOn boolean is inverted from false to true The next line in the loop lastButton = currentButton; sets the lastButton to now be equal to the currentButton value now lastButton = HIGH (true) and currentButton = HIGH (true) Then the next line digitalWrite(ledPin, ledOn); turns on the led So our current state now is The button is on, the led is on, lastButton = true, currentButton = true, and ledOn = true. back to the top of the loop currentButton = debounce(lastButton); lastButton = HIGH (true) now back into the debounce function let's say the button was pressed again and is now off and the voltage is LOW (false) boolean current = digitalRead(switchPin); current is now set to LOW (false) Continued...............................
17th Jun 2017, 7:17 PM
ChaoticDawg
ChaoticDawg - avatar
0
chaoticDawg as you said back to the top of the loop currentButton = debounce(lastButton); lastButton = High(true) Current will be set to low it is clear till now but when the control returns to loop currentButton is LOW and lastButton is HIGH so how it is going to satisfy if condition if(lastButton==LOW && currentButton ==HIGH) ledOn = !ledOn that is required for turning of the led
17th Jun 2017, 7:18 PM
Gagan
Gagan - avatar
0
Note: this all assumes that you're using a toggle type button. I.E. you click it once and release it and the voltage is HIGH and remains HIGH. Then when you click it again and release it the voltage goes LOW and remains LOW. If you're using a pushbutton that is HIGH when depressed and LOW when released, it will change the results and would probably work fine with some possible flickering. Light would be on when button is depressed the first time and turn off when pressed again.
17th Jun 2017, 8:26 PM
ChaoticDawg
ChaoticDawg - avatar
0
if (last != current) last (lastButton) = HIGH (true) while current = LOW (false) This if comparison results in true and the if statement is once again entered delay(5); // 5 millisecond delay and voltage is confirmed and again set to LOW (false) current= digitalRead(switchPin); false is returned from the debounce function and currentButton is set to LOW (false) currentButton = debounce(lastButton); (false) lastButton = HIGH currentButton = LOW if (lastButton == LOW && currentButton == HIGH) (false) if statement is skipped lastButton is set to LOW and the light remains on lastButton = currentButton; digitalWrite(ledPin, ledOn); So now our state is button is off, light is on, lastButton = LOW, currentButton = LOW, ledOn = true back to the top of the loop currentButton = debounce(lastButton); lastButton = false boolean current = digitalRead(switchPin); // button still off LOW current = false if (last != current) (false) // if statement skipped false is returned currentButton = debounce(lastButton); // currentButton = false lastButton = false currentButton = false if (lastButton == LOW && currentButton == HIGH) // if statement skipped lastButton = currentButton; digitalWrite(ledPin, ledOn); So our state should remain: button is off, light is on, lastButton = LOW, currentButton = LOW, ledOn = true The reality though is that the debounce function is inadequate. You have to remember that these loops happen very very fast. A few thousand times per second. I would be surprised if it worked correctly 100% of the time. What most likely is happening is that the noise from toggling the switch is being detected and flickering the led on/off. I would imagine that the output of operating the switch is a bit erratic. Sometimes it will toggle as expected, other times the led will flicker etc. Once the led is set in a state it won't change state until the switch is in transition again. A debounce of 50 milliseconds or greater would probably change your results.
17th Jun 2017, 8:31 PM
ChaoticDawg
ChaoticDawg - avatar