1. Arduino learning notebook -- Button experience (VOL. 2)
1. Arduino
learning notebook -- Button experience (vol.2)
The standard is 512 means 2.5V in this
experience, someone get trouble in this way: led will light when the button
haven’t been pushed sometimes. This is caused by the interference, such as the
induced voltage is more than 2.5V, for improving the accuracy, we need to
improve the threshold.
The reason of closing to the button with
hand will light the lED is that there’s no pull-down resistance, then it has
been interference by the outside.
The way of set the Pull-down resistance:
The following is using a Arduino Uno’s
build-in LED as the indicated LED
This program is different from the vol.1,
for this program has been added a software for eliminating noise, the purpose
is for achieve the goal of push button then the LED light, or when you push the
button, the LED won’t make the corresponding act.
What the meaning of software for
eliminating noise? It’s a delay program run after checking if the button has
been push down, it will delay about 5 to 10 ms, then check the button’s state
again, if confirm the button has been pushed, then judge the button has been
pushed, even though the button has been released, it also delays for 5 to 10
ms, after the noise disappear, it turns to the corresponding program.
Hardware for eliminating noise is connect a
capacitance with the value of 0.1uf to the both side of button, however, this
way can not replace the software for eliminating noise, they have different
influence for the circuit, it’s better to use both ways.
[code]
1. / *
2. Switch connection experiment
3. Wiring method:
4. Material: a light touch switch, a 10 k Ω
resistance, a 0.1 uF capacitance (optional)
5. Connection method:
6. Switch between Arduino D3 and + 5V;
7. 10 k connect between the Arduino D3
&gnd Ω resistance;
8. 0.1 uF capacitance is connected to
Arduino D3 and + 5V (not capacitance);
9. * /
10.
11. int Button = 3;
/ / connect switch to D3.
12. int LED = 13;
/ / connect the LED to 13 (actually the UNO
version already has).
13. boolean onoff = LOW;
/ / set a variable to record the state of
the switch.
14. void setup ()
15. {
16. pinMode (Button, INPUT);
/ / button terminal as input
17. pinMode (LED, the OUTPUT);
/ / LED terminal as output
18.}
19. void loop () {
20. if (digitalRead (Button) = = = LOW) / /
because this example detects the rise along the trigger, so first check whether
the input is LOW level,
21. {
22. delay (10);
/ / then, for a while,
23. if (digitalRead (Button) = = HIGH) / /
then the test will become HIGH.
Yes, the button is pressed.
24. {
25. digitalWrite (LED, onoff);
/ / write the current LED state onoff,
26. onoff = (!
onoff);
/ / then the LED state is reversed for the
next time.
27. delay (10);
/ / delay a time to prevent the button from
suddenly disconnecting again.
28. while (the digitalRead (Button) = = =
HIGH) / / the state of the judging Button, if still pressed, wait to be
released.
Prevent the continuous reversal of LED
output
29. {
30. delay (1);
31.
}
32.
}
33. }
评论
发表评论