I have a set of buttons and a set of NeoPixel LEDs. There's lots of very similar constants (button1pin, button2pin, etc) so there's almost certainly a more efficient way of coding this.
Any ideas?
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(9, 2, NEO_RGBW + NEO_KHZ800);
// Buttons
const int button1pin = 3;
const int button2pin = 4;
const int button3pin = 5;
const int button4pin = 6;
const int button5pin = 7;
const int button6pin = 8;
const int button7pin = 9;
const int button8pin = 10;
const int button9pin = 11;
const int led1 = 0;
const int led2 = 1;
const int led3 = 2;
const int led4 = 3;
const int led5 = 4;
const int led6 = 5;
const int led7 = 6;
const int led8 = 7;
const int led9 = 8;
void setup() {
strip.begin();
strip.show();
pinMode(button1pin, INPUT_PULLUP);
pinMode(button2pin, INPUT_PULLUP);
pinMode(button3pin, INPUT_PULLUP);
pinMode(button4pin, INPUT_PULLUP);
pinMode(button5pin, INPUT_PULLUP);
pinMode(button6pin, INPUT_PULLUP);
pinMode(button7pin, INPUT_PULLUP);
pinMode(button8pin, INPUT_PULLUP);
pinMode(button9pin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(button1pin) == LOW){
strip.setPixelColor(led1, strip.Color(20, 55, 0, 0));
}
if (digitalRead(button2pin) == LOW){
strip.setPixelColor(led2, strip.Color(20, 55, 0, 0));
}
if (digitalRead(button3pin) == LOW){
strip.setPixelColor(led3, strip.Color(20, 55, 0, 0));
}
if (digitalRead(button4pin) == LOW){
strip.setPixelColor(led4, strip.Color(20, 55, 0, 0));
}
if (digitalRead(button5pin) == LOW){
strip.setPixelColor(led5, strip.Color(20, 55, 0, 0));
}
if (digitalRead(button6pin) == LOW){
strip.setPixelColor(led6, strip.Color(20, 55, 0, 0));
}
if (digitalRead(button7pin) == LOW){
strip.setPixelColor(led7, strip.Color(20, 55, 0, 0));
}
if (digitalRead(button8pin) == LOW){
strip.setPixelColor(led8, strip.Color(20, 55, 0, 0));
}
if (digitalRead(button9pin) == LOW){
strip.setPixelColor(led9, strip.Color(20, 55, 0, 0));
}
strip.show();
}
int buttons[9]={3,4,5,6,7,8,9,10,11}; int leds[9]={0,1,2,3,4,5,6,7,8,9};...