-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpedal2.ino
130 lines (112 loc) · 3.4 KB
/
pedal2.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// pedal
//
// changelog:
// v1 - basic operation, LED flashes on when pedal pressed
// v2 - LED on when Teensy on, LED flashes OFF when pedal pressed
// - auto power off after X mins of inactivity
// - long press to return to beginning of doc & go home
// - better power management
// Library for debouncing - https://github.com/thomasfredericks/Bounce-Arduino-Wiring
#include <Bounce2.h>
// Library to turn Teensy off - https://github.com/FrankBoesing/T4_PowerButton/
#include <T4_PowerButton.h>
// connect foot pedal switch to this pin and GND
#define SWITCH_PIN 2
// keys
#define NEXT_KEY KEY_RIGHT_ARROW
#define PREV_KEY KEY_LEFT_ARROW
#define HOME_KEY KEY_HOME
// button press lengths
#define LEN_SHORT 750
#define LEN_MID 1500
#define LEN_LONG 2500
// how long to wait (in milliseconds) before auto off
#define AUTO_OFF_TIME 7200000 // 120 minutes
Bounce* bouncer;
unsigned long last_keypress = millis();
unsigned long now = 0;
bool press_key = false;
void startup_late_hook(){
// init all pins as OUTPUT for better power management, per https://www.pjrc.com/teensy/low_power.html
for (uint8_t i = 0; i < CORE_NUM_DIGITAL; i++) {
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
}
void setup() {
// Use built in LED for status
pinMode(LED_BUILTIN, OUTPUT);
// LED should show ON
digitalWrite(LED_BUILTIN, HIGH);
// Setup the button with an internal pull-up
pinMode(SWITCH_PIN,INPUT_PULLUP);
bouncer = new Bounce();
// After setting up the button, setup the Bounce instance
bouncer->attach(SWITCH_PIN);
bouncer->interval(50);
}
void loop() {
// Update the Bounce instance
bouncer->update();
// current time
now = millis();
// if button just went DOWN, start counter
if (bouncer->changed() && bouncer->rose()) {
last_keypress = millis();
}
// if button just went UP, see how long it was down for
if (bouncer->changed() && bouncer->fell()) {
// short press
if (now - last_keypress < LEN_SHORT) {
Keyboard.press(NEXT_KEY);
delay(10);
Keyboard.releaseAll();
// flash LED off once
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
// mid press
} else if (now - last_keypress >= LEN_SHORT && now - last_keypress < LEN_LONG) {
Keyboard.press(PREV_KEY);
delay(10);
Keyboard.releaseAll();
// flash LED off twice
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
// long press
} else if (now - last_keypress > LEN_LONG) {
Keyboard.press(PREV_KEY);
delay(10);
Keyboard.releaseAll();
Keyboard.press(PREV_KEY);
delay(10);
Keyboard.releaseAll();
Keyboard.press(PREV_KEY);
delay(10);
Keyboard.releaseAll();
Keyboard.press(HOME_KEY);
delay(10);
Keyboard.releaseAll();
// flash LED off three times
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
}
}
// if we've had ZERO activity in X time, shut down Teensy
if (now - last_keypress > AUTO_OFF_TIME)
arm_power_down();
}