-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebounce.ino
54 lines (43 loc) · 1.09 KB
/
Debounce.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
*/
int switchPin = 2;
int ledpin = 13;
bool lastButton = LOW;
bool currentButton = LOW;
bool ledOn = false;
void setup()
{
pinMode(switchPin,INPUT);
pinMode(ledpin,OUTPUT);
}
bool debounce(bool 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);
}