Arduino FSR if statement

Hi,
I have a final year project consisting of an Arduino UNO and four FSR’s I have the code to read the data off the Arduino but I want to put an if statement on it so that if one FSR is off the whole system is off. I can get it to display SYSTEM OFF when there is no pressure on the FSR’s but when I apply pressure it wont leave the if statement.

Any help would be appreciated
Thanks.

FYP Code.txt (2.09 KB)

There is an else if statement in it that I tried but it didnt work I also tried just using else , and still no success.

Please supply a schematic of how your system is wired.

The system is wired simply, four fsrs, each with a wire going to 5V and a 10K resistor from the resistor to analog pins and to ground. It had to be simple as it had to be placed into the sole of a shoe to measure the forces exerted by a subject during Sit to Stand

void loop(void) 
{
  if( FSR1 || FSR2 || FSR3 || FSR4 <=0)
  {

Where is your code to read the sensors? Why is embedded in an if block?

I started with the code that just read the FSR’s so its in the else if statement . When I put the code from the elsie if statement into the if statement the system just stops giving data

JVaughan:
I started with the code that just read the FSR's so its in the else if statement . When I put the code from the elsie if statement into the if statement the system just stops giving data

Well, you have to do a little thinking here. Ask yourself a few questions. Paul got you started with one good question. Why are the analogRead() statements in an if block? It's not enough to say that they are there because you put them there. You need a reason to have them in there. Think about it.

Here are a few more:

What are the values of FSR1, FSR2, FSR3, and FSR4 when you enter loop()?
When do those values change?
If you don't know what the values are, how could you find out?

Do you know which if block you execute?
Do either of the blocks execute?

Thanks for the input this is helping me greatly. When I take the analog read statements out of my IF statement, the output is SYSTEM OFF but it wont change if I apply pressure. Do I take out the whole statement? or just the four lines of analog read ?

The FSR readings are all zero going into the loop and they change when pressure is applied to them.

Does this mean my if and else statements arent correct? Well I know they arent, but why does the IF statement come on and not the else?

Think about what happens when loop() runs. You are making decisions with the values in FSRn, without having populated them.

On each pass through loop(), the first thing to do is populated the values of FSRn - i.e. read the values from the analog pins.

They populate from the AnalogRead Yes? so when I put that before the IF statement should that not populate it?

They populate from the AnalogRead Yes?

From the analogRead(), yes.

so when I put that before the IF statement should that not populate it?

Too many nots for me to follow. Plus, I don't know what "that" is.

JVaughan:
They populate from the AnalogRead Yes? so when I put that before the IF statement should that not populate it?

Probably. Hard to say for sure without seeing the revised code though.

When I put the AnalogRead statements outside and before the IF statement shouldnt that populate the IF statement?

Revised i.e took the AnalogRead out of the statement

FYP Code.txt (2.1 KB)

If I put FS1Pin = Serial.read into the if statement would that populate it?

JVaughan:
Revised i.e took the AnalogRead out of the statement

First, please post code in code tags. To do this:
In the IDE
Select Tools->Auto Format
Click in the code window, and press CTRL+a
Press CTRL+c

In the forum editor window
Click the # icon
Press CTRL+v

Now, to answer your question, yes, the analogRead() statements should place a value into the FSR vaiables. Open a new IDE, and paste the following code into it...

int FSR1Pin = A0; // the FSR and 10K pulldown are connected to a0
int FSR2Pin = A1;
int FSR3Pin = A2;
int FSR4Pin = A3;
int FSR1; // the analog reading from the FSR resistor divider
int FSR2, FSR3, FSR4;
int FSR1Voltage;
int FSR2Voltage, FSR3Voltage, FSR4Voltage;// the analog reading converted to voltage
unsigned long fsrResistance; // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance; 
long fsrForce; // Resistance converted to force

void setup(void) 
{
//   Note: the pins do not need to be set to INPUT.
  Serial.begin(115200);
}

void loop(void) 
{
  FSR1 = analogRead(FSR1Pin);
  FSR2 = analogRead(FSR2Pin);
  FSR3 = analogRead(FSR3Pin);
  FSR4 = analogRead(FSR4Pin);

  Serial.println(FSR1);         
  Serial.println(FSR2);         
  Serial.println(FSR3);         
  Serial.println(FSR4); 
  Serial.println();  
  delay(500);
}

You should always start with the basics, and work from there. Serial.print() is your best friend. You can always take them out later.

Once you get the analogRead() statements working, you can then worry about what to do with them. I suspect that your FSRs are either not working or are wired incorrectly. It might help if you supply data for the FSRs and a schematic of the way you wired them.

This doesn’t look good:

 if( FSR1 || FSR2 || FSR3 || FSR4 <=0)

I assume your intent is to discover if any one of your readings is less than or equal to zero. The || operator doesn’t work like that.

This is more like it:

 if( FSR1<=0 || FSR2<=0 || FSR3<=0 || FSR4 <=0)

Same issue with your use of && further down.

analogRead returns values in the range 0 to 1023 though, so testing for values less than zero is unnecessary. The ADC readings tend to vary a little between readings so it’s probably not a great idea to rely on a specific reading like zero either - give it a bit of a range. How big a range, you’ll need to discover by experiment with your hardware.