Installing 4 fsr Pressuresensors

My Systems is connected like this: Screenshot by Lightshot

I want to add up from every step i of every sensor the Force. The summation is wrong calculated or interrupted.Maybe I defined something in the wrong loop??? I tried an else if (i==3) statement, but it didnt work out. Who sees my mistake? Thanks in regard.

Gesamtgewicht [0]: Force 20g
Gesamtgewicht [1]: Force 25g
Gesamtgewicht [2]: Force 6g
Gesamtgewicht [3]: Force 0g

Gesamtgewicht: a= Gesamtgewicht [0]+Gesamtgewicht [1]+Gesamtgewicht [2]+Gesamtgewicht [3]


const int FSR_PIN[] = {A0, A1, A2, A3}; // Pin connected to FSR/resistor divider

// Measure the voltage at 5V and resistance of your 3.3k resistor, and enter
// their value's below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 9960.0; // Measured resistance of 3.3k resistor

void setup() 
{
 Serial.begin(9600);
 pinMode(A0, INPUT);
 pinMode(A1, INPUT);
 pinMode(A2, INPUT);
 pinMode(A3, INPUT);
}

void loop() 
{ 
 //get number of pins being used
 int numberOfPins = sizeof(FSR_PIN) / sizeof(FSR_PIN[0]);
 int fsrADC[numberOfPins];

 //collect data from all analog sources
 for (int i = 0; i < numberOfPins; i++) {
   fsrADC[i] = analogRead (FSR_PIN[i]);
   Serial.println( "1:"+ String(fsrADC[0]));
   delay (50);
   Serial.println( "2:"+ String(fsrADC[1]));
   delay (50);
   Serial.println( "3:"+ String(fsrADC[2]));
   delay (50);
   Serial.println( "4:"+ String(fsrADC[3]));
   delay (50);
   }

   float volt[numberOfPins]; 
   float resist[numberOfPins];
   float conduct[numberOfPins];
   float Gesamtgewicht[numberOfPins];
 
 for (int i = 0; i < numberOfPins; i++) {
   if (fsrADC[i] != 0) // If the analog reading is non-zero
 {
   // Use ADC reading to calculate voltage:
   float fsrV = fsrADC[i] * VCC / 1023.0;
   volt[i] = fsrV;
   // Use voltage and static resistor value to 
   // calculate FSR resistance:
   float fsrR = R_DIV * (VCC / volt[i] - 1.0);
   resist[i] = fsrR;
   Serial.println(i);
   Serial.println("Resistance___"  + String(fsrR) + " ohms");
   // Guesstimate force based on slopes in figure 3 of
   // FSR datasheet:
   float force;
   float fsrC = 1.0 / resist[i]; // Calculate conductance
   conduct[i] = fsrC;
   
   // Break parabolic curve down into two linear slopes:
   if (fsrR <= 600) 
     force = (fsrC - 0.00075) / 0.00000032639;
   
   else 
     force =  fsrC / 0.000000642857;
   Gesamtgewicht[i]= force;
   
   for (i=0; i=3; i++) {
   // Summe von force, Gesamtgewicht übermitteln
   
   float a=0;
   a += Gesamtgewicht[i];
   Serial.print("Gesamtgewicht: ");
   Serial.println( String(a) + " g");
   Serial.println();

   delay(1000);
 }}
 else
 {
   // No pressure detected
 }
}}


Please use code tags to increase code readability… You have a bug here:

 //collect data from all analog sources
  for (int i = 0; i < numberOfPins; i++) {
    fsrADC = analogRead (FSR_PIN); //No array indicies applied
    //fsrADC[i] = analogRead (FSR_PIN[i]); //Right way to do it

The same bug re-appears multiple places. Your code loops through the pins multiple times for various reasons, looping throgh the pins once should be enough and more efficient.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

At Danois:

Changing this fsrADC i = analogRead (FSR_PIN i);

to

fsrADC = analogRead (FSR_PIN);

gives this problem:

C:\Users\Olej\Desktop\Multiple_FSR\Multiple_FSR.ino: In function 'void loop()':
C:\Users\Olej\Desktop\Multiple_FSR\Multiple_FSR.ino:26:33: warning: invalid conversion from 'const int*' to 'uint8_t {aka unsigned char}' [-fpermissive]
fsrADC = analogRead (FSR_PIN);
^
In file included from sketch\Multiple_FSR.ino.cpp:1:0:
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.39.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Arduino.h:137:5: note: initializing argument 1 of 'int analogRead(uint8_t)'
int analogRead(uint8_t pin);
^~~~~~~~~~
Multiple_FSR:26:33: error: incompatible types in assignment of 'int' to 'int [numberOfPins]'
fsrADC = analogRead (FSR_PIN);
^
exit status 1
incompatible types in assignment of 'int' to 'int [numberOfPins]'

You did not change anything or you posted the wrong snippet.

Hey Danois, I am sitting since 3 days on that code being at 0 progress. THe question was how can i read from every sensor the force?

I have no experience with arduino or C++, that I would understand where to change my code due to you. At the top you have my code. Would you be so nice and change it by yourself and upload it, so i could prove, if it works. And if yes, I would confirm and the next ones would have a solution for multipleFSR sensors systems.

Thanks!

You code is so messy and some of it was lost because you did not use code tags from the beginning. The variable "a" must change scope in order for the sum to be calculated:

float a = 0; //Declare it here

   for (i=0; i=3; i++) {
   // Summe von force, Gesamtgewicht übermitteln
   
   //float a=0; Do not declare it here
   a += Gesamtgewicht[i];
   Serial.print("Gesamtgewicht: ");
   Serial.println( String(a) + " g");
   Serial.println();

   delay(1000);
 }}

I hope it is now readable or tagged correct?

const int FSR_PIN[] = {A0, A1, A2, A3}; // Pin connected to FSR/resistor divider

// Measure the voltage at 5V and resistance of your 3.3k resistor, and enter
// their value's below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 9960.0; // Measured resistance of 3.3k resistor

void setup() 
{
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
}

void loop() 
{ 
  //get number of pins being used
  int numberOfPins = sizeof(FSR_PIN) / sizeof(FSR_PIN[0]);
  int fsrADC[numberOfPins];

  //collect data from all analog sources
  for (int i = 0; i < numberOfPins; i++) {
    fsrADC [i] = analogRead (FSR_PIN [i]);
    Serial.println( "1:"+ String(fsrADC[0]));
    delay (50);
    Serial.println( "2:"+ String(fsrADC[1]));
    delay (101);
    Serial.println( "3:"+ String(fsrADC[2]));
    delay (152);
    Serial.println( "4:"+ String(fsrADC[3]));
    delay (203);
    }

    float volt[numberOfPins]; 
    float resist[numberOfPins];
    float conduct[numberOfPins];
    float Gesamtgewicht[numberOfPins];
  
  for (int i = 0; i < numberOfPins; i++) {
    if (fsrADC[i] != 0) // If the analog reading is non-zero
  {
    // Use ADC reading to calculate voltage:
    float fsrV = fsrADC[i] * VCC / 1023.0;
    volt[i] = fsrV;
    // Use voltage and static resistor value to 
    // calculate FSR resistance:
    float fsrR = R_DIV * (VCC / volt[i] - 1.0);
    resist[i] = fsrR;
    Serial.println(i);
    Serial.println("Resistance___"  + String(fsrR) + " ohms");
    // Guesstimate force based on slopes in figure 3 of
    // FSR datasheet:
    float force;
    float fsrC = 1.0 / resist[i]; // Calculate conductance
    conduct[i] = fsrC;
    
    // Break parabolic curve down into two linear slopes:
    if (fsrR <= 600) 
      force = (fsrC - 0.00075) / 0.00000032639;
    
    else 
      force =  fsrC / 0.000000642857;
    Gesamtgewicht[i]= force;
    
    
    float a=0;
    for (i=0; i=3; i++) {
    // Summe von force, Gesamtgewicht übermitteln
    a += Gesamtgewicht[i];
    Serial.print("Gesamtgewicht: ");
    Serial.println( String(a) + " g");
    Serial.println();

    delay(1000);
  }}
  else
  {
    // No pressure detected
  }
}}

This is also wrong:

for (i=0; i=3; i++)

Should be “i<numberOfPins” as condition.

const int FSR_PIN[] = {A0, A1, A2, A3}; // Pin connected to FSR/resistor divider

// Measure the voltage at 5V and resistance of your 3.3k resistor, and enter
// their value's below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 9960.0; // Measured resistance of 3.3k resistor

void setup() 
{
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
}

void loop() 
{ 
  //get number of pins being used
  int numberOfPins = sizeof(FSR_PIN) / sizeof(FSR_PIN[0]);
  int fsrADC[numberOfPins];

  //collect data from all analog sources
  for (int i = 0; i < numberOfPins; i++) {
    fsrADC [i] = analogRead (FSR_PIN [i]);
    Serial.println( "1:"+ String(fsrADC[0]));
    delay (50);
    Serial.println( "2:"+ String(fsrADC[1]));
    delay (101);
    Serial.println( "3:"+ String(fsrADC[2]));
    delay (152);
    Serial.println( "4:"+ String(fsrADC[3]));
    delay (203);
    }

    float volt[numberOfPins]; 
    float resist[numberOfPins];
    float conduct[numberOfPins];
    float Gesamtgewicht[numberOfPins];
  
  for (int i = 0; i < numberOfPins; i++) {
    if (fsrADC[i] != 0) // If the analog reading is non-zero
  {
    // Use ADC reading to calculate voltage:
    float fsrV = fsrADC[i] * VCC / 1023.0;
    volt[i] = fsrV;
    // Use voltage and static resistor value to 
    // calculate FSR resistance:
    float fsrR = R_DIV * (VCC / volt[i] - 1.0);
    resist[i] = fsrR;
    Serial.println(i);
    Serial.println("Resistance___"  + String(fsrR) + " ohms");
    // Guesstimate force based on slopes in figure 3 of
    // FSR datasheet:
    float force;
    float fsrC = 1.0 / resist[i]; // Calculate conductance
    conduct[i] = fsrC;
    
    // Break parabolic curve down into two linear slopes:
    if (fsrR <= 600) 
      force = (fsrC - 0.00075) / 0.00000032639;
    
    else 
      force =  fsrC / 0.000000642857;
    

    Gesamtgewicht[i]= force;
    
    float a=0;
    for (i=0; i < numberOfPins; i++) {
    // Summe von force, Gesamtgewicht übermitteln
    a += Gesamtgewicht[i];
    Serial.print("Gesamtgewicht: ");
    Serial.println( String(a) + " g");
    Serial.println();

    delay(1000);
  }}
  else
  {
    // No pressure detected
  }
}}

Still a negative scale :-(.

//collect data from all analog sources
If I dont press on any sensor: first loop gives me

1:0
2:0
3:0
4:0

fsrAdc is 0 fo all sensors, due to no voltage.

And then it is looping 4 times over it, until my fingerpress (see picture, pressing sensor 1 and 3) is recognized. But then it doesnt take the force and sum it up correct. :frowning:

It looks like thhey are ovverwriting. Have no clue??? ANy helps?

Still no solution?

Look at the following code:

 for (int i = 0; i < numberOfPins; i++) {
    fsrADC [i] = analogRead (FSR_PIN [i]);
    Serial.println( "1:"+ String(fsrADC[0]));
    delay (50);
    Serial.println( "2:"+ String(fsrADC[1]));
    delay (101);
    Serial.println( "3:"+ String(fsrADC[2]));
    delay (152);
    Serial.println( "4:"+ String(fsrADC[3]));
    delay (203);
    }

The problem with it is, that all 4 readings are printed to serial 4 times. The first time only fsrADC[0] is valid, the second time fsrADC[0] and fsrADC[1] are valid. If you only want to print valid values to serial, you must move the printing out of that for-loop or implement it differently.

for (int i = 0; i < numberOfPins; i++) {
 //Read
 fsrADC[i] = analogRead (FSR_PIN[i]);
 //Print
 Serial.println(String(i+1) + ":" + String(fsrADC[i]));
}

Even though I just used the “String” class, this should be avoided since it’s known to cause problems.

Thank you very much Danois for supporting this :-). I have now almost done it. Here is the Code.

const int FSR_PIN[] = {A0, A1, A2, A3}; // Pin connected to FSR/resistor divider

// Measure the voltage at 5V and resistance of your 10k resistor, and enter
// their value's below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 9950.0; // Measured resistance of 10k resistor
int printForces = 1;

//get number of pins being used
int numberOfPins = sizeof(FSR_PIN) / sizeof(FSR_PIN[0]);

void setup() 
{
  Serial.begin(9600);
  
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
}

void loop() 
{ 
  int fsrADC[numberOfPins];
  float all =0;
  
  for (int i = 0; i < numberOfPins; i++) {
  //Read
    fsrADC[i] = analogRead (FSR_PIN[i]);
  //Print of all Sensors
    Serial.println("Sensor_"+ String(i+1) + ":" + String(fsrADC[i]));
    delay (50);

    
    float volt[numberOfPins]; 
    float resist[numberOfPins];
    float conduct[numberOfPins];
    float weight[numberOfPins];
    
    //Pressure is present on the FSR Sensor
    if (fsrADC[i] != 0) // If the analog reading is non-zero 
    {
      // Use ADC reading to calculate voltage:
      float fsrV = fsrADC[i] * VCC / 1023.0;
      volt[i] = fsrV;
      // Use voltage and static resistor value to 
      // calculate FSR resistance:
      float fsrR = R_DIV * (VCC / volt[i] - 1.0);
      resist[i] = fsrR;
      // Guesstimate force based on slopes in figure 3 of FSR datasheet:
      float force;
    
      float fsrC = 1.0 / resist[i]; // Calculate conductance
      conduct[i] = fsrC;
    
      if (printForces) {
      // Break parabolic curve down into two linear slopes:
        if (resist[i] <= 600) {
          force = ((conduct[i] - 0.00075) / 0.00000032639);
        }
      
        else {
          force =  (conduct[i] / 0.000000642857);
        }
        weight[i]= force;
        all += weight[i];
     }}
  else
  {
    // No pressure detected
  }
  if ( i <= numberOfPins) {
          Serial.print("Gesamtgewicht: ");
          Serial.println( String(all) + " g");
          Serial.println();  
          delay(1000);  
        }

  }}

In the Attachhment you can see that I am pressing on 2 Sensors and the weight is summed up.
I am still thinking how to display only the weight value after sensor 4 is read out. Hmm, do you have an idea?