I need help adding the option to read other analog inputs apart from the multiplexer.
I try to go over a7 example at post #64 but I got stuck.
I don't understand how to make the code checking the other analog inputs after go over all the multiplexer inputs.
I started to edit the code and this is where I have got:
#define ENABLED true
#define DISABLED false
#define N_MUX_INPUTS 3
# define N_OTHER_INPUTS 1
# define N_INPUTS (N_MUX_INPUTS + N_OTHER_INPUTS)
// s0 s1 s2 s3
const byte addressPin[] = {8, 9, 10, 11};
const byte muxChannel[N_MUX_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;
//Others analog pin inputs
const byte otherSIG_pin[N_OTHER_INPUTS] = {A1};
int sigValue;
boolean sampleFlag = DISABLED;
byte muxAddress;
byte filter = 1; //whatever is needed
int muxValue;
int lastValue[N_INPUTS];
bool isOn[N_INPUTS];
bool isOnPre[N_INPUTS];
////mapping values ch0 ch1 ch2 ch3 ch4
//const int lowEnd[] = {0, 0, 500, 0, 0};
//const int highEnd[] = {1023, 1023, -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();
}
//// 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 = 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] >= 2) // should mux channel be on?
{
if (isOn[muxAddress] == 0) // was it off? tell us it went on
{
isOn[muxAddress] = 1; // remember it went on
Serial.print("ch");
Serial.print(muxAddress);
Serial.print("on "); // and say so
Serial.println(isOn[muxAddress]);
}
}
else // mux channel should be off
{
if (isOn[muxAddress] == 1) // was it on? tell us it went off
{
isOn[muxAddress] = 0; // remember it is off
Serial.print("ch");
Serial.print(muxAddress);
Serial.print("on "); // and say so
Serial.println(isOn[muxAddress]);
}
}
//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_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 > N_MUX_INPUTS - 1)
{
//back to the first address
muxAddress = 0;
}
}
} //END of checkSettlingTIMER()
//********************************************^************************************************