I am having some issues getting my code to work properly on one of the 5 buttons.
Some background on what I am trying to do and where this will be going.
I have 5 individual momentary push buttons that are used as 5 separate toggle switches. The code is an adaptation of the state change detection example. Additionally, I have 5 NeoPixels that change their color corresponding to which button has been toggled.
Eventually, once I have the code hammered out, I will be using capacitive touch instead of a physical button, but I am working out the bugs with physical components first.
I get buttons 1 through 4 to work properly, but I get some crazy values on button 5, jumping around from large negative to large positive values on the Serial Monitor. I see values as expected on buttons 1 through 4, but button 5 is not working as it should and I can't seem to figure out why.
The code is below and the attached photo shows how everything is connected. Any help would be greatly appreciated!
#include <Adafruit_NeoPixel.h>
////////////////////Constants////////////////////
#define LEDs 8
#define LEDCount 5
#define LDR A0
int AuxButton[] = {2, 3, 4, 5, 6}; //Array To Define Auxillary Buttons
////////////////////Variables////////////////////
int AuxButtonCounter[5]; //Array To Hold Button Count
int AuxButtonState[5]; //Array To Hold Button State
int AuxLastButtonState[5]; //Array To Hold Last Button State
int AuxLED[5]; //Defines which LED to change color
///Used for smoothing of LDR readings///
const int numReadings = 10;
int LDRReadings[numReadings];
int LDRIndex = 0;
int LDRTotal = 0;
int LDRAverage = 0;
int LightCorrection = 0;
unsigned long previousMillis = 0;
const long LDRDelay = 200;
////////////////////Define LEDs////////////////////
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCount, LEDs, NEO_GRB + NEO_KHZ800);
////////////////////Script to Run on Start-Up////////////////////
void setup() {
pinMode(LDR, INPUT);
int i;
for (i = 0; i < 5; i++){
pinMode(AuxButton[i], INPUT); //Initialize Auxillary Buttons
}
strip.begin();
strip.show();
Serial.begin(9600); //Initialize Serial Communication For Debugging
colorWipe(strip.Color(200, 0, 0), 100); // Red
colorWipe(strip.Color(0, 200, 0), 100); // Green
colorWipe(strip.Color(0, 0, 200), 100); // Blue
}
void loop() {
//////////Light Correction//////////
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= LDRDelay){
previousMillis = currentMillis;
LDRTotal = LDRTotal - LDRReadings[LDRIndex];
LDRReadings[LDRIndex] = analogRead(LDR);
LDRTotal = LDRTotal + LDRReadings[LDRIndex];
LDRIndex = LDRIndex + 1;
if(LDRIndex >= numReadings){
LDRIndex = 0;
}
LDRAverage = LDRTotal/numReadings;
if(LDRAverage < 400){
LDRAverage = 400;
}
// Serial.println(LDRAverage);
}
LightCorrection = map(LDRAverage,400,1000,50,255);
////////////////////
//////////Button State Change Detection//////////
strip.show();
int i;
for (i = 0; i < 5; i++){
AuxButtonState[i] = digitalRead(AuxButton[i]);
if(AuxButtonState[i]>0){
AuxButtonState[i] == 1;
}
else{
AuxButtonState[i] == 0;
}
if (AuxButtonState[i] != AuxLastButtonState[i]) {
if (AuxButtonState[i] == HIGH) {
AuxButtonCounter[i]++;
}
}
delay(10); //Used for debouncing
}
for (i = 0; i <5; i++){
AuxLastButtonState[i] = AuxButtonState[i];
}
for (i = 0; i < 5; i++){
if (AuxButtonCounter[i] % 2-1 == 0) {
digitalWrite(AuxLED[i], HIGH);
strip.setPixelColor(i,LightCorrection,0,0);
strip.show();
} else {
digitalWrite(AuxLED[i], LOW);
strip.setPixelColor(i,0,0,LightCorrection);
strip.show();
}
}
Serial.print("Button 1 ");
Serial.println(AuxButtonCounter[1]);
Serial.print("Button 2 ");
Serial.println(AuxButtonCounter[2]);
Serial.print("Button 3 ");
Serial.println(AuxButtonCounter[3]);
Serial.print("Button 4 ");
Serial.println(AuxButtonCounter[4]);
Serial.print("Button 5 ");
Serial.println(AuxButtonCounter[5]);
delay(500); //Used to troubleshoot button, will be omitted from final script
////////////////////
}
//////////Startup Color Wipe//////////
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
////////////////////