Controlling 3 leds with 3 buttons saperately

Hi,

I am totally new to Arduino and programming. I built a working board; but have problems with the programming. My code and board works for one button and one led. By changing the input and output pins in the sketch, I tested all of my buttons and leds. I couldn't find any solution to control 3 leds with 3 pushbuttons. What I want is very simple. When I push button 1 led1 will go high. When I push button 1 again, led1 will go low. Same thing needs to work for button2 and button 3 also. But I couldn't manage to do that. My code:

int led1 = 1;
int volt1 = 45;
int voltstate1 = HIGH;
int reading1 ;
int previous1 = LOW;
long time1 = 0; 
long debounce1 = 200;


void setup() {                

 pinMode(led1, OUTPUT);
 pinMode(volt1, INPUT); 

}


void loop() {
 
reading1 = digitalRead(volt1);

 
  if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
    delay (2000);
    if (voltstate1 == HIGH) {
      voltstate1 = LOW;
    }
    else {
      voltstate1 = HIGH;
    }

    time1 = millis();    
  }

  digitalWrite(led1, voltstate1);

  previous1 = reading1;
}

Your using both millis() and delay() its either one or the other. Preferably use millis() and cast delay() out of your mind.

Fix that then look into using arrays. I haven't looked too far into the code to see if it will even work, but there are plenty examples on this forum that have done exactly what your are trying to do. Do a search and see what you find.

OK this is totally untested. you may have to debug it.

int firstLed=2;//please don't use 1 it interfers with the Serial port
int secondLed=3;
int thirdLed=4;

bool firstLedState=LOW;
bool secondLedState=LOW;
bool thirdLedState=LOW;

int firstButton=8;
int secondButton=9;
int thirdButton=10;


void setup()
{
pinMode(firstLed,OUTPUT);
pinMode(firstLed,OUTPUT);
pinMode(firstLed,OUTPUT);
}

void loop()
{
//instead of debouncing each button, we'll just have
//one delay at the end of the loop if any button
//has been pressed.  
//(saves having 3 delays when they're all pressed at once)
bool debounce=false;
 
if (digitalRead(firstButton==HIGH))
  {
  firstLedState=!firstLedState;
  //this is a shorthand way of saying the oposite of what it is now
  digitalWrite(firstLed,firstLedState); 
  debounce=true;
  }
  
if (digitalRead(secondButton==HIGH))
  {
  secondLedState=!secondLedState;
  //this is a shorthand way of saying the oposite of what it is now
  digitalWrite(secondLed,secondLedState); 
  debounce=true;  
  }
  
if (digitalRead(thirdButton==HIGH))
  {
  thirdLedState=!thirdLedState;
  //this is a shorthand way of saying the oposite of what it is now
  digitalWrite(thirdLed,thirdLedState); 
  debounce=true;
  }

if(debounce==true) 
  delay(400);
}

Oops. Just spotted a mistake. I didn't setup the button pins for input.

Scrap that last post, try this one instead

int firstLed=2;//please don't use 1 it interfers with the Serial port
int secondLed=3;
int thirdLed=4;

bool firstLedState=LOW;
bool secondLedState=LOW;
bool thirdLedState=LOW;

//CONNECT YOUR BUTTONS TO THESE PINS
//WITH THE OTHER SIDE OF THE BUTTONS GOING TO GND
int firstButton=8;
int secondButton=9;
int thirdButton=10;


void setup()
{
pinMode(firstLed,OUTPUT);
pinMode(firstLed,OUTPUT);
pinMode(firstLed,OUTPUT);

pinMode(firstButton,INPUT_PULLUP);
pinMode(secondButton,INPUT_PULLUP);
pinMode(thirdButton,INPUT_PULLUP);

}

void loop()
{
//instead of debouncing each button, we'll just have
//one delay at the end of the loop if any button
//has been pressed.  
//(saves having 3 delays when they're all pressed at once)
bool debounce=false;
 
if (digitalRead(firstButton==LOW))
  {
  firstLedState=!firstLedState;
  //this is a shorthand way of saying the oposite of what it is now
  digitalWrite(firstLed,firstLedState); 
  debounce=true;
  }
  
if (digitalRead(secondButton==LOW))
  {
  secondLedState=!secondLedState;
  //this is a shorthand way of saying the oposite of what it is now
  digitalWrite(secondLed,secondLedState); 
  debounce=true;  
  }
  
if (digitalRead(thirdButton==LOW))
  {
  thirdLedState=!thirdLedState;
  //this is a shorthand way of saying the oposite of what it is now
  digitalWrite(thirdLed,thirdLedState); 
  debounce=true;
  }

if(debounce==true) 
  delay(400);
}

Maybe simplify it a bit using arrays…
compiles but untested…

#define DEBOUNCE_TIME 50UL

byte ledPin[3] = {2,3,4};
byte buttonPin[3] = {8,9,10};
byte lastButtonState[3];
unsigned long timeStamp[3];
//
void setup()
{
  for (int i = 0; i < 3; i++)
  {
    pinMode(ledPin[i], OUTPUT);
    pinMode(buttonPin[i], INPUT_PULLUP);
  }
}
//
void loop()
{
  for (int i = 0; i < 3; i++)
  {
    byte buttonState = digitalRead(buttonPin[i]);
    if (buttonState == LOW && lastButtonState[i] == HIGH && millis() - timeStamp[i] > DEBOUNCE_TIME)
    {
      digitalWrite(ledPin[i], !digitalRead(ledPin[i]));
      timeStamp[i] = millis();
    }
    lastButtonState[i] = buttonState;
  }
}

Thank you for your replies!

I managed to make it work at last! :slight_smile:

int role1 = 3;
int role2 = 2;
int role3 = 4;
int role4 = 5;
int role5 = 6;
int role6 = 7;
int volt1 = 45;
int volt2 = 49;
int volt3 = 53;
int voltstate1 = HIGH;
int voltstate2 = HIGH;
int voltstate3 = HIGH;
int sarjstate1 = HIGH;
int sarjstate2 = LOW;
int reading1 ;
int reading2 ;
int reading3 ;
int previous1 = LOW;
int previous2 = LOW;
int previous3 = LOW;
long time1 = 0; 
long time2 = 0; 
long time3 = 0;
long debounce1 = 200;
long debounce2 = 200;
long debounce3 = 200;


void setup() {                

 pinMode(role1, OUTPUT);
 pinMode(role2, OUTPUT); 
 pinMode(role3, OUTPUT); 
 pinMode(role4, OUTPUT); 
 pinMode(role5, OUTPUT); 
 pinMode(role6, OUTPUT); 
 pinMode(volt1, INPUT); 
 pinMode(volt2, INPUT); 
 pinMode(volt3, INPUT);
}


void loop() {
 
reading1 = digitalRead(volt1);
reading2 = digitalRead(volt2);
reading3 = digitalRead(volt3);

  if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
  
    if (voltstate1 == HIGH) {

      voltstate1 = LOW;
        digitalWrite(role1, voltstate1);
        delay(300);
        digitalWrite(role4, sarjstate1);
        delay (3000);
        digitalWrite(role4, sarjstate2);
    }
    else {
      voltstate1 = HIGH;
        digitalWrite(role1, voltstate1);
        delay(300);
        digitalWrite(role4, sarjstate1);
        delay (3000);
        digitalWrite(role4, sarjstate2);
    }

    time1 = millis();    
  }
  
  else  if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
    
    if (voltstate2 == HIGH) {
      voltstate2 = LOW;
        digitalWrite(role2, voltstate2);
        delay(300);
        digitalWrite(role5, sarjstate1);
        delay (3000);
        digitalWrite(role5, sarjstate2);
    }
    else {
      voltstate2 = HIGH;
        digitalWrite(role2, voltstate2);
        delay(300);
        digitalWrite(role5, sarjstate1);
        delay (3000);
        digitalWrite(role5, sarjstate2);
    }

    time2 = millis();    
  }
  
    else  if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) {
    
    if (voltstate3 == HIGH) {
      voltstate3 = LOW;
        digitalWrite(role3, voltstate3);
        delay(300);
        digitalWrite(role6, sarjstate1);
        delay (3000);
        digitalWrite(role6, sarjstate2);
    }
    else {
      voltstate3 = HIGH;
        digitalWrite(role3, voltstate3);
        delay(300);
        digitalWrite(role6, sarjstate1);
        delay (3000);
        digitalWrite(role6, sarjstate2);
    }

    time3 = millis();    
  }
  
 
  previous1 = reading1;
  previous2 = reading2;
  previous3 = reading3;
  
}

This code is to control 6 different batteries. When one is in service, the other will be charged. I have thee voltage cutter cards. When voltage drops under 11.3V voltage cutter starts sending signal to Arduino (vultage cutter acts like a button), then arduino activates the relay so charging battery and service battery change places (relays acts like leds).

role4, role5 and role6 is for activating the chargers. The chargers I use have a button on it. It is automatic charger. To start a charge, button needs to be pushed down for two seconds. Thats why just after relay1 is activated, relay3 is also activated for 3 seconds.

I have one last thing to ask: I preferably don't want to use delay for activating role4, role5 and role6. Is there a simple way to activate them without delay?