Hi there,
I have a code that is reading 15 sensor using 4067 multiplexer.
When I read only 1 sensor or 2 sensor the data printed into the serial seems smooth and fast. the moment I added more input it seems that the data printed to the serial is lagged and not smooth.
can someone see a problem with the code itself? what can cause it and how can I fix it?
#define ENABLED true
#define DISABLED false
#define NUM_INPUTS 15
// s0 s1 s2 s3
const byte addressPin[] = {2, 3, 4, 5};
const byte muxChannel[NUM_INPUTS][4] =
{
{0, 0, 0, 0}, //channel 0
{1, 0, 0, 0}, //channel 1
{0, 1, 0, 0}, //channel 2
{1, 1, 0, 0}, //channel 3
{0, 0, 1, 0}, //channel 4
{1, 0, 1, 0}, //channel 5
{0, 1, 1, 0}, //channel 6
{1, 1, 1, 0}, //channel 7
{0, 0, 0, 1}, //channel 8
{1, 0, 0, 1}, //channel 9
{0, 1, 0, 1}, //cahennl 10
{1, 1, 0, 1}, //channel 11
{0, 0, 1, 1}, //channel 12
{1, 0, 1, 1}, //channel 13
{0, 1, 1, 1}, //channel 14
//{1, 1, 1, 1} //channel 15
};
// verify non blocking code//
const byte heartbeatLED = 13;
//Mux in "SIG" pin
const byte SIG_pin = A0;
int sigValue;
boolean sampleFlag = DISABLED;
byte muxAddress;
byte filter = 10; //whatever is needed
//sensor is pressed threshold //
const byte pressThreshold = 30;
int muxValue;
int lastValue[NUM_INPUTS];
bool muxIsOn[NUM_INPUTS];
bool muxIsOnPre[NUM_INPUTS];
//timing stuff//
unsigned long heartbeatMillis;
unsigned long readMuxMillis;
unsigned long settlingMillis;
unsigned long readSigMillis;
//********************************************^************************************************
void setup()
{
Serial.begin(115200);
pinMode(heartbeatLED, OUTPUT);
for (byte x = 0; x <= 3; x++)
{
pinMode(addressPin[x], OUTPUT);
digitalWrite(addressPin[x], LOW);
}
}
void loop()
{
checkHeartbeatTIMER();
checkReadMuxTIMER();
checkSettlingTIMER();
}
//// functions ////
void checkHeartbeatTIMER()
{
//********************************* heartbeat TIMER
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatMillis >= 500ul)
{
//restart this TIMER
heartbeatMillis = millis();
//toggle the heartbeatLED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
} //END of checkHeartbeatTIMER()
void checkReadMuxTIMER()
{
//********************************* readMux TIMER
//is it time to read the next mux analog ?
if (millis() - readMuxMillis >= 5ul)
{
//restart this TIMER
readMuxMillis = millis();
//send the new address to the CD4067 mux
for (byte i = 0; i < 4; i ++)
{
digitalWrite(addressPin[i], muxChannel[muxAddress][i]);
}
//enable the analog settling TIMER
sampleFlag = ENABLED;
//restart the analog settling TIMER
settlingMillis = millis();
}
} //END of checkReadMuxTIMER()
void checkSettlingTIMER()
{
//********************************* settling TIMER
//if this TIMER is enabled, has this it expired ?
if (sampleFlag == ENABLED && millis() - settlingMillis >= 5ul)
{
//we are now finished with this analog settling TIMER
sampleFlag = DISABLED;
//read the stabilized analog
muxValue = 1023 - analogRead(SIG_pin);
//has analog value changed more than the filter amount ?
if (abs(lastValue[muxAddress] - muxValue) > filter)
{
//update to the new value
lastValue[muxAddress] = muxValue;
// /* new feature removed for now
if (lastValue[muxAddress] >= pressThreshold) // should mux channel be on?
{
if (muxIsOn[muxAddress] == 0) // was it off? tell us it went on
{
muxIsOn[muxAddress] = 1; // remember it went on
Serial.print("ch");
Serial.print(muxAddress);
Serial.print("on "); // and say so
Serial.println(muxIsOn[muxAddress]);
}
}
else // mux channel should be off
{
if (muxIsOn[muxAddress] == 1) // was it on? tell us it went off
{
muxIsOn[muxAddress] = 0; // remember it is off
Serial.print("ch");
Serial.print(muxAddress);
Serial.print("on "); // and say so
Serial.println(muxIsOn[muxAddress]);
}
}
Serial.print("value_ch");
Serial.print(muxAddress);
Serial.print(" ");
Serial.println(muxValue);
}
//prepare for the new/next mux address
muxAddress++;
//have we read all the inputs ?
if (muxAddress > NUM_INPUTS - 1)
{
//back to the first address
muxAddress = 0;
}
}
} //END of checkSettlingTIMER()
//********************************************^************************************************
here is the schematics:
