Reseting counter at wrong comand in joystick

hello guys, I'm trying to make the led stay white when a certain sequence is done on the JOYSTICK, but when it was the wrong command he reset the counter, making him need to do the sequence again!

const int r1 = 31;
const int g1 = 32;
const int b1 = 33;


int buttonState = 0;
int countJoy = 0;

int eixo_X = A0; //PINO REFERENTE A LIGAÇÃO DO EIXO X
int eixo_Y = A1; //PINO REFERENTE A LIGAÇÃO DO EIXO Y
const int botao = 30; //PINO REFERENTE A LIGAÇÃO NO PINO KEY (EIXO Z)

unsigned long espera3 = 0;

void setup() {
    Serial.begin(9600);
  pinMode(r1, OUTPUT);
  pinMode(g1, OUTPUT);
  pinMode(b1, OUTPUT);
  pinMode(botao, OUTPUT);
}

void loop() {
joystickColor();
    buttonState = digitalRead(botao);
    Serial.print(countJoy);
Serial.println("\n");
}
  
void joystickColor() {
  if (countJoy == 5) {
    white1();
  }
if ((millis() - espera3) > 500) {

if (buttonState == LOW)  // light the LED
{
  black1();
espera3 = millis();
}

if (analogRead(eixo_X) <= 50) // light the LED
{
  blue1();
  if (countJoy == 1) {
  countJoy++;
}
if (countJoy == 3) {
  countJoy++;
}
}

if (analogRead(eixo_Y) <= 50) // light the LED
{
  green1();
if (countJoy == 0){
countJoy++;
}
if (countJoy == 2) {
  countJoy++;
}
espera3 = millis();
}

if (analogRead(eixo_X) >= 1000) // light the LED
{
red1();
if (countJoy == 4) {
  countJoy++;
}
espera3 = millis();
}

if (analogRead(eixo_Y) >= 1000) // light the LED
{
  countJoy = 0;
espera3 = millis();
}
}
}

void red1()
{
digitalWrite(r1, HIGH);
digitalWrite(g1, LOW);
digitalWrite(b1, LOW);
}
void green1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, HIGH);
digitalWrite(b1, LOW);
}
void blue1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, LOW);
digitalWrite(b1, HIGH);
}
void white1()
{
digitalWrite(r1, HIGH);
digitalWrite(g1, HIGH);
digitalWrite(b1, HIGH);
}
void black1()
{
digitalWrite(r1, LOW);
digitalWrite(g1, LOW);
digitalWrite(b1, LOW);
}

i'm not sure i understand how your code works. but when properly indented, it's obvious that it's only reading the joystick when 500 msec has expired. i don't think that's intended

const int r1 = 31;
const int g1 = 32;
const int b1 = 33;
int buttonState = 0;
int countJoy = 0;
int eixo_X = A0;
//PINO REFERENTE A LIGAÇÃO DO EIXO X
int eixo_Y = A1;
//PINO REFERENTE A LIGAÇÃO DO EIXO Y
const int botao = 30;
//PINO REFERENTE A LIGAÇÃO NO PINO KEY (EIXO Z)
unsigned long espera3 = 0;
void setup() {
    Serial.begin(9600);
    pinMode(r1, OUTPUT);
    pinMode(g1, OUTPUT);
    pinMode(b1, OUTPUT);
    pinMode(botao, OUTPUT);
}

void loop() {
    joystickColor();
    buttonState = digitalRead(botao);
    Serial.print(countJoy);
    Serial.println("\n");
}

void joystickColor() {
    if (countJoy == 5) {
        white1();
    }

    if ((millis() - espera3) > 500) {
        if (buttonState == LOW)  // light the LED
        {
            black1();
            espera3 = millis();
        }

        if (analogRead(eixo_X) <= 50) // light the LED
        {
            blue1();
            if (countJoy == 1) {
                countJoy++;
            }

            if (countJoy == 3) {
                countJoy++;
            }

        }

        if (analogRead(eixo_Y) <= 50) // light the LED
        {
            green1();
            if (countJoy == 0){
                countJoy++;
            }

            if (countJoy == 2) {
                countJoy++;
            }

            espera3 = millis();
        }

        if (analogRead(eixo_X) >= 1000) // light the LED
        {
            red1();
            if (countJoy == 4) {
                countJoy++;
            }

            espera3 = millis();
        }

        if (analogRead(eixo_Y) >= 1000) // light the LED
        {
            countJoy = 0;
            espera3 = millis();
        }

    }

}

void red1()
{
    digitalWrite(r1, HIGH);
    digitalWrite(g1, LOW);
    digitalWrite(b1, LOW);
}

void green1()
{
    digitalWrite(r1, LOW);
    digitalWrite(g1, HIGH);
    digitalWrite(b1, LOW);
}

void blue1()
{
    digitalWrite(r1, LOW);
    digitalWrite(g1, LOW);
    digitalWrite(b1, HIGH);
}

void white1()
{
    digitalWrite(r1, HIGH);
    digitalWrite(g1, HIGH);
    digitalWrite(b1, HIGH);
}

void black1()
{
    digitalWrite(r1, LOW);
    digitalWrite(g1, LOW);
    digitalWrite(b1, LOW);
}

I wanna reset count If wrong command

I wanna reset count If wrong command

you said that in your original ports

maybe i misunderstand how your code works.

there's a condition in your code that prevents reading the joysticks for 5 seconds. what is it intended to do?

500 ms isn't half Seconds? This time to get led off... This working in a half Seconds tô me

Ex: If a joystick get comando to up, he get a color.. another color If left

And If get left UP UP left right... Write led white

Sry about my english

not sure what your trying to do

consider

#if 0
const int r1 = 31;
const int g1 = 32;
const int b1 = 33;
#else
const int r1 = 10;
const int g1 = 11;
const int b1 = 12;
#endif
int buttonState = 0;

int countJoy = 0;
int eixo_X = A0;
//PINO REFERENTE A LIGAÇÃO DO EIXO X
int eixo_Y = A1;
//PINO REFERENTE A LIGAÇÃO DO EIXO Y
const int botao = 30;
//PINO REFERENTE A LIGAÇÃO NO PINO KEY (EIXO Z)
unsigned long espera3 = 0;

// -----------------------------------------------------------------------------
byte butPins [] = { A1, A2, A3 };
byte ledPins [] = { 10, 11, 12 };

#define N_PINS sizeof(butPins)

byte butLst   [N_PINS] = {};
byte butPress [N_PINS] = {};

char s [60];

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin(9600);
    pinMode(r1, OUTPUT);
    pinMode(g1, OUTPUT);
    pinMode(b1, OUTPUT);
    pinMode(botao, OUTPUT);

    butSetup ();
}

// -----------------------------------------------------------------------------
void loop() {
#if 0
    joystickColor();
    buttonState = digitalRead(botao);
    Serial.print(countJoy);
    Serial.println("\n");
#else
    func ();
#endif
}

// -----------------------------------------------------------------------------
void func (void)
{
    static unsigned long usecLst = 0;
           unsigned long usec    = millis ();

    bool e [N_PINS] = {};

    chkButs ();

#if 0
    if (analogRead(eixo_X) <= 50)
        e [0] = 1;
    else if (analogRead(eixo_Y) <= 50)
        e [1] = 1;
    else if (analogRead(eixo_X) >= 1000)
        e [2] = 1;
#else
    for (unsigned int i = 0; i < N_PINS; i++)  {
        e [i]        = butPress [i];
        butPress [i] = 0;
    }
#endif

    if (e [0] | e [1] | e [2] )  {
        sprintf (s, "%s: %d, %d %d %d", __func__, 
            countJoy, e [0], e [1], e [2]);
        Serial.println (s);
    }

    switch (countJoy)  {
    case 0:
        red1 ();
        if (e [0])  {
            countJoy++;
            usecLst = usec;
        }
        break;

    case 1:
        green1 ();
        if (e [1])  {
            countJoy++;
            usecLst = usec;
        }
        break;

    case 2:
        red1 ();
        if (e [0])  {
            countJoy++;
            usecLst = usec;
        }
        break;

    case 3:
        green1 ();
        if (e [1])  {
            countJoy++;
            usecLst = usec;
        }
        break;

    case 4:
        blue1 ();
        if (e [2])  {
            countJoy++;
            usecLst = usec;
        }
        break;

    default:
        white1 ();
        break;
    }

    if (usec - usecLst > 1000)  {
        black1 ();
        delay (2000);
        usecLst  = millis ();
        countJoy = 0;
    }
}

// -----------------------------------------------------------------------------
void joystickColor() {
    if (countJoy == 5) {
        white1();
    }

    if ((millis() - espera3) > 500) {
        if (buttonState == LOW)  // light the LED
        {
            black1();
            espera3 = millis();
        }

        if (analogRead(eixo_X) <= 50) // light the LED
        {
            blue1();
            if (countJoy == 1) {
                countJoy++;
            }

            if (countJoy == 3) {
                countJoy++;
            }

        }

        if (analogRead(eixo_Y) <= 50) // light the LED
        {
            green1();
            if (countJoy == 0){
                countJoy++;
            }

            if (countJoy == 2) {
                countJoy++;
            }

            espera3 = millis();
        }

        if (analogRead(eixo_X) >= 1000) // light the LED
        {
            red1();
            if (countJoy == 4) {
                countJoy++;
            }

            espera3 = millis();
        }

        if (analogRead(eixo_Y) >= 1000) // light the LED
        {
            countJoy = 0;
            espera3 = millis();
        }

    }

}

void red1()
{
    digitalWrite(r1, HIGH);
    digitalWrite(g1, LOW);
    digitalWrite(b1, LOW);
}

void green1()
{
    digitalWrite(r1, LOW);
    digitalWrite(g1, HIGH);
    digitalWrite(b1, LOW);
}

void blue1()
{
    digitalWrite(r1, LOW);
    digitalWrite(g1, LOW);
    digitalWrite(b1, HIGH);
}

void white1()
{
    digitalWrite(r1, HIGH);
    digitalWrite(g1, HIGH);
    digitalWrite(b1, HIGH);
}

void black1()
{
    digitalWrite(r1, LOW);
    digitalWrite(g1, LOW);
    digitalWrite(b1, LOW);
}

// -----------------------------------------------------------------------------
// recognize multiple button presses; tgl LED when pressed
void butSetup (void)
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        digitalWrite (ledPins [n], HIGH);
        pinMode      (ledPins [n], OUTPUT);

        pinMode      (butPins [n], INPUT_PULLUP);
        butLst [n]  = digitalRead (butPins [n]);
    }
}

// -----------------------------------------------------------------------------
void chkButs (void)
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        byte but = digitalRead (butPins [n]);

        if (butLst [n] != but)  {
            butLst [n] = but;

            if (LOW == but)   {    // button pressed
                butPress [n] = 1;
                sprintf (s, "%s: %d", __func__, n);
                Serial.println (s);
            }
        }
    }

    delay (10);         // debounce
}
pinMode(botao, OUTPUT);

buttonState = digitalRead(botao);

I am curious about the above. Shouldn't a button, that you read the state of, be an input? Does the button pin need a pullup or pulldown?