I was able to setup a multiplexer to serial print each indiviual button just fine.
The next step I need to do is now making a logical toggle switch using these same buttons. I have an if statement that checks the state of the button array and attempts to invert that state.
#inputi being the way to detect which button is pressed.
if ( inputi < 1000 && inputStateArray[i] == 0 ){
inputStateArray[i] = 1
}
if ( inputi < 1000 && inputStateArray[i] == 1 ){
inputStateArray[i] = 0
}
But it doesn't work. Any ideas?
Full code below:
//Mux control pins
int s0 = D0;
int s1 = D1;
int s2 = D2;
int s3 = D3;
int Duct1 = D4;
int Duct2 = D5;
//Define Array of 8 items to set state to use for control if button is pressed
byte inputStateArray[] = {0, 0, 0, 0, 0, 0, 0};
//Mux in "SIG" pin
int SIG_pin = A0;
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Serial.begin(9600);
}
void loop(){
//Loop through and read first 7 values (Y0-Y7)
//Reports back Value at channel 6 is: 346
for(int i = 0; i < 7; i ++){
//Serial.println(readMux(i));
int inputi = readMux(i);
// if input of i is pressed run
if ( inputi < 1000 && inputStateArray[i] == 0 ){
Serial.print(i);Serial.print(":");
Serial.println(inputStateArray[i]);
inputStateArray[i] = 1;
Serial.print("Button ");Serial.print(i);Serial.print(" state value is: ");
Serial.println(inputStateArray[i]);
}
if ( inputi < 1000 && inputStateArray[i] == 1 ){
Serial.print(i);Serial.print(":");
Serial.println(inputStateArray[i]);
inputStateArray[i] = 0;
Serial.print("Button ");Serial.print(i);Serial.print(" state value is: ");
Serial.println(inputStateArray[i]);
}
delay(100);
}
// Check Array Items and turn on Duct
//Duct 1
if (inputStateArray[0] = 0){
digitalWrite(Duct1, LOW);
}
if (inputStateArray[0] = 1){
digitalWrite(Duct1, HIGH);
}
//Duct 2
if (inputStateArray[1] = 0){
digitalWrite(Duct1, LOW);
}
if (inputStateArray[1] = 1){
digitalWrite(Duct1, HIGH);
}
}
int readMux(int channel){
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4]={ {0,0,0,0},
{1,0,0,0},
{0,1,0,0},
{1,1,0,0},
{0,0,1,0},
{1,0,1,0},
{0,1,1,0},
{1,1,1,0},
{0,0,0,1},
{1,0,0,1},
{0,1,0,1},
{1,1,0,1},
{0,0,1,1},
{1,0,1,1},
{0,1,1,1},
{1,1,1,1} };
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin); //return the value
return val;
}