const int injPins[4] = {42, 44, 46, 48}; // S-pins to Arduino pins: S0, S1, S2, S3;
const int gndPins[4] = {43, 45, 47, 49}; // S-pins to Arduino pins: S0, S1, S2, S3;
const int msr1Pins[4] = {28, 30, 32, 34}; // S-pins to Arduino pins: S0, S1, S2, S3;
const int msr2Pins[4] = {29, 31, 33, 35}; // S-pins to Arduino pins: S0, S1, S2, S3;
int E1 = 40;
int E2 = 41;
int E3 = 26;
int E4 = 27;
int nelet = 16;
int flag;
void setup(void)
{
analogReadResolution(12);
Serial.begin(9600);
for (int i=0; i<4; i++)
{
pinMode(injPins[i],OUTPUT);
pinMode(gndPins[i],OUTPUT);
pinMode(msr1Pins[i],OUTPUT);
pinMode(msr2Pins[i],OUTPUT);
}
}
void loop(void)
{
int16_t ad0;
int cont = 0;
for (int i=0; i<nelet; i++)
{
ground(i);
if (i+1==nelet)
{
injection(0);
flag = 1;
}
else
{
injection(i+1);
}
for (int j=0; j<nelet; j++) {
if (i==0 && j==0)
{
Serial.println("Meas");
}
if (j==i
-1 || j==i || j==i+1)
{
continue;
}
if (i==0 && j==nelet
-1)
{
continue;
}
if (flag == 1)
{
flag = 0;
continue;
}
measure1(j);
if (j+1==nelet)
{
measure2(0);
}
else
{
measure2(j+1);
}
digitalWrite(E1, 0);
digitalWrite(E2, 0);
digitalWrite(E3, 0);
digitalWrite(E4, 0);
ad0 = analogRead(A0);
delay(60);
sprintf(str, "A%dB", ad0);
Serial.println(str); //%dN%dO%dP%dQ
digitalWrite(E1, 1);
digitalWrite(E2, 1);
digitalWrite(E3, 1);
digitalWrite(E4, 1);
}
}
}
// The selectMuxPin function sets the S0, S1, and S2 pins to select the give pin
void ground(byte pin)
{
if (pin > 15) return; // Exit if pin is out of scope
for (int i=0; i<4; i++)
{
if (pin & (1<<i))
digitalWrite(injPins[i], HIGH);
else
digitalWrite(injPins[i], LOW);
}
}
// The selectMuxPin function sets the S0, S1, and S2 pins to select the give pin
void injection(byte pin)
{
if (pin > 15) return; // Exit if pin is out of scope
for (int i=0; i<4; i++)
{
if (pin & (1<<i))
digitalWrite(gndPins[i], HIGH);
else
digitalWrite(gndPins[i], LOW);
}
}
// The selectMuxPin function sets the S0, S1, and S2 pins to select the give pin
void measure1(byte pin)
{
if (pin > 15) return; // Exit if pin is out of scope
for (int i=0; i<4; i++)
{
if (pin & (1<<i))
digitalWrite(msr1Pins[i], HIGH);
else
digitalWrite(msr1Pins[i], LOW);
}
}
void measure2(byte pin)
{
if (pin > 15) return;
for (int i=0; i<4; i++)
{
if (pin & (1<<i))
digitalWrite(msr2Pins[i], HIGH);
else
digitalWrite(msr2Pins[i], LOW);
}
}
The code cycles through 208 measurements. Although I know there is some noise in the measurements, the second cycle appears as It should be. The first cycle, that begins soon after the serial com opens, appears these spikes. Another thing I notice is that when I only measure the analog signal from another arduino, the signal appears normal.