Edit: I fixed original errors in the code I used to make the sketch posted below.
Edit: Also, you never answered, or I missed your answer : What is the roll of the settling timer? What do you think is "settling" between, it look like, setting the multiplexer address pins and the analogRead () of the mux output?
The entire "new feature" section can be commented out easily. Use /* before and */ after the big if statement that is the new feature.
Then see if your code does what you want, which you have asserted is not the case, so fix it or tell us more about what, with the new feature removed, the code at that point wasn't working.
I do not see a way where adding the new feature would break the old code. As you have said, the old code was already broken, so you need to say more about that and fix it.
#define ENABLED true
#define DISABLED false
#define NUM_INPUTS 2
// 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;
boolean sampleFlag = DISABLED;
byte muxAddress;
byte filter = 10; //whatever is needed
int muxValue;
int lastValue[NUM_INPUTS];
bool muxIsOn[NUM_INPUTS];
bool muxIsOnPre[NUM_INPUTS];
//mapping values ch0 ch1 ch2 ch3 ch4
const int lowEnd[] = {0, 10, 500, 0, 0};
const int highEnd[] = {1023, 100, -50, 1000, 1023};
//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);
}
}
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 >= 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;
/* new feature removed for now
if (lastValue[muxAddress] >= 5) // 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("channel ");
Serial.print(muxAddress);
Serial.println(" Is: ON (1)"); // and say so
}
}
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("channel ");
Serial.print(muxAddress);
Serial.print(" Is: OFF (0)"); // and say so
}
}
new feature removed for now */
//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()
a7