Go Down

Topic: 4 leds and 3 buttons issue (Read 559 times) previous topic - next topic

carranen

Aug 21, 2019, 09:13 pm Last Edit: Aug 21, 2019, 09:42 pm by carranen
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...

Code: [Select]

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




PaulRB

#1
Aug 21, 2019, 09:28 pm Last Edit: Aug 21, 2019, 09:35 pm by PaulRB

You should not share a series resistor between LEDs like that. They should have one each.
Code: [Select]
if(buttonPushCounter[i] >> 1)
I think you meant to use ">" not ">>".

carranen

#2
Aug 21, 2019, 09:41 pm Last Edit: Aug 21, 2019, 09:51 pm by carranen
Thank sir.

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

PaulRB

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.

PaulRB

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.

septillion

Code: [Select]
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....
Code: [Select]
const byte PushButtonPins[] {7, 4, 2};
const byte LedPins[] = {14, 15, 16};
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

J-M-L

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

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!  :smiley-eek:

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.

noiasca

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.
how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

J-M-L

#9
Aug 25, 2019, 10:05 am Last Edit: Aug 25, 2019, 10:07 am by J-M-L
In this case it is passably forgivable as it is reasonably unlikely that the same people will or indeed can view both threads!  :smiley-eek:

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.
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

carranen

here it is: (thanks to Kamill)

Code: [Select]

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

Go Up