Im trying to get the button to reset the code back to the start after the 5th press but it wont seem to work any advice
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 12
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int redPin = A0;
int greenPin = A1;
int bluePin = A2;
int buttonPin = 2;
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
int buttonState = 0;
int lastButtonState = 0;
int currentColor = 0;
int savedColors[4][3];
bool colorsSaved = false;
int resetCounter = 0;
void setup() {
pixels.begin();
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
if (!colorsSaved) {
// Read the potentiometer values and map them to the range 0-255
redValue = map(analogRead(redPin), 0, 1023, 0, 255);
greenValue = map(analogRead(greenPin), 0, 1023, 0, 255);
blueValue = map(analogRead(bluePin), 0, 1023, 0, 255);
// Set the RGB LED to the selected color
pixels.setPixelColor(0, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(1, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(2, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(3, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(4, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(5, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(6, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(7, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(8, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(9, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(10, pixels.Color(redValue, greenValue, blueValue));
pixels.setPixelColor(11, pixels.Color(redValue, greenValue, blueValue));
pixels.show();
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if the button was pressed and released
if (buttonState == LOW && lastButtonState == HIGH) {
// Save the current color
savedColors[currentColor][0] = redValue;
savedColors[currentColor][1] = greenValue;
savedColors[currentColor][2] = blueValue;
currentColor++;
// Check if we've saved four colors
if (currentColor == 4) {
colorsSaved = true;
currentColor = 0;
// Turn off the first 4 pixels
for (int i = 0; i < 12; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
delay(100);
}
}
}
lastButtonState = buttonState;
} else {
// Cycle through the saved colors on all 12 neopixels
for (int i = 0; i < 2; i++) {
for (int j = 0; j < NUMPIXELS; j++) { // turn on/off each pixel one by one
pixels.setPixelColor(j, pixels.Color(savedColors[currentColor][0], savedColors[currentColor][1], savedColors[currentColor][2]));
pixels.show();
delay(250);
currentColor = (currentColor + 1) % 4;
}
for (int j = 0; j < NUMPIXELS; j++) {
pixels.setPixelColor(j, pixels.Color(0, 0, 0));
pixels.show();
delay(250);
}
}
// Check if the button was pressed to reset the code
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
resetCounter++;
if (resetCounter >5) {
currentColor = 0;
colorsSaved = false;
resetCounter = 0;
}
}
lastButtonState = buttonState;
}
}