4 leds and 3 buttons issue

Hi

I've made a board with UNO that is basically 3 buttons and 4 leds, as 3 of them activated by pressing the corresponding button. The fourth led is activated by pressing twice any button (counter >> 1).
I don't understand why but pressing three or four times (alternatively), one button after the other it activates the fourth led . I guess it should not doing that...
You can see the schem and the code below. If anybody guessing what's happened...

const int push_A = 7;
const int push_B = 4;
const int push_C = 2;
const int pushButtons[] = {push_A,push_B,push_C};
const int led_1 = 14;
const int led_2 = 15;
const int led_3 = 16;
int leds[] = {led_1,led_2,led_3};
int buttonPushCounter[] = {0,0,0};   // counter for the number of button presses
int buttonState[3];         // current state of the button

/**********************************************************************/
void setup()
{
  pinMode(push_A, INPUT_PULLUP);
  pinMode(push_B, INPUT_PULLUP);
  pinMode(push_C, INPUT_PULLUP);
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
  pinMode(led_3, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}
/**********************************************************************/

void resetAllLeds()
{
  for(int i=0; i<3; i++)
  {
    digitalWrite(leds[i], LOW);
   }
  digitalWrite(13, LOW);
}
/**********************************************************************/
void resetCounter()
{
  for(int j=0; j<3; j++)
  {
    buttonPushCounter[j]= 0;
  }
}  
/**********************************************************************/
void loop() //ok
{
  for(int i=0; i<3; i++)
  {
    buttonState[i] = digitalRead(pushButtons[i]); 
    switch (buttonState[i]) 
    {
      case 1:
        NULL;
        break;
      case 0:
        buttonPushCounter[i]++;
        resetAllLeds();
        digitalWrite(leds[i], HIGH);
        delay(210);
        if(buttonPushCounter[i] > 1)
          {
            resetCounter();
            resetAllLeds();
            digitalWrite(13, HIGH);
           }
        break;
      }
  }
}


You should not share a series resistor between LEDs like that. They should have one each.

if(buttonPushCounter[i] >> 1)

I think you meant to use ">" not ">>".

Thank sir.

One resistor is ok as no more than one led lights at the same time.
I tried with ">" but same problem...

carranen:
One resistor is ok as no more than one led lights at the same time.

Correct, this is a situation where only one series resistor is needed. It was not clear from your description that this would be the case.

I think the problem you describe may be because your code does not detect when a button becomes pressed, only when it is pressed. In other words it does not detect the button changing state. There is an example sketch in the IDE menu which demonstrates this principal, called "state change" or similar.

const int push_A = 7;
const int push_B = 4;
const int push_C = 2;
const int pushButtons[] = {push_A,push_B,push_C};
const int led_1 = 14;
const int led_2 = 15;
const int led_3 = 16;
int leds[] = {led_1,led_2,led_3};

Uhhhhh....

const byte PushButtonPins[] {7, 4, 2};
const byte LedPins[] = {14, 15, 16};

double posting is bad.
Please don’t.

In this case it is passably forgivable as it is reasonably unlikely that the same people will or indeed can view both threads! :astonished:

The ire against double posting is that those answering on one thread are not privy to what has been suggested on the other and may be effectively wasting their time duplicating thought and advice. It is not quite so obvious here.

cross post violates the forum etiquette.
as described in the how-to-post topic:
Don't cross-post!

Even if one would only read one threat - answering questions which are already answered in another threat is a waste of member time and waste of technical resources.

Paul__B:
In this case it is passably forgivable as it is reasonably unlikely that the same people will or indeed can view both threads! :astonished:

The ire against double posting is that those answering on one thread are not privy to what has been suggested on the other and may be effectively wasting their time duplicating thought and advice. It is not quite so obvious here.

Exactly this is wasting time for helpers... not very ethical. And he did get similar feedback on both sides (and many French members read and post on both sides)

But bottom line, it’s against forum’s etiquette... so should not be done. May be wait a week and if no answer go post elsewhere and mention an link to the other thread / cross reference so that everyone is aware.

here it is: (thanks to Kamill)

const byte pushButtons[] = {2,3,4};
const byte leds[] = {6,7,8};
int buttonPushCounter[] = {0,0,0};   // counter for the number of button presses

/**********************************************************************/
void setup()
{
  for(int l=0;l<3;l++)
  {
    pinMode(pushButtons[l], INPUT_PULLUP);
    pinMode(leds[l], OUTPUT);
  }
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
}
/**********************************************************************/

void resetAllLeds()
{
  for(int k=0; k<3; k++)
  {
    digitalWrite(leds[k], LOW);
   }
  digitalWrite(13, LOW);
}
/**********************************************************************/
void resetCounter()
{
  for(int j=0; j<3; j++)
  {
    buttonPushCounter[j]= 0;
  }
}  
/**********************************************************************/
void loop() //ok
{
  static bool prevButtonState[3] = {};  // état précédent du bouton
  bool buttonState;
  for (byte i = 0; i < 3; i++)
  {
    buttonState = !digitalRead(pushButtons[i]); // ! pour raisonner en logique positive
    if (buttonState != prevButtonState[i])
    {
      // le bouton a changé dtétat
      prevButtonState[i] = buttonState;
      if (buttonState)
      {
        // on vient d'enfoncer le bouton
        if (buttonPushCounter[i] == 0)
        {
          // premier appui sur le bouton
          resetCounter();
          resetAllLeds();
          buttonPushCounter[i]++;
          digitalWrite(leds[i], HIGH);
        }
        else
        {
          // deuxième appui sur le bouton
          resetCounter();
          resetAllLeds();
          digitalWrite(13, HIGH);
        }
      }
      delay(20);  // anti rebond
    }
  }
}