This is a following post to my previous post regarding using multiplexer
In addition to the reading from the 4067 multiplexer I would like to read another 3 or 4 analog inputs.
Here is the original code from the previous post using 4067 multiplexer:
//https://forum.arduino.cc/t/4067-multiplexer-code-question/1019084
//********************************************^************************************************
// XXXXXXXXXXXXXXXXX.ino
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================
// 1.04 22/08/04 Running code
//
//********************************************^************************************************
#define ENABLED true
#define DISABLED false
#define NUM_INPUTS 3
// s0 s1 s2 s3
const byte addressPin[] = {8, 9, 10, 11};
const byte muxChannel[NUM_INPUTS][4] =
{
{0, 0, 0, 0}, //channel 0
{1, 0, 0, 0}, //channel 1
{0, 1, 0, 0}, //channel 2
};
const byte heartbeatLED = 13;
//Mux in "SIG" pin
const byte SIG_pin = A0;
boolean sampleFlag = DISABLED;
byte muxAddress;
byte hysteresis = 20; //whatever is needed
int muxValue;
int lastValue[NUM_INPUTS];
//timing stuff
unsigned long heartbeatMillis;
unsigned long readMuxMillis;
unsigned long settlingMillis;
//********************************************^************************************************
void setup()
{
Serial.begin(115200);
pinMode(heartbeatLED, OUTPUT);
for (byte x = 0; x <= 3; x++)
{
pinMode(addressPin[x], OUTPUT);
digitalWrite(addressPin[x], LOW);
}
} //END of setup()
//********************************************^************************************************
void loop()
{
checkHeartbeatTIMER();
checkReadMuxTIMER();
checkSettlingTIMER();
//*********************************
// other non blocking code goes here
//*********************************
} //END of loop()
//********************************************^************************************************
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 >= 1000ul)
{
//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 >= 10ul)
{
//we are now finished with this analog settling TIMER
sampleFlag = DISABLED;
//read the stabilized analog
muxValue = analogRead(SIG_pin);
//has analog value changed more than the hysteresis amount ?
if (abs(lastValue[muxAddress] - muxValue) > hysteresis)
{
//update to the new value
lastValue[muxAddress] = muxValue;
//log the new value of the selected analog
switch (muxAddress)
{
//****************
case 0:
{
muxValue = map(muxValue, 0, 1023, -70, 6);
Serial.print("Value at channel ");
Serial.print(muxAddress);
Serial.print(" is : ");
Serial.println(muxValue);
}
break;
//****************
case 1:
{
muxValue = map(muxValue, 0, 1023, 10, 100);
Serial.print("Value at channel ");
Serial.print(muxAddress);
Serial.print(" is : ");
Serial.println(muxValue);
}
break;
//****************
case 2:
{
muxValue = map(muxValue, 0, 1023, 500, -50);
Serial.print("Value at channel ");
Serial.print(muxAddress);
Serial.print(" is : ");
Serial.println(muxValue);
}
break;
} //END of switch/case
}
//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()
//********************************************^************************************************
and here is my failure try to add non blocking reading from another 3 analog inputs in addition to the multiplexer:
#define ENABLED true
#define DISABLED false
#define NUM_INPUTS 5
// s0 s1 s2 s3
const byte addressPin[] = {8, 9, 10, 11};
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
};
// verify non blocking code//
const byte heartbeatLED = 13;
//Mux in "SIG" pin
const byte SIG_pin = A0;
//Others analog pin inputs
const byte otherSIG_pin[] = {A1, A2, A3};
int sigValue;
int lastValue_other[3];
//mapping values other analog inputs ch0 ch1 ch2
const int lowEnd_otherInputs[] = {-70, 10, 500};
const int highEnd_otherInputs[] = {6, 100, -50};
boolean sampleFlag = DISABLED;
byte muxAddress;
byte filter = 10; //whatever is needed
int muxValue;
int lastValue[NUM_INPUTS];
//mapping values ch0 ch1 ch2 ch3 ch4
const int lowEnd[] = {-70, 10, 500, 0, 0};
const int highEnd[] = {6, 100, -50, 1000, 1023};
//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();
otherAnalogInputs();
}
//// 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 >= 100ul)
{
//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 >= 10ul)
{
//we are now finished with this analog settling TIMER
sampleFlag = DISABLED;
//read the stabilized analog
muxValue = 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;
//log the new value of the selected analog
muxValue = map(muxValue, 0, 1023, lowEnd[muxAddress], highEnd[muxAddress]);
Serial.print("Value at channel ");
Serial.print(muxAddress);
Serial.print(" is : ");
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()
//********************************************^************************************************
void otherAnalogInputs () {
for (int i = 0; i < 3; i++){
if (millis() - readSigMillis >= 100ul)
{
readSigMillis = millis();
//read the stabilized analog
sigValue = analogRead(otherSIG_pin[i]);
//has analog value changed more than the filter amount ?
if (abs(lastValue_other[i] - sigValue) > filter)
{
//update to the new value
lastValue_other[i] = sigValue;
//log the new value of the selected analog
sigValue = map(sigValue, 0, 1023, lowEnd_otherInputs[i], highEnd_otherInputs[i]);
Serial.print("Value at channel ");
Serial.print(i+NUM_INPUTS);
Serial.print(" is : ");
Serial.println(sigValue);
}
}
}
}
Where I'm wrong?
Thanks