commit 8089bde9aa40da6b7e691d9ead1c32d4395d03ab Author: Dejvino Date: Sun Apr 4 22:16:01 2021 +0200 Initial commit diff --git a/FestivalController.ino b/FestivalController.ino new file mode 100644 index 0000000..f4d7dc5 --- /dev/null +++ b/FestivalController.ino @@ -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 + +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); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..53a533e --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Festival Controller + +Arduino code for interacting with a 50s radio (Tesla Festival in my case, hence the name). Radio controls are read and reported as events over serial to a computer. + +This controller is a part of an Internet radio conversion, providing inputs to an SBC that streams music based on the inputs. + + +## Hardware +- volume potentiometer +- 4 button switches array (exclusive mode) + +Connections are simple, see code for details. +