HELP please on simple 4 button SWITCH selector

I got the first switch to work, how would I go along this program. There are 4 button corresponding to a specific LED. I want to create a selector switch that will remain on until same button are press again or other button are selected. If other button is press, it will shut off the prior LED and turn the LED on for that specific button. Each button are basically different MODEs and Only one Mode is allowed. (I am making a charging selector switch and I am new to programming with arduino) Thanks again for the help..

//Input
const int buttonPin=2;
const int buttonPin2=3;
const int buttonPin3=4;
const int buttonPin4=5;

//Output
const int ledPin=13;
const int ledPin2=12;
const int ledPin3=11;
const int ledPin4=10;

int buttonState=0;

void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(ledPin4,OUTPUT);

pinMode(buttonPin,INPUT);
pinMode(buttonPin2,INPUT);
pinMode(buttonPin3,INPUT);
pinMode(buttonPin4,INPUT);
}

void loop()
{
buttonState=digitalRead(buttonPin);
if(buttonState==HIGH)
{
digitalWrite(ledPin,HIGH);
}

}

Charger_Selector.ino (596 Bytes)

You need to read the state of the 4 buttons and save the values to 4 different variables.

You also need 4 variables that record the states of the buttons from the previous test.

Then you need to check if the value of any of the variables has changed from LOW to HIGH since the last time you checked and if it has you need to turn on the appropriate LED and turn off the others.

...R

You may find inspiration/code with a site search of 'radio button'.

I did something like this, that record the states and if statements for instruction. But for some reason all of them turns on!

//Input
const int buttonPin=2;
const int buttonPin2=3;
const int buttonPin3=4;
const int buttonPin4=5;

//Output
const int ledPin=13;
const int ledPin2=12;
const int ledPin3=11;
const int ledPin4=10;

int buttonState=0;
int buttonState1=0;
int buttonState2=0;
int buttonState3=0;

void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(buttonPin,INPUT);
pinMode(buttonPin2,INPUT);
}

void loop()
{
buttonState=digitalRead(buttonPin);

if(buttonState==HIGH)
{
digitalWrite(ledPin,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}

buttonState2=digitalRead(buttonPin2);
if(buttonState2==HIGH)
{
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin3,LOW);

}
buttonState3=digitalRead(buttonPin3);
if(buttonState3==HIGH)
{
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin2,LOW);

}
}

  pinMode(buttonPin,INPUT);
  pinMode(buttonPin2,INPUT);

Do you have pulldown resistors wired to these pins along with the switches? Where does the other leg of each go?

const int ledPin=13;
const int ledPin2=12;
const int ledPin3=11;
const int ledPin4=10;

Do you count uh-huh, 2, 3, 4? Or do you count 1, 2, 3, 4?

   digitalWrite(ledPin3,LOW);

Why are you writing to an INPUT pin?

I am trying to make a switch that lights up a corresponding LED. Each switch corresponds to an LED. Only one switch is allowed to be ON at a time. Once a switch is press, corresponding LED will turn on until other switch are press.

Problem, Other switch is turning on even though button state are all different given name...

my CODE

//Input
const int buttonPin=2;
const int buttonPin2=3;
const int buttonPin3=4;
const int buttonPin4=5;

//Output
const int ledPin=13;
const int ledPin2=12;
const int ledPin3=11;
const int ledPin4=10;

int buttonState=0;
int buttonState1=0;
int buttonState2=0;
int buttonState3=0;

void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(buttonPin,INPUT);
pinMode(buttonPin2,INPUT);
}

void loop()
{
buttonState=digitalRead(buttonPin);

if(buttonState==HIGH)
{
digitalWrite(ledPin,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}

buttonState2=digitalRead(buttonPin2);
if(buttonState2==HIGH)
{
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin3,LOW);
}

buttonState3=digitalRead(buttonPin3);
if(buttonState3==HIGH)
{
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin2,LOW);
}

}

CODE_Wed.ino (983 Bytes)

Why did you start ANOTHER thread?

Do you always number like apple, apple 2, apple 3 etc? If you start numbering, at least number everything. Or easier, use arrays :wink:

Thing is, nothing is preventing the other buttons to do it's stuff. So if two buttons are pressed, one part tries to turn on a led and the other part to turn ff the led. Non wins but the led is simply on between the on and off command. Lock the reading of the other buttons once a led is on.

Also, do you have pull down resistors connected to the buttons? You do know using the internal pull ups is easier?

How I would do it:

const byte LedPins[] = {13, 12, 11, 10};
const byte NrSets = sizeof(LedPins);
const byte ButtonPins[NrSets] = {2, 3, 4, 5};

bool turnedOn;

void setup(){
  for(byte i = 0; i < NrSets; i++){
    pinMode(LedPins[i], OUTPUT);
    pinMode(ButtonPins[i], INPUT_PULLUP);
  }
}

void loop(){
  if(turnedOn){
    //check to see it all buttons are released
    bool allReleased = true;
    for(byte i = 0; i < NrSets; i++){
      allReleased &= digitalRead(ButtonPins[i]);;
    }
    //if so, turn off all leds and remember that
    if(allReleased){
      for(byte i = 0; i < NrSets; i++){
        digitalWrite(LedPins[i], LOW);
      }
      turnedOn = false;
    }
  }
  else{
    //check all buttons to see if one is pressed
    for(byte i = 0; i < NrSets; i++){
      //if so
      if(!digitalRead(ButtonPins[i])){
        //turn on corresponding led
        digitalWrite(LedPins[i], HIGH);
        //remember a led is on
        turnedOn = true;
        //and stop looking any further
        break;
      }
    }
  }
}

Note, this code uses internal pull ups and thus expects a pushed button to read LOW.

PaulS:

   digitalWrite(ledPin3,LOW);

Why are you writing to an INPUT pin?

OOPs ....

...R

jayded70:
I did something like this, that record the states and if statements for instruction. But for some reason all of them turns on!

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is very raggedy on two counts. {A} it is not indented consistently which makes it hard to see what belongs with what. Use the AutoFormat tool. And {B} you have things scattered about, and you don't have all of everything. For example put all the lines that read the buttons one after the other. Then you can see that you have 3 lines when there should be 4.

As @PaulS has pointed out you need to either use INPUT_PULLUP or use external pull-up or pull-down resistors.

...R

Thanks I got it to work with the Pull up switch.. I was going to use Solid State Relay along with an arduino to control 4 different charger for a battery. Wall charger, Generator, Vehicle to vehicle, and Hyrdaulics.. Maybe I can implement this .. Is there anything else that I should look out for?

@jayded70, please do not cross-post. Threads merged.