1 input controlling 2 outputs Arduino Uno

Here I have programmed 2 buttons, and each of the buttons turns on a single LED. I want to add a third button that will turn on all the LED's.

int buttonState1 = 0;
int buttonState2 = 0;
void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  digitalWrite(12, digitalRead(2));
  digitalWrite(11, digitalRead(3));
  
  delay(10);
}

Here I changed the code so that I can add the third button. But I am struggling with the syntax on line 16 (the digitalWrite section). I want output pins 11 and 12 to be "active". One button turns the led connected to pin 11 on. the second button turns the led connected to pin 13 on. the third button should turn both led's connected to pins 11 and 12 on.

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  buttonState3 = digitalRead(4);
  digitalWrite(12, digitalRead(2));
  digitalWrite(11, digitalRead(3));
  digitalWrite(11, 12 digitalRead(4));
  
  delay(10);
}

What have you tried ?

Not much. I have come so far. I am struggling with the syntax. I am a beginner :sweat_smile:

buttonState1 = digitalRead(2);

digitalWrite(12, digitalRead(2));

Side note: it's not wrong in the sense of not working or being illegal, but there's little point in reading the inputs into variables and then reading them again in those writes.

Seeing as you have them, might as well use them:

digitalWrite(12, buttonState1);

Or of course, not bother reading them the first time.

You first have to learn to read code before you can write it.
So just read what those lines that you have got working so far, and then duplicate one set but use different numbers in the read and write calls.

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  buttonState3 = digitalRead(4);

  if (buttonState1 == 1) {
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    Serial.println("11 HIGH and 12 LOW");
  }

  if (buttonState2 == 1) {
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    Serial.println("11 LOW and 12 HIGH");
  }

  if (buttonState3 == 1) {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
    Serial.println("11 and 12 HIGH");
  }
  delay(10);
}

Here is a quick example of how to use digital read and digital write, so the third button will set the LEDs HIGH
Also, you should use boolean as a type for the variables, because the value of the button should only be set to true or false.

Or of course just wait until someone @dimulino pops a full answer up in the thread :wink:

Side note: the buttons have the internal pullups enabled, so pressed is low in case that fact was missed.

Yes but you will have learnt nothing then did you.

@dimulino is a brand new member and hasn't yet got the difference between helping you and doing it for you.

I think the loss is all yours.

I also think that his code is not what you asked for. According to your title you wanted to control 3 outputs with one input. That code controls three outputs with three inputs.

haha, sorry for bothering I just realized the "policy" of the forum, from now on I will try to help others to learn. I am stuck at some dead-end in my project and I wait for some new information on continuing. Therefore I tried to utilize my time on helping others

@Grumpy_Mike @silvershovel

1 Like

@janesmostert It's very confusing that you changed the code in the opening post- makes the thread very difficult to follow now. Better idea is to post a full program with the changes in a later post, leaving the original post intact. Also means that someone who is already in the thread might not notice what you did higher up.

I also want my buttons to work so that for as long as their pushed, the led should be on and when the button is released, the led should turn of.

Thanks for your earlier help, BTW

Will keep it in mind, thanks!

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);

All 3 button pins will be HIGH unless the button is pressed so all 3 if statements will return true

Is that what is wanted ?

Hi, here I have 3 buttons. One button turns one LED on, the other buttons turns another LED on, the third button turns both LEDs on. But I want the LED to stay on for as long as I push the button and I want it to turn of when I release the button.

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop()
{
  buttonState1 = digitalRead(2);
  buttonState2 = digitalRead(3);
  buttonState3 = digitalRead(4);

  if (buttonState1 == 1) {
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
  }

  if (buttonState2 == 1) {
    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);
  }

  if (buttonState3 == 1) {
    digitalWrite(12, HIGH);
    digitalWrite(11, HIGH);
  }
  delay(10);
}

Duplicate topics merged

@janesmostert Do not create multiple topics for the same problem or you run the risk of being suspended for a period

It's always helpful if the debug prints mirror what is actually going on.

It's not so much a policy as a philosophy. Many of the forum regulars prefer a "teach a man to fish" method over "give a man a fish", but there's no regulation saying that you can't give fish away if you want to.

3 Likes

YMMD

But I don´t like fish.

I do, and they are healthier for you. :+1:

Of course the crumbed fish I had for lunch is not really as healthy as just plain fish, but dead tasty (with lemon juice) and overall supposed to be healthier than red meat (but it's not that I don't eat that at all :grin:). That was the Monday menu, Friday will be the fish fillets with all sorts of condiments, veggies and "potato bake". Can't wait. :sunglasses:

Of course it is faster to provide the full working code, and teaching fishing is a slow process.

Oh well. :thinking: