Pressure mat

Hey guys,

I am completely new to arduino and I am trying to make a sensor-mat (force sensors). I want to calibrate all four sensors so that they start at 0, this way the difference in weight placement can be measured afterwards.

Pressure.ino (2.54 KB)

``````//#define DEBUG

// Include Libraries
#include "Settings.h"

int sensorPins[numberOfInputs] = { A0 , A1 , A2 , A3};
int sensorMax[numberOfInputs] = { A0 , A1 , A2 , A3};
int sensorMin[numberOfInputs] = { A0 , A1 , A2 , A3};

int ledPin   = 13;
int count = 0;

// Variables
int sensorValue = 0;         // the sensor value
//int sensorMin = 50;        // minimum sensor value
//int sensorMax = 0;           // maximum sensor value

void setup()
{
Serial.begin(9600);  // Baud Rate Serial Communication

pinMode(ledPin, OUTPUT); // LED

// Define Sensorpins as inputs
for(int i = 0; i < numberOfInputs; i++)
{  pinMode(sensorPins[i], INPUT);

digitalWrite(13, HIGH);
// calibrate during the first five seconds using all sensor pins, so each sensor  has to see the max and min pressure during that time !!!
while (millis() < 5000) {
for (int I = 0 ; I < numberOfInputs; I++)
{
// record the maximum sensor value
sensorMax[I] = max(sensorMax[I], sensorValue);
// record the minimum sensor value
sensorMin[I] = min(sensorMin[I], sensorValue);
}
}
}
// after this sensorMin hold the minimum value recorded in the first 5 seconds and sensorMax the highest one

digitalWrite(13, LOW);

}

void loop() {

for (int I = 0 ; I < numberOfInputs; I++)
for (int i=0; i<numberOfInputs; i++) valueReadings[i] = map(analogRead(sensorPins[i]), sensorMin[I], sensorMax[I], 0, 100);

Serial.print("Time: ");
Serial.print(count);
Serial.println(" seconds");
Serial.println("L1: " + String(PercentageL1) + " %" "  R1: " + String(PercentageR1) + " %");
Serial.println("L2: " + String(PercentageL2) + " %" "  R2: " + String(PercentageR2) + " %");
Serial.println();

count += 1;
delay(1000);
}
``````
``````int sensorMax[numberOfInputs] = { A0 , A1 , A2 , A3};
int sensorMin[numberOfInputs] = { A0 , A1 , A2 , A3};
``````

Strange values to use for initial min and max. Perhaps you meant:

``````int sensorMax[numberOfInputs] = { 0 , 0 , 0 , 0};
int sensorMin[numberOfInputs] = { 1023, 1023, 1023, 1023};
``````

Here, I’ve cleaned up the code for you:

``````//#define DEBUG

// Include Libraries
// #include "Settings.h"
const byte numberOfInputs = 4;
//                                 L1, L2, R1, R2
int sensorPins[numberOfInputs] = { A1 , A0 , A2 , A3};
int sensorMax[numberOfInputs] = { 0, 0, 0, 0};
int sensorMin[numberOfInputs] = { 1023, 1023, 1023, 1023};

int ledPin   = 13;

// Variables
int sensorValue = 0;         // the sensor value
//int sensorMin = 50;        // minimum sensor value
//int sensorMax = 0;           // maximum sensor value

void setup()
{
Serial.begin(9600);  // Baud Rate Serial Communication

pinMode(ledPin, OUTPUT); // LED

// for (int i = 0; i < numberOfInputs; i++)
//  pinMode(sensorPins[i], INPUT);

digitalWrite(ledPin, HIGH);
// calibrate during the first five seconds using all sensor pins,
// so each sensor has to see the max and min pressure during that time !!!
while (millis() < 5000) {
for (int I = 0 ; I < numberOfInputs; I++)
{
// record the maximum sensor value
sensorMax[I] = max(sensorMax[I], sensorValue);
// record the minimum sensor value
sensorMin[I] = min(sensorMin[I], sensorValue);
}
}
// Now sensorMin[] hold the minimum value recorded in the first 5 seconds and
// sensorMax[] the highest one

digitalWrite(ledPin, LOW);
}

void loop() {

for (int i = 0; i < numberOfInputs; i++)
{
// Note: If the input to map() goes outside the limits, the output will, too.
// If that is possible, use constrain() on the input or output to avoid problems.
}

int PercentageL1 = map(valueReadings[0], sensorMin[0], sensorMax[0], 0, 100);
int PercentageL2 = map(valueReadings[1], sensorMin[1], sensorMax[1], 0, 100);
int PercentageR1 = map(valueReadings[2], sensorMin[2], sensorMax[2], 0, 100);
int PercentageR2 = map(valueReadings[3], sensorMin[3], sensorMax[3], 0, 100);

Serial.print("Time: ");
Serial.print(millis() / 1000.0, 3);
Serial.println(" seconds");
Serial.println("L1: " + String(PercentageL1) + " %" "  R1: " + String(PercentageR1) + " %");
Serial.println("L2: " + String(PercentageL2) + " %" "  R2: " + String(PercentageR2) + " %");
Serial.println();

delay(1000);
}
``````