Can someone show me how to integrate the 4051 Demultiplexing code into the analog sensor sketch I have written. I can't find any detailed documentation on how to do this. I have referenced the 4051 learning section, read some posts, understand the pin wiring, but can't quite figure out how to integrate the code. The sketch I have written has only 6 sensors in it (because of the arduino pin limitations), and I will eventually expand that number to 24 once i can get the 4051 demultiplexing working. I just don't see how to merge the two sketches together.
Here is the 4051 sketch:
/*
* codeexample for useing a 4051 * analog multiplexer / demultiplexer
* by david c. and tomek n.* for k3 / malm? h?gskola
*
* edited by Ross R.
*/
int r0 = 0; //value of select pin at the 4051 (s0)
int r1 = 0; //value of select pin at the 4051 (s1)
int r2 = 0; //value of select pin at the 4051 (s2)
int count = 0; //which y pin we are selecting
void setup(){
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
}
void loop () {
for (count=0; count<=7; count++) {
// select the bit
r0 = bitRead(count,0); // use this with arduino 0013 (and newer versions)
r1 = bitRead(count,1); // use this with arduino 0013 (and newer versions)
r2 = bitRead(count,2); // use this with arduino 0013 (and newer versions)
//r0 = count & 0x01; // old version of setting the bits
//r1 = (count>>1) & 0x01; // old version of setting the bits
//r2 = (count>>2) & 0x01; // old version of setting the bits
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
//Either read or write the multiplexed pin here
}
}
And here is my sketch:
int IRpin = 0;
int IRpin1 = 1;
int IRpin2 = 2;
int IRpin3 = 3;
int IRpin4 = 4;
int IRpin5 = 5;
int ledpin = 8;
int ledpin1 = 9;
int ledpin2 = 10;
int ledpin3 = 7;
int ledpin4 = 6;
int ledpin5 = 5;
void setup() {
Serial.begin(9600);
pinMode(ledpin, OUTPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(ledpin3, OUTPUT);
pinMode(ledpin4, OUTPUT);
pinMode(ledpin5, OUTPUT);
}
void loop() {
digitalWrite(ledpin, HIGH);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, LOW);
digitalWrite(ledpin5, LOW);
delay(1000);
float volts = analogRead(IRpin)*0.0048828125;
float distance = 27*pow(volts, -1.15);
float Inches = (distance / 2.54);
if (Inches <= 6){
Serial.print("MISS...The ball stopped in ROW1 ");
Serial.println( Inches );
}
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, LOW);
digitalWrite(ledpin5, LOW);
delay(1000);
float volts1 = analogRead(IRpin1)*0.0048828125;
float distance1 = 27*pow(volts1, -1.15);
float Inches1 = (distance1 / 2.54);
if (Inches1 <= 6){
Serial.print("MISS...The ball stopped in ROW2 ");
Serial.println( Inches1 );
}
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, HIGH);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, LOW);
digitalWrite(ledpin5, LOW);
delay(1000);
float volts2 = analogRead(IRpin2)*0.0048828125;
float distance2 = 27*pow(volts2, -1.15);
float Inches2 = (distance2 / 2.54);
if (Inches2 <= 6){
Serial.print("MISS...The ball stopped in ROW3 ");
Serial.println( Inches2 );
}
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
digitalWrite(ledpin3, HIGH);
digitalWrite(ledpin4, LOW);
digitalWrite(ledpin5, LOW);
delay(1000);
float volts3 = analogRead(IRpin3)*0.0048828125;
float distance3 = 27*pow(volts3, -1.15);
float Inches3 = (distance3 / 2.54);
if (Inches3 <= 23){
Serial.print("MISS...The ball stopped in Column 3 ");
Serial.println( Inches3 );
}
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, HIGH);
digitalWrite(ledpin5, LOW);
delay(1000);
float volts4 = analogRead(IRpin4)*0.0048828125;
float distance4 = 27*pow(volts4, -1.15);
float Inches4 = (distance4 / 2.54);
if (Inches4 <= 6){
Serial.print("MISS...The ball stopped in Column 2 ");
Serial.println( Inches4 );
}
digitalWrite(ledpin, LOW);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
digitalWrite(ledpin3, LOW);
digitalWrite(ledpin4, LOW);
digitalWrite(ledpin5, HIGH);
delay(1000);
float volts5 = analogRead(IRpin5)*0.0048828125;
float distance5 = 27*pow(volts5, -1.15);
float Inches5 = (distance5 / 2.54);
if (Inches5 <= 6){
Serial.print("MISS...The ball stopped in Column 1 ");
Serial.println( Inches5 );
}
}
THANKS FOR THE HELP IN ADVANCE!
-Aaron