controlling different sets of LEDs with different buttons

Hey guys,

I want to control 4 rows of LED, each row containing 5 different LED's, from 4 different buttons and have them work independently.

I've got this code which controls one row of LED's using switch states.

#define button 3
#define redone 4
#define redtwo 5
#define redthree 6
#define redfour 7
#define green 8


int state = 0;
int old = 0;
int buttonPoll = 0;


void setup() {
  // put your setup code here, to run once:
pinMode (button, INPUT);
pinMode (redone, OUTPUT);
pinMode (redtwo, OUTPUT);
pinMode (redthree, OUTPUT);
pinMode (redfour, OUTPUT);
pinMode (green, OUTPUT);

digitalWrite (redone, LOW);
digitalWrite (redtwo, LOW);
digitalWrite (redthree, LOW);
digitalWrite (redfour, LOW);
digitalWrite (green, LOW);

}

void loop() {
  // put your main code here, to run repeatedly:
buttonPoll = digitalRead(button);
if (buttonPoll == 1){
  delay (50);
  buttonPoll = digitalRead(button);
  if(buttonPoll == 0){
    state = old + 1;
    
  }}
else {
 delay(100);
}

 switch(state) {
  case 1:
digitalWrite (redone, HIGH);
digitalWrite (redtwo, LOW);
digitalWrite (redthree, LOW);
digitalWrite (redfour, LOW);
digitalWrite (green, LOW);
old = state;
break;

case 2:
digitalWrite (redone, HIGH);
digitalWrite (redtwo, HIGH);
digitalWrite (redthree, LOW);
digitalWrite (redfour, LOW);
digitalWrite (green, LOW);
old = state;
break;

case 3:
digitalWrite (redone, HIGH);
digitalWrite (redtwo, HIGH);
digitalWrite (redthree, HIGH);
digitalWrite (redfour, LOW);
digitalWrite (green, LOW);
old = state;
break;

case 4:
digitalWrite (redone, HIGH);
digitalWrite (redtwo, HIGH);
digitalWrite (redthree, HIGH);
digitalWrite (redfour, HIGH);
digitalWrite (green, LOW);
old = state;
break;

case 5:
digitalWrite (redone, HIGH);
digitalWrite (redtwo, HIGH);
digitalWrite (redthree, HIGH);
digitalWrite (redfour, HIGH);
digitalWrite (green, HIGH);
old = state;
break;

case 6:
digitalWrite (redone, LOW);
digitalWrite (redtwo, LOW);
digitalWrite (redthree, LOW);
digitalWrite (redfour, LOW);
digitalWrite (green, LOW);
old = 0;
break;
}
}

How can I create this action but have 4 buttons doing this independently? Thanks very much, any help would be greatly appreciated, still learning the ropes!

Well before we get into code, yo seem to be describing twenty LEDs - how are you going to control these?

Or how is your arrangement actually?

okay cool, thanks guys ill look into using array. Something told me this was a lot of unnecessary code!

The picture bellow shows how i have arranged the circuit

the problem im having is I only want the next light in the array to come on with the push of a button. With arrays it looks like i can get a set pattern but not multiple push buttons controlling different rows of LED's

50938112_361656971282336_2658492182498902016_n.jpg

Wow! A well-taken photo with light! Unusual in posts here. :roll_eyes:

Using Mega 2560 - OK, this has plenty of pins. You still need to be careful about the total current used and provide it with a proper 5 V supply. Otherwise no advantage to using a Mega 2560. For 20 LEDs, the resistors should be about 330 Ohms.

And you should connect push-buttons between the pin and ground; you can use pinMode of INPUT_PULLUP or may need a pull-up resistor if the switch is any distance from the processor.

pranghead:
the problem im having is I only want the next light in the array to come on with the push of a button. With arrays it looks like i can get a set pattern but not multiple push buttons controlling different rows of LED's

Nothing whatsoever to do with the nature of arrays.

Okay thanks, I have installed a resistor to act as a pulldown resistor. Do you mean that arrays arnt what I should be using

would I have to use millis to have multiple pushbuttons controlling code like this?

int timer = 100;           // The higher the number, the slower the timing.
int ledPins[] = {
  2, 7, 4, 6, 5, 3
};       // an array of pin numbers to which LEDs are attached
int pinCount = 6;           // the number of pins (i.e. the length of the array)

void setup() {
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
}

pranghead:
Okay thanks, I have installed a resistor to act as a pull-down resistor. Do you mean that arrays aren't what I should be using

As Delta_G explained, it is far more sensible to use arrays for repetitive elements You said "With arrays it looks like i can get a set pattern but not multiple push buttons controlling different rows of LED's". I pointed out that whatever problems you have with the code, using arrays for multiple push-buttons makes it easier, not more difficult.

pranghead:
Would I have to use millis to have multiple push-buttons controlling code like this?

You may have noticed a theme in these forums. The function "delay()" is provided as a beginners toy - a "pacifier" if you like.

It has virtually no place in "real world" code such as you require. The general theme on "state machine" coding is this discussion though I have some code of my own.

Yes, it is based on "millis()".

ok cool. Ive been looking through all the videos but im still struggling at the moment to understand millis and array and using them together. Ill just keep looking for now. Need this project to be done soon as so even though its cheating, i may just use four separate boards. Please dont judge me!

pranghead:
I'm still struggling at the moment to understand millis and array and using them together.

Well, there really is no connection between the two. Yes, of course you would use them together because you are writing a program that uses them.

pranghead:
Need this project to be done soon as so even though its cheating, I may just use four separate boards.

What's the hurry?

If it is really urgent for a good cause, I may let you cheat and find you some "boilerplate" code. :grinning: