I have the following code which should read the value from three different sensors and print in new line each sensor value
#define NUM_INPUTS 3
const byte SIG_pins[] = { A0, A1, A2 };
int heartbeatLED = 13;
const byte filter = 2; //adjust as needed
const byte pressThreshold = 10;
int lastValue[NUM_INPUTS];
bool muxIsOn[NUM_INPUTS];
//timing variables
unsigned long previousMillis = 0;
unsigned long heartbeatMillis;
unsigned long interval = 20;
//********************************************^************************************************
void setup() {
Serial.begin(115200);
pinMode(heartbeatLED, OUTPUT);
for (byte i = 0; i < NUM_INPUTS; i++) {
pinMode(SIG_pins[i], INPUT);
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
readValues();
checkHeartbeatTIMER();
}
}
//// functions ////
void readValues() {
for (byte i = 0; i < NUM_INPUTS; i++) {
//read the stabilized analog
int SensorValue = 1023 - analogRead(SIG_pins[i]);
//has analog value changed more than the filter amount ?
if (abs(lastValue[i] - SensorValue) > filter) {
if (lastValue[i] != SensorValue) {
//update to the new value
lastValue[i] = SensorValue;
Serial.print("sensor");
Serial.print(i + 1);
Serial.print(" ");
Serial.println(SensorValue);
// /* new feature removed for now
if (SensorValue >= pressThreshold) // should mux channel be on?
{
if (muxIsOn[i] == 0) // was it off? tell us it went on
{
muxIsOn[i] = 1; // remember it went on
Serial.print("ch");
Serial.print(i + 1);
Serial.print("on "); // and say so
Serial.println(muxIsOn[i]);
}
} else // mux channel should be off
{
if (muxIsOn[i] == 1) // was it on? tell us it went off
{
muxIsOn[i] = 0; // remember it is off
Serial.print("ch");
Serial.print(i + 1);
Serial.print("on "); // and say so
Serial.println(muxIsOn[i]);
}
}
}
}
}
}
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()
The printing I'm getting is something like this which is not what I expected:
How can I fix it? what I'm doing wrong?