/*
The UI will have 3 buttons and 5 actions:
B0 = Reset Stations, B1 = Channel UP, B2 = Volume
Outputs: [0] = Enter, [1] = UP, [2] = Presets, [3] = Volume UP, [4] = Volume DOWN
Action_0: Program up - UP, Enter
Action_1: BAck to preset 1 - Presets, Enter
Action_2: Mute - Volume down 19 times
Action_3: Default Volume - Volume up 13 times, Enter, Volume up
*/
#include <Arduino.h>
// Define the pin numbers for the pushbuttons and LEDs
const int buttonPin[] = {2, 3, 4}; // Change these to the pin numbers where your buttons are connected
const int ledPin[] = {7, 8, 9, 10, 11}; // Change these to the pin numbers where your LEDs are connected
const int led = 13; // Onboard LED
// Define other constants
const int CmdDelay = 150; // Delay between button presses
const int ButtonDur = 100; // Duration of button press
const int LoopDelay = 500; // Delay between loops to prevent errors due to trembling hand
// Variables will change:
uint8_t buttonState[] = {HIGH, HIGH, HIGH, HIGH}; // the current reading from the input pin
uint8_t lastButtonState[] = {HIGH, HIGH, HIGH, HIGH}; // the previous reading from the input pin
uint8_t reading[] = {HIGH, HIGH, HIGH, HIGH}; // the current reading from the input pin
bool muted = false; // Volume Mute status
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 25; // the debounce time; increase if the output flickers
// function declarations
void Action0(); // Program up
void Action1(); // Preset 1
void Action2(); // Volume Mute
void Action3(); // Volume to default level
void setup() {
// Set the pin modes
for (int i = 0; i < 3; i++) {
pinMode(buttonPin[i], INPUT_PULLUP);
}
for(int i = 0; i < 5; i++) {
pinMode(ledPin[i], OUTPUT);
digitalWrite(ledPin[i], HIGH);
}
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
delay(5000); // Wait for 5 seconds for the DAB Radio to boot up
// Perform the initial actions
Action2();
delay(LoopDelay);
Action3();
}
void loop() {
for (int i = 0; i < 3; i++) {
// read the state of the switch into a local variable:
reading[i] = digitalRead(buttonPin[i]);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading[i] != lastButtonState[i]) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading[i] != buttonState[i]) {
buttonState[i] = reading[i];
// only call the Action if the new button state is HIGH
if (buttonState[i] == HIGH) {
// ledState[i] = !ledState[i];
switch (i) {
case 0:
Action0();
break;
case 1:
Action1();
break;
case 2:
if (muted) {
Action3();
} else {
Action2();
}
break;
//default:
//Do nothing
//break;
}
delay(LoopDelay); // Delay to prevent multiple presses
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState[i] = reading[i];
}
}
// My functions
void Action0() { // Program up
digitalWrite(led, HIGH); // Turn on onboard LED
for (int ii = 0; ii < 2; ii++) { //Press Program up twice
digitalWrite(ledPin[1], LOW);
delay(ButtonDur);
digitalWrite(ledPin[1], HIGH);
delay(CmdDelay);
}
digitalWrite(ledPin[0], LOW); // Press Enter
delay(ButtonDur);
digitalWrite(ledPin[0], HIGH);
digitalWrite(led, LOW); // Turn off onboard LED
}
void Action1() { // Preset 1
digitalWrite(led, HIGH); // Turn on onboard LED
digitalWrite(ledPin[2], LOW); // Press Presets
delay(ButtonDur);
digitalWrite(ledPin[2], HIGH);
delay(CmdDelay);
digitalWrite(ledPin[0], LOW); // Press Enter
delay(ButtonDur);
digitalWrite(ledPin[0], HIGH);
digitalWrite(led, LOW); // Turn off onboard LED
}
void Action2() { // Volume Mute = 19x V-
digitalWrite(led, HIGH); // Turn on onboard LED
for (int ii = 0; ii < 19; ii++) { //Press Volume Down 19 times
digitalWrite(ledPin[4], LOW);
delay(ButtonDur);
digitalWrite(ledPin[4], HIGH);
delay(CmdDelay);
}
muted = true; // Volume is muted
digitalWrite(led, LOW); // Turn off onboard LED
}
void Action3() { // Volume to default level = 13x V+ + Enter + V+
digitalWrite(led, HIGH); // Turn on onboard LED
for (int ii = 0; ii < 13; ii++) { //Press Volume Up 13 times
digitalWrite(ledPin[3], LOW);
delay(ButtonDur);
digitalWrite(ledPin[3], HIGH);
delay(CmdDelay);
}
digitalWrite(ledPin[0], LOW); // Press Enter
delay(ButtonDur);
digitalWrite(ledPin[0], HIGH);
delay(CmdDelay);
digitalWrite(ledPin[3], LOW); // Press Volume Up
delay(ButtonDur);
digitalWrite(ledPin[3], HIGH);
muted = false; // Volume is not muted
digitalWrite(led, LOW); // Turn off onboard LED
}