how to write a code for multiple fsr sensor?

Hi :slight_smile:

I have a 5 fsr sensors which I want to connect to Arduino UNO.
The code thtat I’ve found for one sensor is working but I want to write a routine which will let the same solutions for 5 sensor.
This is my code (it is not compiling)

/* FSR testing sketch. 
Connect one end of FSR to power, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground 
int i=0; 
int fsrPin = 0;     // the FSR and 10K pulldown are connected to a0
int fsrPin1 = 1;
int fsrpin2 = 2;
int fsrPin3 = 3;
int fsrPin4 = 4;
unsigned long fsrReading;     // the analog reading from the FSR resistor divider
unsigned long fsrReading1;
unsigned long fsrReading2;
unsigned long fsrReading3;
unsigned long fsrReading4;

unsigned long fsrVoltage;     // the analog reading converted to voltage
unsigned long fsrVoltage1;
unsigned long fsrVoltage2;
unsigned long fsrVoltage3;
unsigned long fsrVoltage4;

unsigned long fsrResistance;  // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrResistance1;
unsigned long fsrResistance2;
unsigned long fsrResistance3;
unsigned long fsrResistance4;

unsigned long fsrConductance;
unsigned long fsrConductance1;
unsigned long fsrConductance2;
unsigned long fsrConductance3;
unsigned long fsrConductance4;

long fsrForce;       // Finally, the resistance converted to force
long fsrForce1;
long fsrForce2;
long fsrForce3;
long fsrForce4;
long fsrForce5;
void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
void loop(void) {
  for(i=0; i<5 ;i++){
  fsrReading(i) = analogRead(fsrPin(i));
  /*fsrReading1 = analogRead(fsrPin1);
  fsrReading2 = analogRead(fsrPin2);
  fsrReading3 = analogRead(fsrPin3);
  fsrReading4 = analogRead(fsrPin4);
  fsrReading5 = analogRead(fsrPin5);*/
  Serial.print("Analog reading = ");
  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  fsrVoltage(i) = map(fsrReading(i), 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  if (fsrVoltage(i) == 0) {
    Serial.println("No pressure");  
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V       
    fsrResistance(i) = 5000 - fsrVoltage(i);     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance(i) *= 10000;                // 10K resistor
    fsrResistance(i) /= fsrVoltage(i);
    Serial.print("FSR resistance in ohms = ");
    fsrConductance(i) = 1000000;           // we measure in micromhos so 
    fsrConductance(i) /= fsrResistance(i);
    Serial.print("Conductance in microMhos: ");
    // Use the two FSR guide graphs to approximate the force
    if (fsrConductance(i) <= 1000) {
      fsrForce(i) = fsrConductance(i) / 80;
      Serial.print("Force in Newtons: ");
    } else {
      fsrForce(i) = fsrConductance(i) - 1000;
      fsrForce(i) /= 30;
      Serial.print("Force in Newtons: ");

thanks :slight_smile:

Learn to use arrays.

Describe what your code should do.

My code just needs to display the value of force from each 5 fsr sensors

Simple solution, without arrays: Create a subroutine void display(int fsrPin) { } and move into it all your code to read, calculate and display the values for fsrPin. Note that the parameter fsrPin within display() is independent from the global fsrPin. Keep the global fsrPin, fsrPin1 etc., but remove all other variables with a digit at the end of their name.

Then in loop()

  //and so on

Thanks :) I'll try it I think that the code will be in the same size, thats the reason that I want to write 'for' loop In addition, if I make the subeoutine I should duplicate "if" cases that I have for one to all 5 sensors which makes it very long code

Of course you can use a ‘for’ loop like this:

  for (int i=fsrPin; i<fsrPin4; ++i) display(i);

Thanks :slight_smile:

I’m not sure if I know how to do it right but i’ll try and then I upload it here

I tried to do it but I didn't succeed :/ I'll be appreciate if someone can help with it :)

Present your code, using code tags </>