Button reset code

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;
  }
}

What exactly do you mean by this ?

sorry wasn't clear the purpose of the code is its a game each player picks a colour and confirms it with the button and after the 4th press the chosen colours create a colour display and im trying to make the 5th button press clear the colour display and chosen colours so the game can continue and they can pick another colour, so basically the code starts again

Describe what the code does and explain why that is not correct.

basically its a game each player takes it in turns to mix a colour and then confirm it with the press of the button once the 4th player presses the button it creates a colour display with the colours they have chosen and after the button is pressed a 5th time the game should restart so players can pick their colours again and create a new display but when it is pressed a 5th time the display just continues instead of clearing to allow for the next 4 colours to be chosen

You are repeating yourself, so I will repeat myself.

Explain the problem, not the goal.

This all sounds very familiar. Why have you started another topic on the same subject ?

when the the saved colours have been chosen and are displayed on the neopixel when the button is pressed again to clear the pixels it just doesnt do anything , i added a counter so it counts the button presses and after 5 or more it should clear the pixels but it doesn't

Clear the pixels here:

  resetCounter++;
  if (resetCounter >5) {
    currentColor = 0;
    colorsSaved = false;
    resetCounter = 0;
  }
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.