I am trying to make a machine where a neopixel lights up a specific colour depending on which button has been pressed however currently the neopixel flashes the right colour then the light goes either off or white light.
I am very much a beginner so any help would be great, thank you.
#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define BUTTON_PIN1 2
#define BUTTON_PIN2 10
#define BUTTON_PIN3 11
#define PIXEL_PIN 6 // Digital pin 6 connected to the NeoPixels.
#define PIXEL_COUNT 13 // Number of NeoPixels
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); // Declare our NeoPixel strip object:
boolean oldState = HIGH;
boolean newState = HIGH;
boolean toggle = LOW;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN1, INPUT_PULLUP);
pinMode(BUTTON_PIN2, INPUT_PULLUP);
pinMode(BUTTON_PIN3, INPUT_PULLUP);
strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
updateButtonState1();
updateButtonState2();
updateButtonState3();
// debugButtonState();
}
void updateButtonState1() {
// Get current button state.
newState = digitalRead(BUTTON_PIN1);
if ((newState == LOW) && (oldState == HIGH)) {
delay(50);
newState = digitalRead(BUTTON_PIN1);
if (newState == LOW) {
toggle = !toggle;
}
}
oldState = newState;
if (toggle == HIGH) {
strip.setPixelColor(2, 245, 15, 15); // (red)
} else {
strip.setPixelColor(2, 0, 0, 0);
}
strip.show(); // Update strip with new contents
}
void updateButtonState2() {
// Get current button state.
newState = digitalRead(BUTTON_PIN2);
if ((newState == LOW) && (oldState == HIGH)) {
delay(50);
newState = digitalRead(BUTTON_PIN2);
if (newState == LOW) {
toggle = !toggle;
}
}
oldState = newState;
if (toggle == HIGH) {
strip.setPixelColor(2, 13, 50, 190); // (blue)
} else {
strip.setPixelColor(2, 0, 0, 0);
}
strip.show(); // Update strip with new contents
}
void updateButtonState3() {
// Get current button state.
newState = digitalRead(BUTTON_PIN3);
if ((newState == LOW) && (oldState == HIGH)) {
delay(50);
newState = digitalRead(BUTTON_PIN3);
if (newState == LOW) {
toggle = !toggle;
}
}
oldState = newState;
if (toggle == HIGH) {
strip.setPixelColor(2, 58, 233, 65); // (green)
} else {
strip.setPixelColor(2, 0, 0, 0);
}
strip.show(); // Update strip with new contents
}