Dear all,
I have been trying to learn about the CD4051 multiplexer (mux?) and have read an article by Nick Gammon on his blog, though I admit I cannot fathom out a few lines of his code, and I have listed the lines in question below.
For completeness the full code is at the end of this post.
The lines I have problems with are here:
int readSensor (const byte which)
{
// select correct MUX channel
digitalWrite (addressA, (which & 1) ? HIGH : LOW); // low-order bit
digitalWrite (addressB, (which & 2) ? HIGH : LOW);
digitalWrite (addressC, (which & 4) ? HIGH : LOW); // high-order bit
// now read the sensor
return analogRead (sensor);
} // end of readSensor
In particular, what does digitalWrite (addressA, (which & 1) ? HIGH : LOW); // low-order bit do? The Arduino reference does not appear to cover this.
Please will someone explain in plain english what is happening here, and perhaps why.
GM
Full code below:
// Example of using the 74HC4051 multiplexer/demultiplexer
// Author: Nick Gammon
// Date: 14 March 2013
const byte sensor = A0; // where the multiplexer in/out port is connected
// the multiplexer address select lines (A/B/C)
const byte addressA = 6; // low-order bit
const byte addressB = 5;
const byte addressC = 4; // high-order bit
void setup ()
{
Serial.begin (115200);
Serial.println ("Starting multiplexer test ...");
pinMode (addressA, OUTPUT);
pinMode (addressB, OUTPUT);
pinMode (addressC, OUTPUT);
} // end of setup
int readSensor (const byte which)
{
// select correct MUX channel
digitalWrite (addressA, (which & 1) ? HIGH : LOW); // low-order bit
digitalWrite (addressB, (which & 2) ? HIGH : LOW);
digitalWrite (addressC, (which & 4) ? HIGH : LOW); // high-order bit
// now read the sensor
return analogRead (sensor);
} // end of readSensor
void loop ()
{
// show all 8 sensor readings
for (byte i = 0; i < 7; i++)
{
Serial.print ("Sensor ");
Serial.print (i);
Serial.print (" reads: ");
Serial.println (readSensor (i));
}
delay (1000);
} // end of loop