Hi all,
I'm currently in the testing phase of a digitally controlled analog system and I am using LEDs to get a proof of concept.
to start with I need x about of LEDs each having a dedicated button to switch the LED state between on and off. I am able to do this successfully with a single LED and button and it should be easy enough to copy and paste my code to add more, but that is going to get messy pretty fast.
I have made a small amount of progress and figured out how to easily assign multiple pins as inputs and outputs. Here is my current code with one button controlling multiple LEDs.
int ledPins[] = {
8, 9, 10, 11, 12, 13
}; // an array of pin numbers to which LEDs are attached
int pinCount = 6; // the number of pins (i.e. the length of the array)
int Button = 2;
int LedState = LOW;
int ButtonCurrent;
int ButtonPrev = HIGH; // has to be opposite of led.
void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
pinMode(Button, INPUT_PULLUP);
}
void loop() {
ButtonCurrent = digitalRead (Button); // Read current button state.
if(ButtonCurrent == HIGH && ButtonPrev == LOW) // if statement starts when button is pressed. and changes led state to the opposit of its current state.
{
if (LedState == HIGH)
{
LedState = LOW;
}
else
{
LedState = HIGH;
}
}
for (int thisPin = 0; thisPin < pinCount; thisPin++) { // apply LedState to all LED OUTPUTS.
digitalWrite(ledPins[thisPin], LedState);
}
ButtonPrev = ButtonCurrent;
}
I can just as easily assign multiple pins as outputs, but I can not figure out how to approach having button1 switch only LED1, etc.
Once I figure that out, the actual end goal is to be able to save and load different LED arrangements as presets.
Any input is much appreciated.