- Fri Jan 19, 2018 1:49 pm
#197970
Hi all,
I purchased the Mayhew Labs Mux Shield II (https://www.sparkfun.com/products/11723) and am having a weird issue with it. I have it connected to an Elegoo Mega2560 R3 (Arduino Mega) and am testing it with 16 analog inputs as a proof of concept so I can eventually use all 48 analog inputs.
I have 16 photoresistors, each of which are in series with a 4.7k ohm resistor. I have each pin in the I/O1 row of the Mux Shield reading the voltage across a 4.7k ohm resistor. My code (below) takes 10 readings from each pin and averages them to create a baseline value. Then, in the loop function, the voltage across each resistor is read and then subtracted from the baseline for that specific pin. The program outputs the difference from the baseline for all 16 pins.
The code works perfectly for about 45 seconds. The pin voltages hover around the baseline (some noise exists, causing them to fluctuate slightly). The voltages will also increase or decrease when I cover the sensors or shine a light on them. However, after about 45 seconds, the voltage readings get stuck at a certain value and no longer increase or decrease due to light input. The only solution I've found is to completely remove the USB cable from my laptop, then reconnect it and reupload the code, which causes it to work for another 45 seconds or so.
To ensure it is the multiplexer and not my code, I wired one photoresistor + resistor to analog pin A15. This pin hovers around its baseline and consistently responds to light input no matter how long the code has been running.
Does anyone have any idea why this is occurring?
Code:
I purchased the Mayhew Labs Mux Shield II (https://www.sparkfun.com/products/11723) and am having a weird issue with it. I have it connected to an Elegoo Mega2560 R3 (Arduino Mega) and am testing it with 16 analog inputs as a proof of concept so I can eventually use all 48 analog inputs.
I have 16 photoresistors, each of which are in series with a 4.7k ohm resistor. I have each pin in the I/O1 row of the Mux Shield reading the voltage across a 4.7k ohm resistor. My code (below) takes 10 readings from each pin and averages them to create a baseline value. Then, in the loop function, the voltage across each resistor is read and then subtracted from the baseline for that specific pin. The program outputs the difference from the baseline for all 16 pins.
The code works perfectly for about 45 seconds. The pin voltages hover around the baseline (some noise exists, causing them to fluctuate slightly). The voltages will also increase or decrease when I cover the sensors or shine a light on them. However, after about 45 seconds, the voltage readings get stuck at a certain value and no longer increase or decrease due to light input. The only solution I've found is to completely remove the USB cable from my laptop, then reconnect it and reupload the code, which causes it to work for another 45 seconds or so.
To ensure it is the multiplexer and not my code, I wired one photoresistor + resistor to analog pin A15. This pin hovers around its baseline and consistently responds to light input no matter how long the code has been running.
Does anyone have any idea why this is occurring?
Code:
Code: Select all
#include <MuxShield.h>
//Initialize the Mux Shield
MuxShield muxShield;
int baseline1[16];
int baseline2[16];
int baseline3[16];
int baseline15;
//Arrays to store analog values
int IO1AnalogVals[16];
int IO2AnalogVals[16];
int IO3AnalogVals[16];
void setup()
{
//Set I/O 1, I/O 2, and I/O 3 as analog inputs
muxShield.setMode(1,ANALOG_IN);
muxShield.setMode(2,ANALOG_IN);
muxShield.setMode(3,ANALOG_IN);
for (int i=0; i<16; i++) {
int temp1 = 0;
int temp2 = 0;
int temp3 = 0;
int temp15 = 0;
for(int j = 0; j < 10; j++) {
//Analog read on all 16 inputs on IO1, IO2, and IO3
temp1 += muxShield.analogReadMS(1,i);
temp2 += muxShield.analogReadMS(2,i);
temp3 += muxShield.analogReadMS(3,i);
temp15 += analogRead(15);
}
temp1 /= 10;
temp2 /= 10;
temp3 /= 10;
temp15 /= 10;
baseline1[i] = temp1;
baseline2[i] = temp2;
baseline3[i] = temp3;
baseline15 = temp15;
}
Serial.begin(9600);
}
void loop()
{
for (int i=0; i<16; i++)
{
//Analog read on all 16 inputs on IO1, IO2, and IO3
int raw1 = muxShield.analogReadMS(1,i);
int raw2 = muxShield.analogReadMS(2,i);
int raw3 = muxShield.analogReadMS(3,i);
IO1AnalogVals[i] = raw1 - baseline1[i];
IO2AnalogVals[i] = raw2 - baseline2[i];
IO3AnalogVals[i] = raw3 - baseline3[i];
}
int a15 = analogRead(15) - baseline15;
/*for (int i=0; i<16; i++)
{
Serial.print(IO1AnalogVals[i]);
Serial.print("\t");
}
Serial.println("");*/
/*//Print IO2 values for inspection
for (int i=0; i<16; i++)
{
Serial.print(IO2AnalogVals[i]);
Serial.print("\t");
}
Serial.println("");*/
for (int i=0; i<16; i++)
{
Serial.print(IO1AnalogVals[i]);
Serial.print("\t");
}
Serial.println(a15);
Serial.println("----------------------------------------------------------------------------------------");
delay (1000);
}