I need to connect multiple sensors to my Arduino Yun for the first time and have selected the original Mux Shield for this purpose:
I am using their Analog In example code, but want to modify it so the logic supports only 8 inputs instead of the maximum 48. I will only be connecting at most eight sensors to the board. Here is my sketch:
//Mux_Shield_AnalogIn_Example
//http://mayhewlabs.com/arduino-mux-shield
/*
This example shows how to read and store all 48 pins as analog inputs into arrays and print the results over serial.
Multiplexer pin inputs that do not have a voltage reading (i.e nothing connected) will have erratic values.
To simplify this code further, one might use nested for loops or function calls.
*/
//Give convenient names to the control pins
#define CONTROL0 5
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2
//Create arrays for data from the the MUXs
//See the Arduino Array Reference: http://www.arduino.cc/en/Reference/Array
int mux0array[8];
void setup()
{
//Set MUX control pins to output
pinMode(CONTROL0, OUTPUT);
pinMode(CONTROL1, OUTPUT);
pinMode(CONTROL2, OUTPUT);
pinMode(CONTROL3, OUTPUT);
//Open the serial port at 28800 bps
Serial.begin(28800);
}
void loop()
{
//This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
for (int i=0; i<8; i++)
{
//The following 4 commands set the correct logic for the control pins to select the desired input
//See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
//See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
digitalWrite(CONTROL0, (i&15)>>3);
digitalWrite(CONTROL1, (i&7)>>2);
digitalWrite(CONTROL2, (i&3)>>1);
digitalWrite(CONTROL3, (i&1));
//Read and store the input value at a location in the array
mux0array[i] = analogRead(0);
}
//The following lines are for printing out results of array0
Serial.print("mux0array: ");
for (int i=0; i<8; i++)
{
Serial.print(mux0array[i]);
Serial.print("-");
}
Serial.println(); //line feed
}
I am admittedly confused by the Bitwise AND Bitshift part of their example code, so I have not modified that part at all. Here is the compiler error I am seeing:
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino\CDC.cpp: In member function 'available':
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino\CDC.cpp:187:1: internal compiler error: Segmentation fault
}
^
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status
compilation terminated.
c:/program files/windowsapps/arduinollc.arduinoide_1.8.21.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed
collect2.exe: error: ld returned 1 exit status
Can someone tell me what this error means? I did some research on it and some posts mentioned it was related to a faulty version of the compiler. That being said, I am new to the Mux Shield and I am not sure if I have modified the sample code properly. The tech support at the manufacturer of the Mux Shield seems defunct, so I thought I would ask the question here.
I am seeing some strange behavior with my IDE (1.8.9 version, Windows Store 1.8.21.0).
The sketch listed above would not compile, but since you said it compiled for you on 1.8.9, I copied the text and created a new sketch exactly the same as the one above. It now compiles without issue. So I guess I will just use the copy for now.
My related question is about downsizing the array of 16 inputs to eight. I am not familiar with Bitshift and Bitwise AND operations. Is this for loop valid for an array of 8 inputs (reduced from 16)?
for (int i=0; i<8; i++)
{
//The following 4 commands set the correct logic for the control pins to select the desired input
//See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
//See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
digitalWrite(CONTROL0, (i&15)>>3);
digitalWrite(CONTROL1, (i&7)>>2);
digitalWrite(CONTROL2, (i&3)>>1);
digitalWrite(CONTROL3, (i&1));
//Read and store the input value at a location in the array
mux0array[i] = analogRead(0);
}
Looks to be a nonsense. For a start normally CONTROL0 will be the LEAST significant bit of the selection address and CONTROL3 should be the MOST significant bit. As the index i only goes from 0 to 7 then CONTROL3 should always be zero.
To get it the right way round I would use:-
digitalWrite(CONTROL0, i & 1);
digitalWrite(CONTROL1, (i & 2)>>1);
digitalWrite(CONTROL2, (i & 4)>>2);
digitalWrite(CONTROL3, 0);
I guessed you would not approve, Mike! But you are not an Arduino beginner. bitRead() and similar functions are designed to make the learning curve a little less steep, like stabilisers on a bicycle.
And what do you think it was like for me when I was learning machine language? No only did I have to work out what the logic and shift operations did I also had to work out where they would be useful. Back in those days there were no books that covered that sort of thing only the processor's data sheet which contained no examples of code at all.
Reminds me of coding in Z80 machine code on ZX81. No assembler, you had to do it all in hex. After a while, I memorised most of the Z80 instruction set and could count down in hex in my head from 0xFF for relative jumps. Later I got a BBC micro, as you know, and it had a 6502 assembler built-into the BASIC interpreter. What luxury!
There was an episode of Dr Who where the Dr was reading out the code that ran the universe in hex. It was real Z80 code, and the point where they found the Master had changed it was a jump to subroutine instruction. Often wonder how many people got that joke?