When no sensors are pressed I have flactuating of reading around 15 numbers jumping around (between 0 to 15 ).
Yesterday I was facing the same but after a while it stopped and reading was stable.
What can It be? a change in temperature can make it?
I am wiring my arduino as follow:
I'm reading with the following code:
const byte heartbeatLED = 13;
unsigned long heartbeatMillis;
unsigned long previousMillis = 0;
unsigned long interval = 20; //reading every x ms
//mux pin for reading//
int s0 = 2;
int s1 = 3;
int s2 = 4;
int s3 = 5;
const int NUMSENSORS = 14; // change this variable to set the number of sensors
int SIG_pin = A0;
int previousValue[NUMSENSORS] = {0};
bool isOn[NUMSENSORS] = {false};
int pressThreshold = 22; // Change this variable to set the pressThreshold value
byte filter = 5; //whatever is needed
void setup() {
Serial.begin(115200);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
readSensors();
checkHeartbeatTIMER();
}
}
void readSensors() {
// Loop through all the sensors
for (int i = 0; i < NUMSENSORS; i++) {
// Set the multiplexer to select the correct channel
digitalWrite(s0, (i & 1));
digitalWrite(s1, (i & 2) >> 1);
digitalWrite(s2, (i & 4) >> 2);
digitalWrite(s3, (i & 8) >> 3);
// Read the sensor value
int sensorValue = 1023 - analogRead(SIG_pin);
// Check if the sensor value has changed
if (sensorValue != previousValue[i]) {
// Check if the change is greater than the filter value
if (abs(previousValue[i] - sensorValue) > filter) {
// Update the previous value
previousValue[i] = sensorValue;
// Print the sensor value
Serial.print("value_ch");
Serial.print(i);
Serial.print(" ");
Serial.println(sensorValue);
// Check if the sensor value is greater than the press threshold
if (sensorValue > pressThreshold) {
// Check if the sensor is not already on
if (!isOn[i]) {
// Update the on/off state
isOn[i] = true;
// Print the on/off state
Serial.print("ch");
Serial.print(i);
Serial.print("on ");
Serial.println(isOn[i]);
}
}
else {
if (isOn[i]) {
isOn[i] = false;
Serial.print("ch");
Serial.print(i);
Serial.print("on ");
Serial.println(isOn[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()