forked from orangkucing/MewPro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_Switch.ino
More file actions
84 lines (75 loc) · 1.82 KB
/
Copy pathf_Switch.ino
File metadata and controls
84 lines (75 loc) · 1.82 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Interface to simple mechanical switches with software debounce.
//
// There are possibly two types of switch functions in video mode, and we assign them in the sample code as:
// SWITCH0_PIN --- ON = start recording --> OFF = do nothing --> ON = stop recording --> OFF = do nothing
// SWITCH1_PIN --- ON = start recording --> OFF = stop recording
//
// Simple mechanical switches require debounce. For software debounce, original code can be found at everywhere, for example,
// http://www.arduino.cc/en/Tutorial/Debounce
//
#ifdef USE_SWITCHES
void switchClosedCommand(int state)
{
switch (state) {
case (1 << 0): // SWITCH0_PIN
if (!ledState) {
startRecording();
} else {
stopRecording();
}
break;
case (1 << 1): // SWITCH1_PIN
startRecording();
break;
default:
break;
}
}
void switchOpenedCommand(int state)
{
switch (state) {
case (1 << 0): // SWITCH0_PIN
delay(1000);
break;
case (1 << 1): // SWITCH1_PIN
stopRecording();
break;
default:
break;
}
}
void setupSwitch()
{
pinMode(SWITCH0_PIN, INPUT_PULLUP);
pinMode(SWITCH1_PIN, INPUT_PULLUP);
}
void checkSwitch()
{
static unsigned long lastDebounceTime = 0;
static int lastButtonState = 0;
static int buttonState;
// read switch with debounce
int reading = (digitalRead(SWITCH0_PIN) ? 0 : (1 << 0)) | (digitalRead(SWITCH1_PIN) ? 0 : (1 << 1));
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if (millis() - lastDebounceTime > 100) {
if (reading != buttonState) {
if (reading != 0) {
switchClosedCommand(reading);
} else {
switchOpenedCommand(buttonState);
}
buttonState = reading;
}
}
lastButtonState = reading;
}
#else
void setupSwitch()
{
}
void checkSwitch()
{
}
#endif