Multiple toggle buttons for multiple functions

Hello,
We’ve built a 25ft by 25ft laser grid on the ceiling of our garage. Lasers spaced out by a foot interval, it makes the ceiling look like a Tron game. We wrote a few different functions for controlling the lasers such as all on, random, moving squares, and sound active. We’re using an Arduino Mega and pretty much all of its ports. Our problem is now being able to switch the functions on the fly. The best solution in our minds is to have a box with buttons and each button toggles a different laser function. The analog pins on our Mega are being used as digital inputs for the buttons because we’ve ran out of pins. (we know we can learn to use shift registers but the debut of the laser grid is coming up and we wanted to get it done) We just cant seem to be able to get more than 3 buttons working as toggles at a time. We tried many different codes found online and to no avail. Ones with debounce, ones with booleans, and its making us go bananas.

We’re using these buttons: Arcade Button with LED - 30mm Translucent Red : ID 3489 : $2.50 : Adafruit Industries, Unique & fun DIY electronics and kits

We’ve printed this beautiful box shown below, in the end we’re trying to get 9 buttons.

The buttons seem to be very bouncy. They switch high low randomly not after being pressed as well.
When a button is pressed it should turn off the last button that was on (only one function at a time).

In the picture’s and in this code we’re using a Arduino Uno because the Mega is downstairs and have 50 tedious wires running to it.

Right now were just trying to get the button LEDs to light but will add the laser functions later.

Here is the best code we’ve had so far but by no means is it good in any way:

int ledpin = 15;
int ledpin2 = 17;
int ledpin3 = 19;
int ledpin4 = 3;
int pushbutton1 = 14;
int pushbutton2 = 16;
int pushbutton3 = 18;
int pushbutton4 = 2;
boolean lastbuttonstate1 = LOW;
boolean lastbuttonstate2 = LOW;
boolean lastbuttonstate3 = LOW;
boolean lastbuttonstate4 = LOW;

void setup()
{
pinMode(ledpin,OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(ledpin3, OUTPUT);
pinMode(ledpin4, OUTPUT);
pinMode(pushbutton1,INPUT);
pinMode(pushbutton2,INPUT);
pinMode(pushbutton3,INPUT);
pinMode(pushbutton4,INPUT);
}

void loop()
{
if ( digitalRead(pushbutton1)== HIGH && digitalRead(pushbutton2) == LOW && digitalRead(pushbutton3) == LOW && digitalRead(pushbutton4) == LOW && lastbuttonstate1 == LOW )
{ digitalWrite (ledpin,HIGH);
digitalWrite(ledpin2, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, LOW);

}

else if ( digitalRead(pushbutton1) == LOW && digitalRead(pushbutton2)== LOW && digitalRead(pushbutton3) == LOW && digitalRead(pushbutton4) == LOW && lastbuttonstate1 == HIGH)
{ digitalWrite (ledpin,HIGH);
digitalWrite(ledpin2, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, LOW);

}

else if ( digitalRead(pushbutton1)== LOW && digitalRead(pushbutton2) == HIGH && digitalRead(pushbutton3) == LOW && digitalRead(pushbutton4) == LOW && lastbuttonstate2 == LOW)
{
digitalWrite (ledpin2,HIGH);
digitalWrite(ledpin, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, LOW);

}

else if ( digitalRead(pushbutton1) == LOW && digitalRead(pushbutton2)== LOW && digitalRead(pushbutton3) == LOW && digitalRead(pushbutton4) == LOW && lastbuttonstate2 == HIGH)
{
digitalWrite (ledpin2,HIGH);
digitalWrite(ledpin, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, LOW);
}

else if ( digitalRead(pushbutton1)== LOW && digitalRead(pushbutton2) == LOW && digitalRead(pushbutton3) == HIGH && digitalRead(pushbutton4) == LOW && lastbuttonstate3 == LOW)
{ digitalWrite (ledpin2,LOW);
digitalWrite(ledpin, LOW);
digitalWrite(ledpin3, HIGH);
digitalWrite(ledpin4, LOW);

}

else if ( digitalRead(pushbutton1) == LOW && digitalRead(pushbutton2)== LOW && digitalRead(pushbutton3) == LOW && digitalRead(pushbutton4) == LOW && lastbuttonstate3 == HIGH) {
digitalWrite (ledpin2,LOW);
digitalWrite(ledpin, LOW);
digitalWrite(ledpin3, HIGH);
digitalWrite(ledpin4, LOW);

}

else if ( digitalRead(pushbutton1)== LOW && digitalRead(pushbutton2) == LOW && digitalRead(pushbutton3) == LOW && digitalRead(pushbutton4) == HIGH && lastbuttonstate4 == LOW)
{
digitalWrite (ledpin2,LOW);
digitalWrite(ledpin, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, HIGH);

}

else if ( digitalRead(pushbutton1) == LOW && digitalRead(pushbutton2)== LOW && digitalRead(pushbutton3) == LOW && digitalRead(pushbutton4) == LOW && lastbuttonstate4 == HIGH)
{
digitalWrite (ledpin2,LOW);
digitalWrite(ledpin, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, HIGH);
}

}

First off, change the pinMode statements to INPUT_PULLUP. This should cure the bounce problem.

2nd, it looks like it'd be easier to select a 'state' with a switch/case statement. Do something like:

if (switch1Pressed){ laser*State* = 1; } if (switch2Pressed){ laser*State* = 2; // etc., etc.

Somewhere after that is a switch statement followed by several case:

Within each case: you set your outputs to the desired state. They'll stay that way 'til laserState changes.

OK, so you have cross-posted this enquiry to two different threads, so that is going to be a great nuisance for people trying to help you.

On the one hand, you need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output) from the IDE and click the icon. In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window.

It is inappropriate to attach it as a “.ino” file. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And that would also assume they are using a PC and have the IDE running on that PC.

But don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code”, it can be quite garbled and is always more difficult to read.

Also tidy up your blank space. Use blank lines only between functional blocks.

Now, next, your question - and your project - is pretty disorganised. That is not going to get you far. You need to decide - and then explain - what you want these buttons to do. Are they to be “radio buttons” where one button selects a mode and cancels all others, or do you want group “radio buttons” where each group selects a particular parameter of the display, or are they toggles where each switches on and off one particular function independent of all the others - or is it a mix of the last two?

You refer to patterns, but have not defined any.

If you cannot write a complete description of how you want your display elements to behave to show us, then I conclude you have no idea yourself and are going to get nowhere - fast! :astonished: Let’s see an ordered description please. :roll_eyes: