Led are on and I don't know why

Hello everybody. I miss some basic electroncs here, so I'll appreciate any help.
I'm trying to control a two led signal with an Arduino structure. You can see the setup in the attached diagram. The idea is that when the pin 2 is put to GND (LOW) the signal switches from red to green by changing pins 4 and 7 ouputs in order to have a different current-flow direction.

While green is the only led on when I put 2 to GND, I get both leds on while pin 2 is left alone. Why?

The code I'm currently running is this:

#define SIGNAL_1_A 4
#define SIGNAL_1_B 7
#define BUTTON 2
int buttonState = HIGH;

void setup() {
pinMode(SIGNAL_1_A, OUTPUT);
pinMode(SIGNAL_1_B, OUTPUT);
pinMode(BUTTON, INPUT);

}
void loop (){
buttonState = digitalRead(BUTTON);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == LOW) {
setSignalTo(2);

} else {
setSignalTo(1);
}
}
void setSignalTo(int color) {
switch(color) {
case 1: //Green
digitalWrite(SIGNAL_1_A, LOW);
digitalWrite(SIGNAL_1_B, HIGH);
break;
case 2://Red
digitalWrite(SIGNAL_1_A, HIGH);
digitalWrite(SIGNAL_1_B, LOW);
break;
}
}

2019-01-06 00_31_45-Window.png

nightskywalker's picture:
a7a83f688aa4874763b3618e06d106eb7dc7152d.png

The diagram says the LEDs are connected between pins D5 and D7 but your code is written for them to be connected between pins 4 and 7.

Do you have a pull-up resistor on your button?

Thank you for the answer. Pins are good (changed them after the diagram was drawn). With a pullup resistor the circuit works fine indeed (I knew it should be something basic, sorry! :sweat_smile: )
Thank you!

2019-01-06 00_34_54-Window.png

Much easier to connect the button between pin and ground, and use pull up in code (no resistor needed).
pinMode(BUTTON, INPUT_PULLUP);
Compressed code attached.
Leo..

const byte ledA = 4;
const byte ledB = 7;
const byte buttonPin = 2;

void setup() {
 pinMode(ledA, OUTPUT);
 pinMode(ledB, OUTPUT);
 pinMode(buttonPin, INPUT_PULLUP); // button between pin and ground
}

void loop() {
 digitalWrite(ledA, digitalRead(buttonPin));
 digitalWrite(ledB, !digitalRead(ledA));
}

You're welcome. I'm glad to hear it's working now!

Here's what I think was happening: Without a pull-up resistor, pin 2 was floating when the button was not connecting it to ground. When floating, a pin can flip back and forth from HIGH to LOW very quickly. If so, the LEDs were actually flashing on and off, but maybe so fast that they appeared to the eye to both be on at the same time.