Coding for multiple FSRs

I have this code which displays the output for one FSR. I would like to use arrays to display the output for 3, but am unsure of how to do so.

Thanks


float cf = 19.5; // caliberation factor

 

int ffs1 = A0; // FlexiForce sensor is connected analog pin A0 of arduino or mega. 

 

int ffsdata = 0; 

float vout; 

void setup()

{

  Serial.begin(9600); 

  pinMode(ffs1, INPUT); 

  

}

 

void loop()

{

  

 ffsdata = analogRead(ffs1);

vout = (ffsdata * 5.0) / 1023.0; 

vout = vout * cf ; 

Serial.print("Flexi Force sensor: "); 

Serial.print(vout,3); 

Serial.println(""); 

delay(100); 

  

}

Too much code!

const float cf = 19.5; // caliberation factor
const byte ffs1 = A0; // FlexiForce sensor is connected analog pin A0 of arduino or mega. 

void setup()
{
  Serial.begin(9600); 
}

void loop()
{
  int  ffsdata = analogRead(ffs1);
  float vout = (ffsdata * 5.0) / 1024.0; 
  vout *= cf; 

  Serial.print("Flexi Force sensor: "); 
  Serial.print(vout, 3); 
  Serial.println(""); 
  delay(100); 
}

How are the other FSRs connected?
What did you try?

int ffss[3] = {A0, A1, A2}; // FlexiForce sensors connected to analog pins. 
...
  for (int i=0; i<3; i++)
  {
     ffsdata = analogRead(ffss[i]);

I have attached an image showing how the first FSR is connected. I have replicated this three times, to attach the other two to the breadboard.
I tried a long-winded way of writing the code out three times.
I will try the altered code and add the arrays in.
Thank you.
FSR Connection

or with a range based for loop

const float calibrationFactor = 19.5; // calibration factor
const byte flexiForceSensorPins[] = {A0, A1, A2};      // FlexiForce sensors are connected to 3 analog pins of arduino or mega.

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (auto &ff : flexiForceSensorPins) {
    float vout = (analogRead(ff) * 5.0) / 1023.0;
    Serial.print(vout * calibrationFactor, 3); Serial.write('\t');
  }
  Serial.println();
  delay(100);
}

I have just tried this now, but there are errors, showing it's an undeclared identifier.

We can't see your code or your error.

Apologies, my laptop has just crashed, so I will reboot and upload the code shortly. thankyou

Thank you for the code alterations. It wasn't working because I am using an ESP32 board and needed to change the output pins.
I have attached the code, and the corresponding serial output.

const float calibrationFactor = 19.5; // calibration factor
const byte flexiForceSensorPins[] = {A0, A3, A6};      // FlexiForce sensors are connected to 3 analog pins of arduino or mega.

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (auto &ff : flexiForceSensorPins) {
    float vout = (analogRead(ff) * 5.0) / 1023.0;
    Serial.print(vout * calibrationFactor, 3); Serial.write('\t');
  }
  Serial.println();
  delay(100);
}

3 FSR Serial Output

Have fun !

After reviewing the results and doing some further testing, I've noticed some problems.

When the 1st FSR is pressed it is accurate but it also triggers a display from the second.

When the 2nd is pressed, it only registers for the highest value of around 390.

The 3rd FSR is accurate and works well.

Sounds like there may be a wiring problem. Double-check that everything is wired to the right pins. Try swapping pins between two FSR's to see if the problem moves (FSR problem) or stays the same (pin problem).

I agree that its wiring, but I'm unsure of how to change this. I've attached a photo of how this is currently held together on my breadboard. It uses the concept above where a potentiometer is used to customise the sensitivity. Thanks


I swapped around sensors, and wire connections and had the same issues. Where the first FSR displays and output for 1 and 2.
And pressing the 2nd always gives a maxed out value

I see the problem.

Look at your solderless breadboard. Look at the red and blue lines down the power busses. See the gaps in the red and blue lines? That is a gap in the power busses. You need jumpers across that gap if you want to have power on both ends of the breadboard.

To add to what @johnwasser posted, jumper the gaps like as shown in the (crude) black squares:

Hi guys, after soldering onto a PCB, the connection issues are fixed. And I get accurate responses from the sensors!

I have another piece of code for an air pump. How would I able to activate this code, if an FSR showed an output value of 200?

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.