|
|
@@ -0,0 +1,105 @@ |
|
|
|
/* |
|
|
|
Connections: |
|
|
|
- Volume control potentiometer |
|
|
|
- +5V -> pot MAX |
|
|
|
- A0 -> pot sweeper (center) |
|
|
|
- GND -> pot MIN |
|
|
|
- buttons |
|
|
|
- +5V -> [1k ohm] -> common connection to buttons |
|
|
|
- D2 -> button 1 |
|
|
|
-> [1M ohm] -> GND |
|
|
|
- D3 -> button 2 |
|
|
|
-> [1M ohm] -> GND |
|
|
|
- D4 -> button 3 |
|
|
|
-> [1M ohm] -> GND |
|
|
|
- D5 -> button 4 |
|
|
|
-> [1M ohm] -> GND |
|
|
|
(or drop the resistors completely and use the internal pull-down resistors) |
|
|
|
*/ |
|
|
|
|
|
|
|
#include <DebounceEvent.h> |
|
|
|
|
|
|
|
const int btns = 4; |
|
|
|
const int SWITCH_DEBOUNCE_DELAY = 500; |
|
|
|
DebounceEvent switches[btns] = { |
|
|
|
DebounceEvent(2, BUTTON_SWITCH, SWITCH_DEBOUNCE_DELAY), |
|
|
|
DebounceEvent(3, BUTTON_SWITCH, SWITCH_DEBOUNCE_DELAY), |
|
|
|
DebounceEvent(4, BUTTON_SWITCH, SWITCH_DEBOUNCE_DELAY), |
|
|
|
DebounceEvent(5, BUTTON_SWITCH, SWITCH_DEBOUNCE_DELAY) |
|
|
|
}; |
|
|
|
|
|
|
|
const int volumePin = A0; |
|
|
|
const int volumeRawMin = 0; |
|
|
|
const int volumeRawMax = 850; |
|
|
|
const int volumeStep = 5; |
|
|
|
int volume = -1; |
|
|
|
|
|
|
|
// === Switches === |
|
|
|
|
|
|
|
void switchesSetup() { |
|
|
|
for (int i = 0; i < btns; i++) { |
|
|
|
sendButtonEvent(i+1, switches[i].pressed()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void switchesLoop() { |
|
|
|
for (int i = 0; i < btns; i++) { |
|
|
|
int event = switches[i].loop(); |
|
|
|
if (event != EVENT_NONE) { |
|
|
|
sendButtonEvent(i+1, switches[i].pressed()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// === Volume === |
|
|
|
|
|
|
|
void volumeInit() { |
|
|
|
pinMode(volumePin, INPUT); |
|
|
|
volume = volumeRead(); |
|
|
|
sendVolumeEvent(); |
|
|
|
} |
|
|
|
|
|
|
|
int volumeRead() { |
|
|
|
int volumeRaw = analogRead(volumePin); |
|
|
|
return map(volumeRaw, volumeRawMin, volumeRawMax, 0, 255); |
|
|
|
} |
|
|
|
|
|
|
|
void volumeLoop() { |
|
|
|
int newVolume = volumeRead(); |
|
|
|
if (abs(volume - newVolume) >= volumeStep) { |
|
|
|
volume = newVolume; |
|
|
|
sendVolumeEvent(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// === Serial Events === |
|
|
|
|
|
|
|
void sendEvent(char *name, int value) { |
|
|
|
Serial.print(name); |
|
|
|
Serial.print("="); |
|
|
|
Serial.println(value); |
|
|
|
} |
|
|
|
|
|
|
|
void sendVolumeEvent() { |
|
|
|
sendEvent("volume", volume); |
|
|
|
} |
|
|
|
|
|
|
|
void sendButtonEvent(int buttonNumber, int state) { |
|
|
|
String name = "button"; |
|
|
|
name.concat(buttonNumber); |
|
|
|
sendEvent(name.c_str(), state); |
|
|
|
} |
|
|
|
|
|
|
|
// === MAIN === |
|
|
|
|
|
|
|
void setup() { |
|
|
|
Serial.begin(9600); |
|
|
|
switchesSetup(); |
|
|
|
volumeInit(); |
|
|
|
} |
|
|
|
|
|
|
|
void loop() { |
|
|
|
switchesLoop(); |
|
|
|
volumeLoop(); |
|
|
|
delay(100); |
|
|
|
} |