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);
}
}
