Skip to content

Instantly share code, notes, and snippets.

@ammist
Created March 31, 2015 04:20
Show Gist options
  • Save ammist/f7a9ae7a638e9e357cca to your computer and use it in GitHub Desktop.
Save ammist/f7a9ae7a638e9e357cca to your computer and use it in GitHub Desktop.
Basic Arduino Multiplexer code
/*
* Reading switches and tell to Serial.
*
*/
int numPins = 2;
int out[] = {4,5};
int in[] = {2,3};
void setup(){
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Serial.println("Initializing!");
Serial.println("Pins: ");
// make the pushbutton's pin an input:
for (int i=0; i < numPins; i++){
pinMode(in[i], INPUT);
pinMode(out[i], OUTPUT);
Serial.print("{");
Serial.print(in[i]);
Serial.print(" ");
Serial.print(out[i]);
Serial.println("}");
}
delay(100);
}
void loop(){
checkSwitch();
delay(10);
}
int checkSwitch(){
int val, row, column = 0;
for(int i =0; i<numPins; i++){
digitalWrite(out[i], LOW);
}
for(int i=0; i<numPins; i++){
row = i;
// get the row
digitalWrite(out[i], HIGH);
for(int j=0; j<numPins; j++){
val = digitalRead(in[j]);
if (val == HIGH){
column = j;
break;
}
}
if (column > 0){
break;
}
digitalWrite(out[i], LOW);
}
if (column > 0){
Serial.print("Pushed: {");
Serial.print(row);
Serial.print(" ");
Serial.print(column);
Serial.println("}");
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment