Hi,
I'm building a foot controller to select presets on a DSP based audio processor. The interface is quite simple; pull a pin low to select that preset. The manufacturer even provides +5VDC and ground for powering external circuitry.
My first sketch just interlocked 3 buttons; press one button and it turns the other outputs off and the selected one on. It works fine, but you can't turn all of the selections off; one always remains on.
I then tried to add a function to clear a selection by looking at the state of the output; if an output is already on and it's button is pressed again, turn it off.
Unfortunately, it doesn't work and I'm mystified as to why it doesn't work. I can't get any of the outputs to turn on. What am I missing? Thanks in advance.
// interlocked_buttons_2
// machambers 9/12/2021
// Arduino Uno & SparkFun MIDI Shield
// HIGH = Off, LOW = On
// in version 1 interlocks work, but you can't turn all selections off
// Constants won't change:
const int button1 = 2; // the pin that the pushbutton is attached to
const int led1 = 5; // the pin that the LED is attached to
const int button2 = 3;
const int led2 = 6;
const int button3 = 4;
const int led3 = 7;
void setup() {
// initialize the button pins as inputs:
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
// initialize the LED pins as outputs:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
}
void loop() {
if (digitalRead(button1) == LOW && led1 == HIGH) {
// if the current state is LOW then the button went from off to on
// if led1 is HIGH it is not enabled; turn off led2 & led3 then enable led1
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led1, LOW);
// below added to turn selection off if it's already on
} else if (digitalRead(button1) == LOW && led1 == LOW) {
// if led1 is low it is enabled; disable it
digitalWrite(led1, HIGH);
} while (digitalRead(button1) == LOW);
delay(50);
if (digitalRead(button2) == LOW && led2 == HIGH) {
digitalWrite(led1, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
} else if (digitalRead(button2) == LOW && led2 == LOW) {
digitalWrite(led2, HIGH);
} while (digitalRead(button2) == LOW);
delay(50);
if (digitalRead(button3) == LOW && led3 == HIGH) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
} else if (digitalRead(button3) == LOW && led3 == LOW) {
digitalWrite(led3, HIGH);
} while (digitalRead(button3) == LOW);
delay(50);
}