how to control 3 leds with 3 pushbutton

I want to control 3 leds with 3 pushbutton. If i press pushbutton 1 one of the leds get switched on and after pressing it again it must go off. Same with other two leds and pushbutton. Please help i am trying to do it since 5hrs please help.

Here is a toggle code for one push-button. By duplicating the toggle part, you can use it for as many LEDs as you want.

// Button toggle with extreme reliability!

const int led1Pin =  13;    // LED pin number
const int button1 =  10;
int led1State = LOW;        // initialise the LED
char bstate1 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
// Routines by Paul__B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
// Routines by Paul__B of Arduino Forum
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(button1, INPUT);      
  digitalWrite(button1,HIGH);        // internal pullup all versions
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW; 
    } 
    digitalWrite(led1Pin, led1State);
  } 
}

All you need to do is make duplicates for 2 and 3 from bcount1 bstate1 and led1State and corresponding duplicates of the toggle code.

{Be warned though - if you were to submit this as part of a college assignment, the instructor would be very likely to ask you to explain it.}

Hey , PAUL i am not been able to understanding the 10UL in the program
void loop() {
// Toggle LED if button debounced
if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {

10UL is the constant number 10 but in Unsigned Long format. That is because the butndown() function may need this value in this format.

It is showing error while compiling.
the error "invalid conversion from long unsined int* to long unsined int .

The "long unsined int*" is the second parameter of the function butndown() as you may see bellow:

boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval)

As the variable bcount1 is an unsigned long:

unsigned long bcount1 = 0;

It shouldn't be any problem, because you are passing the reference of this variable:

  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {

in the &bcount1.

Have you changed this line?

can you please help me with some other program.

I have tried this program but it is not working as i needed it to work
.
const int buttonPin = 0;//pushbutton pin
const int buttonPin1 = 1;// pushbutton pin
const int ledPin = 2; // LED pin
const int ledPin1 = 3;
int ledPinState = HIGH; // the current state of the output pin
int ledPin1State = HIGH;
int buttonPinState; // the current reading from the input pin
int buttonPin1State;
int lastButtonPinState = LOW; // the previous reading from the input pin
int lastButtonPin1State = LOW;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);

// set initial LED state
digitalWrite(ledPin, ledPinState);
digitalWrite(ledPin1, ledPin1State);
}

void loop() {
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonPinState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonPinState ) {
{
buttonPinState = reading;
if (buttonPinState == HIGH) {
ledPinState = !ledPinState;
}
}
}
lastButtonPinState = reading;

int reading =digitalRead(buttonPin1);
if(reading!=lastButtonPin1State){
lastDebounceTime=millis();
}

if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonPin1State ) {
{
buttonPin1State=reading;
if(buttonPin1State==HIGH){
ledPinState = !ledPin1State;
}
}
}
lastButtonPin1State=reading;
}

// set the LED:
digitalWrite(ledPin, ledPinState);
digitalWrite(ledPin1, ledPin1State);

}

Is better for what you want this example.

I think my last post was a bit wrong. Maybe this example is better.

Based on this last example, I did the following code. Try it, and tell if is something like this that you need.

#define LED_NUMBER 3

const int buttonPin[LED_NUMBER] = { 7, 6, 5 };    
const int ledPin[LED_NUMBER] = { 13, 12, 11 };  

int ledState[LED_NUMBER] = { LOW, LOW, LOW };  
int buttonState[LED_NUMBER];    
int lastButtonState[LED_NUMBER] = { LOW, LOW, LOW };  

long lastDebounceTime[LED_NUMBER] = { 0, 0, 0 }; 
const long debounceDelay = 50;   

void setup() {
  for (int i=0; i<LED_NUMBER; i++) {
    pinMode(buttonPin[i], INPUT);
    pinMode(ledPin[i], OUTPUT);

    // turn on the internal pull-up 
    digitalWrite(buttonPin[i], HIGH);

    // set initial LED state
    digitalWrite(ledPin[i], ledState[i]);
  }
}

void loop() {
  
  for (int i=0; i<LED_NUMBER; i++) {
    int reading = digitalRead(buttonPin[i]);
    if (reading != lastButtonState[i]) {
      lastDebounceTime[i] = millis();
    }
  
    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      if (reading != buttonState[i]) {
        buttonState[i] = reading;

        if (buttonState[i] == LOW) {
          ledState[i] = !ledState[i];
        }
      }
    }
    digitalWrite(ledPin[i], ledState[i]);

    lastButtonState[i] = reading;
  }
}

I used the code provided by Paul_B earlier in this post and duplicated it for 4 buttons. It works great for me.

I do need some help though. My project requires the buttons to not turn off unless another button is pressed. I added code that uses "analogWrite" to turn off the previous button.

Issues:

  1. It seems to not affect the button count so the next time I try and push a button it will turn off the previous but it still requires 1 more press to turn on the one I want.

  2. I need each button to not be able to turn itself off and only turn off when another is pressed.

The following is the code for the 4 buttons along with the "analogWrite" code that I added

const int led1Pin =  3;    // LED pin number
const int button1 =  2;
const int led2Pin =  5; 
const int button2 =  4;
const int led3Pin =  6;
const int button3 =  7;
const int led4Pin =  9;
const int button4 =  8;
int led1State = LOW;        // initialise the LED
int led2State = HIGH;
int led3State = HIGH;
int led4State = HIGH;
char bstate1 = 0;
char bstate2 = 0;
char bstate3 = 0;
char bstate4 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.
unsigned long bcount2 = 0;
unsigned long bcount3 = 0;
unsigned long bcount4 = 0;


// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
// Routines by Paul__B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
// Routines by Paul__B of Arduino Forum
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(button1, INPUT); 
  pinMode(led2Pin, OUTPUT);      
  pinMode(button2, INPUT);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button3, INPUT);      
  pinMode(led4Pin, OUTPUT);      
  pinMode(button4, INPUT);        
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
      analogWrite (led1Pin, 255);
      analogWrite (led2Pin, 0);
      analogWrite (led3Pin, 0);
      analogWrite (led4Pin, 0);
      delay(500);
    }
    else {
      led1State = LOW; 
    }
    digitalWrite(led1Pin, led1State);
  } 
   // Toggle LED if button debounced
  if (butndown(digitalRead(button2), &bcount2, &bstate2, 10UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
      analogWrite (led1Pin, 0);
      analogWrite (led2Pin, 255);
      analogWrite (led3Pin, 0);
      analogWrite (led4Pin, 0);
      delay(500);
    }
    else {
      led2State = LOW; 
    }
    digitalWrite(led2Pin, led2State);
  } 
   // Toggle LED if button debounced
  if (butndown(digitalRead(button3), &bcount3, &bstate3, 10UL )) {
    if (led3State == LOW) {
      led3State = HIGH;
      analogWrite (led1Pin, 0);
      analogWrite (led2Pin, 0);
      analogWrite (led3Pin, 255);
      analogWrite (led4Pin, 0);
      delay(500);
    }
    else {
      led3State = LOW; 
    }
    digitalWrite(led3Pin, led3State);
  } 
   // Toggle LED if button debounced
  if (butndown(digitalRead(button4), &bcount4, &bstate4, 10UL )) {
    if (led4State == LOW) {
      led4State = HIGH;
      analogWrite (led1Pin, 0);
      analogWrite (led2Pin, 0);
      analogWrite (led3Pin, 0);
      analogWrite (led4Pin, 255);
      delay(500);      
    }
    else {
      led4State = LOW; 
    }
    digitalWrite(led4Pin, led4State);
  } 
}

I really appreciate all of your help!!

Through further experiment I was able to replicate the desired effect that I was seeking in my previous post.

Here is the code.

// Button toggle with extreme reliability!

//LED/BUTTON 1: SABOT
//LED/BUTTON 2: MPAT
//LED/BUTTON 3: HEAT
//LED/BUTTON 4: CAN


const int led1Pin =  3;    // LED pin number
const int button1 =  2;
const int led2Pin =  5; 
const int button2 =  4;
const int led3Pin =  6;
const int button3 =  7;
const int led4Pin =  9;
const int button4 =  8;
int led1State = LOW;        // initialise the LED
int led2State = HIGH;
int led3State = HIGH;
int led4State = HIGH;
char bstate1 = 0;
char bstate2 = 0;
char bstate3 = 0;
char bstate4 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.
unsigned long bcount2 = 0;
unsigned long bcount3 = 0;
unsigned long bcount4 = 0;



// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
// Routines by Paul__B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
// Routines by Paul__B of Arduino Forum
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(button1, INPUT); 
  pinMode(led2Pin, OUTPUT);      
  pinMode(button2, INPUT);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button3, INPUT);      
  pinMode(led4Pin, OUTPUT);      
  pinMode(button4, INPUT);        
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW; 
    }
    digitalWrite(led1Pin, led1State);
    if (led1State == HIGH){{
      led1State = LOW;
    }
      digitalWrite (led2Pin, LOW);
      digitalWrite (led3Pin, LOW);
      digitalWrite (led4Pin, LOW);
      delay(500);
    }
  } 
   // Toggle LED if button debounced
  if (butndown(digitalRead(button2), &bcount2, &bstate2, 10UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
    }
    else {
      led2State = LOW; 
    }
    digitalWrite(led2Pin, led2State);
    if (led2State == HIGH){{
      led2State = LOW;
     }
      digitalWrite (led1Pin, LOW);
      digitalWrite (led3Pin, LOW);
      digitalWrite (led4Pin, LOW);
      delay(500);
    }
  } 
   // Toggle LED if button debounced
  if (butndown(digitalRead(button3), &bcount3, &bstate3, 10UL )) {
    if (led3State == LOW) {
      led3State = HIGH;
    }
    else {
      led3State = LOW; 
    }
    digitalWrite(led3Pin, led3State);
    if (led3State == HIGH){{
      led3State = LOW;
    }
      digitalWrite (led1Pin, LOW);
      digitalWrite (led2Pin, LOW);
      digitalWrite (led4Pin, LOW);
      delay(500);
    }
  }
   // Toggle LED if button debounced
  if (butndown(digitalRead(button4), &bcount4, &bstate4, 10UL )) {
    if (led4State == LOW) {
      led4State = HIGH;   
    }
    else {
      led4State = LOW; 
    }
    digitalWrite(led4Pin, led4State);
    if (led4State == HIGH){{
      led4State = LOW;
    }
      digitalWrite (led1Pin, LOW);
      digitalWrite (led2Pin, LOW);
      digitalWrite (led3Pin, LOW);
      delay(500);   
    } 
  }
}

Thanks again guys!

You are now asking for the so-called "Radio Button" code.

// Radio Buttons!
const int led1Pin =  3;    // LED pin number
const int button1 =  2;
const int led2Pin =  5; 
const int button2 =  4;
const int led3Pin =  6;
const int button3 =  7;
const int led4Pin =  9;
const int button4 =  8;
char bstate1 = 0;
char bstate2 = 0;
char bstate3 = 0;
char bstate4 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.
unsigned long bcount2 = 0;
unsigned long bcount3 = 0;
unsigned long bcount4 = 0;


// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
// Routines by Paul__B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
// Routines by Paul__B of Arduino Forum
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(button1, INPUT); 
  pinMode(led2Pin, OUTPUT);      
  pinMode(button2, INPUT);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button3, INPUT);      
  pinMode(led4Pin, OUTPUT);      
  pinMode(button4, INPUT);        
  digitalWrite (led1Pin, LOW);
  digitalWrite (led2Pin, LOW);
  digitalWrite (led3Pin, LOW);
  digitalWrite (led4Pin, LOW);
}

void loop() {
  // Select LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    digitalWrite (led1Pin, HIGH);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, LOW);
    delay(500);
  } 
  // Select LED if button debounced
  if (butndown(digitalRead(button2), &bcount2, &bstate2, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, HIGH);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, LOW);
    delay(500);
  } 
  // Select LED if button debounced
  if (butndown(digitalRead(button3), &bcount3, &bstate3, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, HIGH);
    digitalWrite (led4Pin, LOW);
    delay(500);
  } 
  if (butndown(digitalRead(button4), &bcount4, &bstate4, 10UL )) {
    // Select LED if button debounced
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, HIGH);
    delay(500);      
  } 
}

And yes, there are certain tricks to its behaviour. :smiley:

Paul_B,

So what your saying is.....although I did achieve the desired effect by tinkering with the code.....I made it way more complicated than it had to be!! lol :smiley:

I am going to try the new code that you provided and let you know how it goes!!

PAUL__B how can we do this by using boolean function.I tried but was unsuccessful.Please help.

First you had better confirm whether my code works.

Hi Paul

My name is Neil, and new to this arduino stuff, and I was hoping you could help me. I have used your code for the above, and it works great! But what i am trying to achieve is that when "pushbutton 3" is pressed, LED3 stays lit infinity, then when "pushbutton 2" is pressed, it stays lit, but then delay of 10 seconds, it flashes LED2 until you acknowledge it by pressing "pushbutton2 again", then stays lit infinity. Then replicate that for "pushbutton 1" same as 2. Also, while the "flashes" happen, i would like a buzzer/piezo to sound as well. I have tried several different things, but just can't get it to work right. I'm still learning the code/sketch stuff...

Are you able to help me or point in the right direction? If you can help me out i can probably offer you some compensation like Paypal donation or something?

Cheers

Neil