Domotic, how to handle multiple LED with multiple buttons ?

Hi!

I'm making a program to control multiple LED with multiple button (one for 2 LED and another for one LED), but my problem is that one led is blinking :confused:

The pinLed3, I totaly don't get it, because it's the same code that for the 2 other LED!

I think it's a problem with my code, because when I delete the code for the pinLed3, it works fine.

Thanks for your help :wink:

/*
  le bouton poussoir est connecté au pin 2 avec une résistance pull-down de 10KΩ
  les LED sont connectées au pins 4 et 6 avec des résistances de 220Ω
*/

//déclaration des variables
int buttonPin = 6;
int buttonPin2 = 8;
int pinLed1, pinLed2, pinLed3;
int buttonState = 0;     // current state of the button
int buttonState2 = 0;
int lastButtonState = 0;
int lastButtonState2 = 0;
int ledState = 0;
int ledState2 = 0;
int t = 1000;


void setup()
{
  //initialisation des variables
  buttonPin = 6;
  buttonPin2 = 8;
  pinLed1 = 11;
  pinLed2 = 5;
  pinLed3 = 4;

  //définition des modes
  pinMode(buttonPin, INPUT_PULLUP); //mode lecture pour le bouton
  pinMode(buttonPin2, INPUT_PULLUP); //mode lecture pour le bouton
  pinMode(pinLed1, OUTPUT); //mode écriture pour led1
  pinMode(pinLed2, OUTPUT); //mode écriture pour led2
  pinMode(pinLed3, OUTPUT); //mode écriture pour led2

}
void loop()
{
  //lecture de l'état du bouton et stockage dans etatBouton
  boolean buttonState = digitalRead(buttonPin);
  boolean buttonState2 = digitalRead(buttonPin2);


  //test des conditions

  if (buttonState == 1) {

    if (ledState == 1) ledState = 0;

    else ledState = 1;

    lastButtonState = buttonState;

    digitalWrite(pinLed1, ledState);
    digitalWrite(pinLed2, ledState);

    delay(t);

  }

  else if (buttonState2 == 1) {

    if (ledState2 == 1) ledState2 = 0;

    else ledState2 = 1;

    lastButtonState2 = buttonState2;

    digitalWrite(pinLed3, ledState2);

    delay(t);

  }



}

How are your buttons wired? I am suspicious because you use INPUT_PULLUP and check digitalRead() == 1...

Thanks for your reply !

They are wired without resistor if it's what you want to know. They're not the arduino's button,

They're wired in NO, one side on the ground and the other side on the pin of the arduino.

What do you mean by ckeck digitalRead()==1 ?

Ty

With INPUT_PULLUP and the NO and COMM switch terminals wired between the input pin and ground, digitalRead() will return LOW (==0) when the switch is pressed and HIGH (==1) when not pressed. Is that what you expected?

Also, +1 karma for using code tags and inserting images correctly in your posts.

Question: what or whom is "Domotic"? If he/she is a forum member, it is usual to put "@" before the name, so "@Domotic".

PaulRB:
With INPUT_PULLUP and the NO and COMM switch terminals wired between the input pin and ground, digitalRead() will return LOW (==0) when the switch is pressed and HIGH (==1) when not pressed. Is that what you expected?

Yes, but what I don't expect is when I use a second if statement, (which is the copy paste from the one before ) the pinLed3 blink! What I want from the pinLed3 is ton turn on/off when I press the button.

And Domotic stands for "home automation" sorry for the poor translation, but Deepl.com told me that it was a correct answer

Domotic stands for "home automation"

Ah, ok thanks.

I have read your code again, but I did not spot any obvious cause of your problem. I did notice that lastButtonState and lastButtonState2 are set but never read/used. I guess they are intended to detect when a button state changes.