0

In the following code :- The aim of code is " when push button is pressed , led is on and when push button is is pressed again led is of"

#define led 12
#define button 7
boolean trigger = false;
boolean previous = false;
void setup()
{
  pinMode(led , OUTPUT);
  pinMode(button , INPUT);
}
void loop()
{
  int current = digitalRead(button) ;  
  if (current && !previous)                   
  {
    trigger = !trigger;                   
  }
  previous = current;                                                   
  digitalWrite(led , trigger);  
}

My Questions are as follows:-

  1. When i used digitalread(button) instead of introducing variable i.e current. LED shows abnormal behavior. Why ?
  2. What is the purpose of current = previous expression. Whats its significance?
  3. Also please explain the working of a code in a brief manner in a loop.
  4. Why I should use Boolean instead of int?
  • what is abnormal behavior ... that description has meaning only to you – jsotola Jul 28 at 15:15
1

When i used digitalread(button) instead of introducing variable i.e current. LED shows abnormal behavior. Why ?

Likely this is because of switch bounce. The processor is very fast compared to the bounces a mechanical switch makes when operated. So the results can be unexpected. You can get around this problem in software by using a state machine. Or in hardware by using capacitors to filter out bouncing.

What is the purpose of current = previous expression. Whats its significance?

The program is tracking the state of the switch. So you need to know the previous state of the switch to know if the state of the switch has changed.

Also please explain the working of a code in a brief manner in a loop.

The two fixed or reserved function names in an Arduino sketch are "setup" and "loop". "setup" is called only once on boot up. And "loop" is called over and over again after "setup"is called. In "loop" the code reads that state of the button, then tests to see if the state of the button is true and the previous state is false. If it is, then toggle the variable which tracks the state of the LED. Then the code will always save the current state as the past state and turns on / off the LED according to the value tracking the state of the LED.

Now comes the tricky part. Assume the switch is perfect and does not bounce. The toggling only occurs when the switch is 1st pressed. That is because the next time through "loop" both trigger & previous are true and the test will then be false and the LED will not be toggled. Not until after the switch is released does the code reset previous to false. Now if the switch is pressed again, for only 1 iteration through "loop", the test will be true and the LED will be toggled.

This approach depends on the switch not to bounce. And is difficult to understand. Consider using a 4 state machine (button up / LED off, button down / LED on, button up / LED on, button down/ LED off) rather than this 2 state machine (button down / LED toggle, button up / LED steady).

Why I should use Boolean instead of int?

Bools only have two states. Integers can have many states. It is less confusing and less error prone to use a variable that only has two states if that is the intent of the program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.