My project is progressing quite well, but I am having some issues reading button presses through my MUX.
The Goal
Cycle through twelve buttons and twelve LEDs. When a button is pressed, light up the corresponding LED. I have one MUX for the LEDs which is working great thanks to the stack exchange community. My button MUX is the issue.
The Symptom
I have one button hooked up to the MUX. When it is not pressed, nothing lights up - that's right. But when it is pressed, every LED lights up as it is cycled through. It is as if pressing this button for one of the MUX pins is sending a HIGH signal to all of the MUX pins.
The Pictures
^^ You can see the big honking button in the bottom right. It is connected to 5V and then the MUX pin.
^^ I have two multiplexers. The one on the left is for the LEDs and the one on the right is for the buttons (of which I currently [ha!] have only one).
The Code
// set vars for all the pins
int led_input = 0;
int led_ctrl_1 = 1;
int led_ctrl_2 = 2;
int led_ctrl_3 = 3;
int led_ctrl_4 = 4;
int btn_input = 5;
int btn_ctrl_1 = 6;
int btn_ctrl_2 = 7;
int btn_ctrl_3 = 8;
int btn_ctrl_4 = 9;
// how many MUX pins?
int players = 12;
// function to take player number and convert to control pins
void ledWrite(int led, boolean val){
if(!val){
digitalWrite(led_input, LOW);
}else{
if((led - 8) >= 0){
led = led - 8;
digitalWrite(led_ctrl_4, HIGH);
}else{
digitalWrite(led_ctrl_4, LOW);
}
if((led - 4) >= 0){
led = led - 4;
digitalWrite(led_ctrl_3, HIGH);
}else{
digitalWrite(led_ctrl_3, LOW);
}
if((led - 2) >= 0){
led = led - 2;
digitalWrite(led_ctrl_2, HIGH);
}else{
digitalWrite(led_ctrl_2, LOW);
}
if((led - 1) >= 0){
led = led - 1;
digitalWrite(led_ctrl_1, HIGH);
}else{
digitalWrite(led_ctrl_1, LOW);
}
digitalWrite(led_input, HIGH);
}
}
// function to take player number and convert to control pins
int btnRead(int btn){
if((btn - 8) >= 0){
btn = btn - 8;
digitalWrite(btn_ctrl_4, HIGH);
}else{
digitalWrite(btn_ctrl_4, LOW);
}
if((btn - 4) >= 0){
btn = btn - 4;
digitalWrite(btn_ctrl_3, HIGH);
}else{
digitalWrite(btn_ctrl_3, LOW);
}
if((btn - 2) >= 0){
btn = btn - 2;
digitalWrite(btn_ctrl_2, HIGH);
}else{
digitalWrite(btn_ctrl_2, LOW);
}
if((btn - 1) >= 0){
btn = btn - 1;
digitalWrite(btn_ctrl_1, HIGH);
}else{
digitalWrite(btn_ctrl_1, LOW);
}
return digitalRead(btn_input);
}
void setup(){
pinMode(led_input, OUTPUT);
pinMode(led_ctrl_1, OUTPUT);
pinMode(led_ctrl_2, OUTPUT);
pinMode(led_ctrl_3, OUTPUT);
pinMode(led_ctrl_4, OUTPUT);
pinMode(btn_input, INPUT);
pinMode(btn_ctrl_1, OUTPUT);
pinMode(btn_ctrl_2, OUTPUT);
pinMode(btn_ctrl_3, OUTPUT);
pinMode(btn_ctrl_4, OUTPUT);
}
void loop(){
for(int x = 1; x <= players; x++){
if(btnRead(x) == HIGH){
ledWrite(x, true);
delay(100);
ledWrite(x, false);
}
}
}
I know this is a lot to trudge through, and I honestly appreciate any help you are willing to offer. Thanks in advance!