Ignoring other inputs while any input is high

I have a hopefully simple question.

I just started (within the past 48 hours) programming Arduino and have next to no other programming experience.

The task I am trying to accomplish is to monitor three inputs. When an input is high (button press), the appropriate led will turn on, and when the button is released the led turns off. If a second or third input goes high during this, these should be ignored and stay off, until the first input goes low.

I tried using a while command, but didn't have any success.

Edit:
The problem I currently have is if one button is pressed and held, pressing another button will allow that led to also turn on. I can get all three to turn on by holding down all three buttons. I only want the first led to turn on and the others to be ignored.

Here is the code I currently have

int greenledPin = 9;
int redledPin = 12;
int blueledPin = 10;
int buttonApin = 2;
int buttonBpin = 3;
int buttonCpin = 4;


void setup() 
{
  pinMode(greenledPin, OUTPUT);
  pinMode(redledPin, OUTPUT);
  pinMode(blueledPin, OUTPUT);
  pinMode(buttonApin, INPUT);  
  pinMode(buttonBpin, INPUT);  
  pinMode(buttonCpin, INPUT);  
}


void loop() {
  int stateAButton = digitalRead(buttonApin); //read the state of button A
  if(stateAButton == 1) { //if is pressed    
    digitalWrite(greenledPin, HIGH); //write 1 to green led pin 
  }else { //if not pressed
    digitalWrite(greenledPin, LOW);  //write 0 to green led pin
  }
    int stateBButton = digitalRead(buttonBpin); //read the state of button B
  if(stateBButton == 1) { //if is pressed
    digitalWrite(blueledPin, HIGH); //write 1 to blue led pin
  } else { //if not pressed
    digitalWrite(blueledPin, LOW);  //write 0 to blue led pin
  }    
     int stateCButton = digitalRead(buttonCpin); //read the state of button C
  if(stateCButton == 1) { //if is pressed
     digitalWrite(redledPin, HIGH); //write 1 to red led pin
  } else { //if not pressed
    digitalWrite(redledPin, LOW);  //write 0 to red led pin
  }
  }

How are the inputs wired ?
Have you got pulldown resistors in place keeping them normally LOW

UKHeliBob:
How are the inputs wired ?
Have you got pulldown resistors in place keeping them normally LOW

All inputs are NO push button switches.

There is 5v on one side and 10k pull downs on the other side. The input is wired to the 10k pull down side.

I should note the problem I currently have is if one button is pressed and held, pressing another button will allow that led to also turn on. I can get all three to turn on by holding down all three buttons. I only want the first led to turn on and the others to be ignored.

A popular approach is to use compound if statements to prevent unwanted actions.

if( (digitalRead(buttonApin) == HIGH) && (digitalRead(buttonBpin) == LOW) && (digitalRead(buttonCpin) == LOW)) turn_on_LED();

Reverse HIGH/LOW if your switch is LOW when pushed.

There are many other approaches, another being to use a state variable:

int buttons_state;
...
buttons_state = 4*digitalRead(buttonApin) + 2*digitalRead(buttonBpin) + digitalRead(buttonBpin);
if (buttons_state == 4) do_something(); // only button A is pushed
if (buttons_state == 6) do_another_thing(); //button A and button B are pushed

jremington:
A popular approach is to use compound if statements to prevent unwanted actions.

if( (digitalRead(buttonApin) == HIGH) && (digitalRead(buttonBpin) == LOW) && (digitalRead(buttonCpin) == LOW)) turn_on_LED();

I thought this was going to be the answer, but once a button is pressed, that led stays on. I need the led to turn off when the button is released.

I changed the inputs from colors to a priority level.

//www.elegoo.com
//2016.12.08

int greenledPin = 9;
int redledPin = 12;
int blueledPin = 10;
int high = 2;
int middle = 3;
int low = 4;


void setup() 
{
  pinMode(greenledPin, OUTPUT);
  pinMode(redledPin, OUTPUT);
  pinMode(blueledPin, OUTPUT);
  pinMode(high, INPUT);  
  pinMode(middle, INPUT);  
  pinMode(low, INPUT);  
}


void loop(){
 buttoncheck();
}

void buttoncheck(){

  if( (digitalRead(high) == HIGH) && (digitalRead(middle) == LOW) && (digitalRead(low) == LOW) ) turn_on_red_LED();
  if( (digitalRead(high) == HIGH) && (digitalRead(middle) == HIGH) && (digitalRead(low) == LOW) ) turn_on_red_LED();
  if( (digitalRead(high) == HIGH) && (digitalRead(middle) == LOW) && (digitalRead(low) == HIGH) ) turn_on_red_LED();
  if( (digitalRead(high) == HIGH) && (digitalRead(middle) == HIGH) && (digitalRead(low) == HIGH) ) turn_on_red_LED();

  if( (digitalRead(high) == LOW) && (digitalRead(middle) == HIGH) && (digitalRead(low) == LOW) ) turn_on_blue_LED();
  if( (digitalRead(high) == LOW) && (digitalRead(middle) == HIGH) && (digitalRead(low) == HIGH) ) turn_on_blue_LED();

  if( (digitalRead(high) == LOW) && (digitalRead(middle) == LOW) && (digitalRead(low) == HIGH) ) turn_on_green_LED();

}
 
  void turn_on_red_LED(){
     digitalWrite(redledPin, HIGH);
  }
   void turn_on_blue_LED(){
     digitalWrite(blueledPin, HIGH);
  }  
 void turn_on_green_LED(){
     digitalWrite(greenledPin, HIGH);
  }

You've overcomplicated the if statements. You want to switch on each LED when it's switch is pressed and the others aren't. You switch off each LED when it's LED is NOT pressed (and you don't care about the other switches).

Steve

I need the led to turn off when the button is released.

Then you need to add an if condition to check for that release, or the other button state, and turn the LED off.

The computer does EXACTLY what you tell it to do, and nothing else.

void loop() {
  while (digitalRead(redledPin)) digitalWrite(redledPin, HIGH);
  digitalWrite(redledPin, LOW);
  while (digitalRead(blueledPin)) digitalWrite(blueledPin, HIGH);
  digitalWrite(blueledPin, LOW);
  while (digitalRead(greenledPin)) digitalWrite(greenledPin, HIGH);
  digitalWrite(greenledPin, LOW);
}

:wink:

jremington:
Then you need to add an if condition to check for that release, or the other button state, and turn the LED off.

The computer does EXACTLY what you tell it to do, and nothing else.

Thanks - a quick check for all buttons fixed the issue.