Pressure Sensor Averages

Hello,
So I am using a Pressure Sensor from Sparkfun (SEN-09375) that I have glued with epoxy to a metal plate that will be mounted on a wall to function as a momentary switch when pressure over a threshold is reached.

The problem is the mounting method I used isn't completely stable so when the sensor is pressed multiple times the base number fluctuates. How would I calculate the average from the sensor, then determine and change what the threshold needs to be accordingly lets say 10 over the base amount.

Here is the code I have currently. I tried to implement the smoothing example to do this but had no success.

Any advice would be great! Thanks!


int threshold1 = 745;
int threshold2 = 100;
int threshold3 = 100;
int threshold4 = 100;
int threshold5 = 100;
int threshold6 = 100;

int effect1Last = 0;
int effect2Last = 0;
int effect3Last = 0;
int effect4Last = 0;
int effect5Last = 0;
int effect6Last = 0;

void setup()
{
// Start serial at 9600 baud
Serial.begin(9600);

}

void loop()
{
// Read the input on analog pins:
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
int sensorValue4 = analogRead(A3);
int sensorValue5 = analogRead(A4);
int sensorValue6 = analogRead(A5);
{

//Instrument 1 Control; 11 = on, 10 = off
if (!effect1Last && sensorValue1 > threshold1) {
effect1Last = 1;
Serial.println(11);
} else if(effect1Last && sensorValue1 <= threshold1) {
effect1Last = 0;
Serial.println(10);
//Instrument 2 Control; 21 = on, 20 = off
if (!effect2Last && sensorValue2 > threshold2) {
effect2Last = 1;
Serial.println(21);
} else if(effect2Last && sensorValue2 <= threshold2) {
effect2Last = 0;
Serial.println(20);
}

//Instrument 3 Control; 31 = on, 30 = off
if (!effect3Last && sensorValue3 > threshold3) {
effect3Last = 1;
Serial.println(31);
} else if(effect3Last && sensorValue3 <= threshold3) {
effect3Last = 0;
Serial.println(30);
}

//Instrument 4 Control; 41 = on, 40 = off
if (!effect4Last && sensorValue4 > threshold4) {
effect4Last = 1;
Serial.println(41);
} else if(effect4Last && sensorValue4 <= threshold4) {
effect4Last = 0;
Serial.println(40);
}

//Instrument 5 Control; 51 = on, 50 = off
if (!effect5Last && sensorValue5 > threshold5) {
effect5Last = 1;
Serial.println(51);
} else if(effect5Last && sensorValue5 <= threshold5) {
effect5Last = 0;
Serial.println(50);
}

//Instrument 6 Control; 61 = on, 60 = off
if (!effect6Last && sensorValue6 > threshold6) {
effect6Last = 1;
Serial.println(61);
} else if(effect6Last && sensorValue6 <= threshold6) {
effect6Last = 0;
Serial.println(60);
}
}
Serial.println(sensorValue1);
// Wait 100 milliseconds
delay(100);
}
}

sketch_dec29a.ino (2.3 KB)

Do you press on the wall with the same firmness, every time ?

Unless properly mounted and subjected to pressure in a very systematic and repeatable way, I would not expect to get the same reading from such a device twice in a row.

Please use code tags to make your program more readable "#".

To start with, those sensors are inaccurate and nonlinear and you can't rely on repeatability, either. But you might be able to make one work with some programming effort. I might be able to help but I don't understand your program or your setup. Do you actually have six sensors connected to the plate or do you have 6 different plates, each with a single sensor?

Hint: rather than use a bunch of numbered variables, consider using a single array to store six sensor values, another to store averages and yet another to store a limit, etc. Your program will be much shorter and there is less likelihood of making typographical errors.