FSR SENSOR PROGRAMMING

ya,hi, im a newbie to programming and arduino

i used 3 fsr sensor on Arduino 101, i wan to calculate the force at those three fsr sensors. For example, if the difference between sensor A and B is more than 10, it will trigger a buzzer. how should i do??

My ideal came from here

but the coding at the site cnt work

ya,hi, im a newbie to programming and arduino

i used 3 fsr sensor on Arduino 101, i wan to calculate the force at those three fsr sensors. For example, if the difference between sensor A and B is more than 10, it will trigger a buzzer. how should i do??

My idea came from here

but the coding at the site cnt work

Can you read the output from the sensor, possibly using analogRead() ?

I assume that you can as you know that the difference between two readings may be 10 or more

What code do you have ? Post it here after you have read read this before posting a programming question

What exactly do you mean by

but the coding at the site cnt work

this is my coding, it didnt trigger the buzzer,even though i exert force at either 1 fsr only

1.ino (4.72 KB)

Forget all the fancy stuff for now. Read the values from the force sensors and print it to the Serial monitor. If you do not know how to do this do not try anything more complicated until you get that working

Have you use successfully used Blynk for anything else ?

no , this is my very first time get into arduino and blynk

Forget Blynk for now.

As I said, get the basics working before you move on. Try the examples in the IDE and understand them. You will make progress very slowly or not at all if you simply copy/paste complicated code from the Web. Look at the AnalogInput example

Do you have any experience of programming ?

i just know the basic c++ language, im totally a newbie

i juz attached my coding to read one fsr reading at below, and it works.....the problem is i am quite confuse bout the coding when i want to add in another 2 fsr

sketch_jan17a.ino (804 Bytes)

i am quite confuse bout the coding when i want to add in another 2 fsr

There are simple but clumsy ways to do this and clever ways to do this.

Others may disagree but try the clumsy way first.

Add a zero to all the references to fsr in your code so fsrPin becomes fsrPin0, fsrReading becomes fsrReading0 etc and make sure that it works.

Then duplicate the code in loop() but suffix the fsr references with 1 instead of zero. Precede the printing with text indicating whether you are printing information regarding number 0 or number 1

Come back when you have got that working and we will move on to the cleverer way to do this using arrays

u mean like this?

sketch_jan17a.ino (1.98 KB)

revanneo12:
u mean like this?

Please post your code here using code tags when you do

int fsrPin0 = 0; 
int fsrPin1 = 1;
int fsrPin2 = 2;// the FSR and 10K pulldown are connected to a0
int fsrReading0;
int fsrReading1;
int fsrReading2;// the analog reading from the FSR resistor divider
int buzzer =8;
int fsrVoltage0;  
int fsrVoltage1;  
int fsrVoltage2;  
unsigned long fsrResistance0;
unsigned long fsrResistance1;
unsigned long fsrResistance2;
void setup(void) {
 Serial.begin(9600);  
}

void loop(void) {
 fsrReading0 = analogRead(fsrPin0); 
 Serial.print("Analog reading = ");
 Serial.print(fsrReading0);     // the raw analog reading
 // We\\\'ll have a few threshholds, qualitatively determined
 if (fsrReading0 < 10) {
   Serial.println(" - No pressure");
 } else if (fsrReading0 < 200) {
   Serial.println(" - Light touch");
 } else if (fsrReading0 < 500) {
   Serial.println(" - Light squeeze");
 } else if (fsrReading0 < 800) {
   Serial.println(" - Medium squeeze");
 } else {
   Serial.println(" - Big squeeze");
}

{
 fsrReading1 = analogRead(fsrPin1); 
 Serial.print("Analog reading = ");
 Serial.print(fsrReading1);     // the raw analog reading
 // We\\\'ll have a few threshholds, qualitatively determined
 if (fsrReading1 < 10) {
   Serial.println(" - No pressure");
 } else if (fsrReading1< 200) {
   Serial.println(" - Light touch");
 } else if (fsrReading1 < 500) {
   Serial.println(" - Light squeeze");
 } else if (fsrReading1 < 800) {
   Serial.println(" - Medium squeeze");
 } else {
   Serial.println(" - Big squeeze");
}
}

{
 fsrReading2 = analogRead(fsrPin2); 
 Serial.print("Analog reading = ");
 Serial.print(fsrReading2);     // the raw analog reading
 // We\\\'ll have a few threshholds, qualitatively determined
 if (fsrReading2 < 10) {
   Serial.println(" - No pressure");
 } else if (fsrReading2 < 200) {
   Serial.println(" - Light touch");
 } else if (fsrReading2 < 500) {
   Serial.println(" - Light squeeze");
 } else if (fsrReading2 < 800) {
   Serial.println(" - Medium squeeze");
 } else {
   Serial.println(" - Big squeeze");
}

fsrVoltage0 = map(fsrReading0, 0, 1023, 0, 5000);
 Serial.print("Voltage reading in mV = ");
 Serial.println(fsrVoltage0);  
 if (fsrVoltage0 == 0) {
   Serial.println("No pressure");  
 } else {
   // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
   // so FSR = ((Vcc - V) * R) / V        yay math!
   fsrResistance0 = 5000 - fsrVoltage0;     // fsrVoltage is in millivolts so 5V = 5000mV
   fsrResistance0 *= 10000;                // 10K resistor
   fsrResistance0 /= fsrVoltage0;
   Serial.print("FSR resistance in ohms = ");
   Serial.println(fsrResistance0);
 }
   fsrVoltage1 = map(fsrReading1, 0, 1023, 0, 5000);
 Serial.print("Voltage reading in mV = ");
 Serial.println(fsrVoltage1);  
 if (fsrVoltage1 == 0) {
   Serial.println("No pressure");  
 } else {
   // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
   // so FSR = ((Vcc - V) * R) / V        yay math!
   fsrResistance1 = 5000 - fsrVoltage1;     // fsrVoltage is in millivolts so 5V = 5000mV
   fsrResistance1 *= 10000;                // 10K resistor
   fsrResistance1 /= fsrVoltage1;
   Serial.print("FSR resistance in ohms = ");
   Serial.println(fsrResistance1);

 }
   fsrVoltage2 = map(fsrReading2, 0, 1023, 0, 5000);
 Serial.print("Voltage reading in mV = ");
 Serial.println(fsrVoltage2);  
 if (fsrVoltage2 == 0) {
   Serial.println("No pressure");  
 } else {
   // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
   // so FSR = ((Vcc - V) * R) / V        yay math!
   fsrResistance2 = 5000 - fsrVoltage2;     // fsrVoltage is in millivolts so 5V = 5000mV
   fsrResistance2 *= 10000;                // 10K resistor
   fsrResistance2 /= fsrVoltage2;
   Serial.print("FSR resistance in ohms = ");
   Serial.println(fsrResistance1);
delay(1000);

}

  

}
}

That is the sort of thing I had in mind. Does it work ?

If it does work then you cannot tell from the Serial.print()s which sensor is being reported on, hence my suggestion to label the outputs. Also, you don't need to put curly brackets around the code for each sensor

So, does it work ?

done removed the curly bracket, am i right?
how to label the output?

int fsrPin0 = 0; 
int fsrPin1 = 1;
int fsrPin2 = 2;// the FSR and 10K pulldown are connected to a0
int fsrReading0;
int fsrReading1;
int fsrReading2;// the analog reading from the FSR resistor divider
int buzzer =8;
int fsrVoltage0;  
int fsrVoltage1;  
int fsrVoltage2;  
unsigned long fsrResistance0;
unsigned long fsrResistance1;
unsigned long fsrResistance2;
void setup(void) {
  Serial.begin(9600);  
}
 
void loop(void) { 
  {
  fsrReading0 = analogRead(fsrPin0); 
  Serial.print("Analog reading = ");
  Serial.print(fsrReading0);     // the raw analog reading
  // We\\\'ll have a few threshholds, qualitatively determined
  if (fsrReading0 < 10) {
    Serial.println(" - No pressure");
  } else if (fsrReading0 < 200) {
    Serial.println(" - Light touch");
  } else if (fsrReading0 < 500) {
    Serial.println(" - Light squeeze");
  } else if (fsrReading0 < 800) {
    Serial.println(" - Medium squeeze");
  } else {
    Serial.println(" - Big squeeze");
}
  

 
  fsrReading1 = analogRead(fsrPin1); 
  Serial.print("Analog reading = ");
  Serial.print(fsrReading1);     // the raw analog reading
  // We\\\'ll have a few threshholds, qualitatively determined
  if (fsrReading1 < 10) {
    Serial.println(" - No pressure");
  } else if (fsrReading1< 200) {
    Serial.println(" - Light touch");
  } else if (fsrReading1 < 500) {
    Serial.println(" - Light squeeze");
  } else if (fsrReading1 < 800) {
    Serial.println(" - Medium squeeze");
  } else {
    Serial.println(" - Big squeeze");
}
 

 
  fsrReading2 = analogRead(fsrPin2); 
  Serial.print("Analog reading = ");
  Serial.print(fsrReading2);     // the raw analog reading
  // We\\\'ll have a few threshholds, qualitatively determined
  if (fsrReading2 < 10) {
    Serial.println(" - No pressure");
  } else if (fsrReading2 < 200) {
    Serial.println(" - Light touch");
  } else if (fsrReading2 < 500) {
    Serial.println(" - Light squeeze");
  } else if (fsrReading2 < 800) {
    Serial.println(" - Medium squeeze");
  } else {
    Serial.println(" - Big squeeze");
}
  }
 
 
 fsrVoltage0 = map(fsrReading0, 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  Serial.println(fsrVoltage0);  
  if (fsrVoltage0 == 0) {
    Serial.println("No pressure");  
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V        yay math!
    fsrResistance0 = 5000 - fsrVoltage0;     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance0 *= 10000;                // 10K resistor
    fsrResistance0 /= fsrVoltage0;
    Serial.print("FSR resistance in ohms = ");
    Serial.println(fsrResistance0);
  }
    fsrVoltage1 = map(fsrReading1, 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  Serial.println(fsrVoltage1);  
  if (fsrVoltage1 == 0) {
    Serial.println("No pressure");  
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V        yay math!
    fsrResistance1 = 5000 - fsrVoltage1;     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance1 *= 10000;                // 10K resistor
    fsrResistance1 /= fsrVoltage1;
    Serial.print("FSR resistance in ohms = ");
    Serial.println(fsrResistance1);

  }
    fsrVoltage2 = map(fsrReading2, 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  Serial.println(fsrVoltage2);  
  if (fsrVoltage2 == 0) {
    Serial.println("No pressure");  
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V        yay math!
    fsrResistance2 = 5000 - fsrVoltage2;     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance2 *= 10000;                // 10K resistor
    fsrResistance2 /= fsrVoltage2;
    Serial.print("FSR resistance in ohms = ");
    Serial.println(fsrResistance2);
 delay(1000);
 
}  

 }

can i trigger the buzzer by using this command?

if(fsrResistance2>1200){
tone(buz,1000,500);
}

Many questions in your reply.

You have removed the brackets round the code for each sensor but now have unnecessary brackets around all of the code in loop(). The code will work with them in place but they are not needed.

To label the output just use Serial.print() to print a message but include the sensor number in the text.

Forget about producing tones for now, that can come later.

The next improvement to your code is to introduce arrays so that soon you can drasically reduce the size of the program

First replace

int fsrReading0;
int fsrReading1;
int fsrReading2;// the analog reading from the FSR resistor divider
int buzzer = 8;
int fsrVoltage0;
int fsrVoltage1;
int fsrVoltage2;
unsigned long fsrResistance0;
unsigned long fsrResistance1;
unsigned long fsrResistance2;

with

int fsrReading[3];
int buzzer = 8;
int fsrVoltage[3];
unsigned long fsrResistance[3];

Which will declare 3 arrays instead of 9 individual variables

Then, in your code, wherever you have the original variables with a number suffix put square brackets around the number so that the data is stored in the arrays. Do not change the pin number variables.

The code should work just the same way as without arrays, but once that works we can remove about 2 thirds of you code

done.......if im not mistaken.

int fsrPin0 = 0; 
int fsrPin1 = 1;
int fsrPin2 = 2;// the FSR and 10K pulldown are connected to a0
int fsrReading[3];
int buzzer = 8;
int fsrVoltage[3];
unsigned long fsrResistance[3];
void setup(void) {
  Serial.begin(9600);  
}
 
void loop(void) { 
  {
  fsrReading[0] = analogRead(fsrPin0); 
  Serial.print("Analog reading = ");
  Serial.print(fsrReading[0]);     // the raw analog reading
  // We\\\'ll have a few threshholds, qualitatively determined
  if (fsrReading[0] < 10) {
    Serial.println(" - No pressure");
  } else if (fsrReading[0] < 200) {
    Serial.println(" - Light touch");
  } else if (fsrReading[0] < 500) {
    Serial.println(" - Light squeeze");
  } else if (fsrReading[0] < 800) {
    Serial.println(" - Medium squeeze");
  } else {
    Serial.println(" - Big squeeze");
}
  

 
  fsrReading[1] = analogRead(fsrPin1); 
  Serial.print("Analog reading = ");
  Serial.print(fsrReading[1]);     // the raw analog reading
  // We\\\'ll have a few threshholds, qualitatively determined
  if (fsrReading[1] < 10) {
    Serial.println(" - No pressure");
  } else if (fsrReading[1]< 200) {
    Serial.println(" - Light touch");
  } else if (fsrReading[1] < 500) {
    Serial.println(" - Light squeeze");
  } else if (fsrReading[1] < 800) {
    Serial.println(" - Medium squeeze");
  } else {
    Serial.println(" - Big squeeze");
}
 

 
  fsrReading[2] = analogRead(fsrPin2); 
  Serial.print("Analog reading = ");
  Serial.print(fsrReading[2]);     // the raw analog reading
  // We\\\'ll have a few threshholds, qualitatively determined
  if (fsrReading[2] < 10) {
    Serial.println(" - No pressure");
  } else if (fsrReading[2] < 200) {
    Serial.println(" - Light touch");
  } else if (fsrReading[2 ]< 500) {
    Serial.println(" - Light squeeze");
  } else if (fsrReading[2] < 800) {
    Serial.println(" - Medium squeeze");
  } else {
    Serial.println(" - Big squeeze");
}
  }
 
 
 fsrVoltage[0] = map(fsrReading[0], 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  Serial.println(fsrVoltage[0]);  
  if (fsrVoltage[0] == 0) {
    Serial.println("No pressure");  
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V        yay math!
    fsrResistance[0] = 5000 - fsrVoltage[0];     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance[0] *= 22000;                // 10K resistor
    fsrResistance[0] /= fsrVoltage[0];
    Serial.print("FSR resistance in ohms = ");
    Serial.println(fsrResistance[0]);
  }
    fsrVoltage[1] = map(fsrReading[1], 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  Serial.println(fsrVoltage[1]);  
  if (fsrVoltage[1 ]== 0) {
    Serial.println("No pressure");  
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V        yay math!
    fsrResistance[1] = 5000 - fsrVoltage[1];     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance[1] *= 22000;                // 10K resistor
    fsrResistance[1] /= fsrVoltage[1];
    Serial.print("FSR resistance in ohms = ");
    Serial.println(fsrResistance[1]);

  }
    fsrVoltage[2] = map(fsrReading[2], 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  Serial.println(fsrVoltage[2]);  
  if (fsrVoltage[2] == 0) {
    Serial.println("No pressure");  
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V        yay math!
    fsrResistance[2] = 5000 - fsrVoltage[2];     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance[2] *= 22000;                // 10K resistor
    fsrResistance[2] /= fsrVoltage[2];
    Serial.print("FSR resistance in ohms = ");
    Serial.println(fsrResistance[2]);
 delay(1000);
 
}

The code you posted has either one too many curly brackets.
Take out one or other of these

void loop(void) { 
  {

Now, let's work some magic using the arrays. Basically you have 3 copies of the code in your program, one for each sensor. That is because I told you to do it and the program works. However, even when you use the arrays, as now, the code is repeated for each sensor. What if we could use the same code for each sensor and get the program to replace the sensor numbers in the square brackets with the sensor number ? If we can do that then we only need one copy of the code.

Up to now the 0, 1, and 2 suffixes and the arrays have been a nuisance but now they are going to come into their own. We are going to use a for loop counting 0, 1, 2 and make the program use those numbers to access the data in the arrays. Rather than explain what to do, on this occasion I am going to post the solution as a complete program. Have a look at it, change the baud rate and delay()s to suit yourself but please try to understand how and why it works. Basically we use the value of the sensor variable to access the required level of the arrays and end up with not much more code than is used for one sensor.

const byte fsrPins[] = {0, 1, 2};
int fsrReading[3];
int buzzer = 8;
int fsrVoltage[3];
unsigned long fsrResistance[3];
void setup(void)
{
  Serial.begin(115200);
}

void loop(void)
{
  for (int sensor = 0; sensor < 3; sensor++)
  {
    Serial.print("Sensor ");
    Serial.println(sensor);
    fsrReading[sensor] = analogRead(fsrPins[sensor]);
    Serial.print("Analog reading = ");
    Serial.print(fsrReading[sensor]);     // the raw analog reading
    // We\\\'ll have a few threshholds, qualitatively determined
    if (fsrReading[sensor] < 10)
    {
      Serial.println(" - No pressure");
    }
    else if (fsrReading[sensor] < 200)
    {
      Serial.println(" - Light touch");
    }
    else if (fsrReading[sensor] < 500)
    {
      Serial.println(" - Light squeeze");
    }
    else if (fsrReading[sensor] < 800)
    {
      Serial.println(" - Medium squeeze");
    }
    else
    {
      Serial.println(" - Big squeeze");
    }
    fsrVoltage[sensor] = map(fsrReading[sensor], 0, 1023, 0, 5000);
    Serial.print("Voltage reading in mV = ");
    Serial.println(fsrVoltage[sensor]);
    if (fsrVoltage[sensor] == 0)
    {
      Serial.println("No pressure");
    }
    else
    {
      // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
      // so FSR = ((Vcc - V) * R) / V        yay math!
      fsrResistance[sensor] = 5000 - fsrVoltage[sensor];     // fsrVoltage is in millivolts so 5V = 5000mV
      fsrResistance[sensor] *= 10000;                // 10K resistor
      fsrResistance[sensor] /= fsrVoltage[sensor];
      Serial.print("FSR resistance in ohms = ");
      Serial.println(fsrResistance[sensor]);
    }
    Serial.println();
  }
  Serial.println();
  delay(2000);
}

Note that I have also put the sensor pins in an array and changed the Serial output. Note also that this is not the only way to structure the code and putting sections of it in named functions would be a good idea.

okay....i understand this, so now means im done with getting the reading?

so how can i use the reading to trigger the buzzer,

for example

if((fsrVoltage1+fsrVoltage2)>100){
tone(buzzer,1000,500);
}

like this?

and what does Serial.begin(115200); means?

like this?

Not quite. Remember that the readings are in an array

SO, HOW SHOULD I TRIGGER THE BUZZER?