need help with an IF statement for the following code

Hey i need help converting this code to an IF statement as due to the code it does the opposite than what i want it to do.

what i'm trying to do if there is pressure on an sensor then do nothing else if no pressure on the senor allow the buzzer to go off until pressure if applied

int buzzer = 5; //declares buzzer @pin 5
int FSRanalogpin = A0; //declares the FSR @analogpin A0
int FSRvalue = 0; //saves the analog value as integer
void setup()
{
pinMode(FSRanalogpin,INPUT); //sets the FSR as the input
pinMode(buzzer,OUTPUT); //sets the buzzer as the output
}
void loop()
{
FSRvalue = analogRead(FSRanalogpin); //reads the FSR and store it in integer
analogWrite(buzzer,FSRvalue/4); //sets the buzzer duty ratio
}

I don't see an if statement in the code.

no i need help to implement one. with the current code it does if pressure if on the sensor allow buzzer to go off i need it to be the opposite away around .

mattzboy:
with the current code it does if pressure if on the sensor allow buzzer to go off i need it to be the opposite away around .

If you show us the code that does that, it'll be easier for us to suggest a solution.

Can you explain simply what your code does now, and what you want it to do?

AWOL:
Can you explain simply what your code does now, and what you want it to do?

the code is above but here it is again

int buzzer = 5; //declares buzzer @pin 5
int FSRanalogpin = A0; //declares the FSR @analogpin A0
int FSRvalue = 0; //saves the analog value as integer
void setup()
{
pinMode(FSRanalogpin,INPUT); //sets the FSR as the input
pinMode(buzzer,OUTPUT); //sets the buzzer as the output
}
void loop()
{
FSRvalue = analogRead(FSRanalogpin); //reads the FSR and store it in integer
analogWrite(buzzer,FSRvalue/4); //sets the buzzer duty ratio
}

We don't want the code. We want to know what you think the code is actually doing. hint: The code you have written has the buzzer output proportional to the pressure applied. So it does not work in the manner you think it does.

Have you checked the Arduino Reference section on how to write if statement?

You need to program just what you typed.

if (sensorvalue > x)
{ analogWrite(buzzer, 0); }
else
{ analogWrite(buzzer,somevalue); }

Yes, I saw the code (I didn't see the code tags), but that is not what I asked.

You get to wait five minutes between posts - use those 300 seconds wisely.

right the code works the way it needs to but i need help to change the way it works.

the way i want the code to work to be if the sensor has no pressure the output (buzzer) will continue until the sensor has pressure again which stops the buzzer from working again. so if anyone help rewrite the code to do so it would be a big help.

OK, let's try a different approach.

Does pressure give you a high FSRvalue, or a low one?

GypsumFantastic:
OK, let's try a different approach.

Does pressure give you a high FSRvalue, or a low one?

am sorry to say i haven't got a clue what i'm doing am new to all this at the moment the FSRvalue is the sensor and is acting like an on and off switch but with pressure i can link you all to the webpage i went to see if it helps.

OK, here's the code that adwsystems gave you. I've adapted it a bit to use your variable names and I inserted a couple of values.

if (FSRvalue > 300)
{ analogWrite(buzzer, 0); }
else
{ analogWrite(buzzer,128); } // EDIT - oops I put 512 before

Try using that in your code instead of this line:

analogWrite(buzzer,FSRvalue/4); //sets the buzzer duty ratio

You'll probably find that 300 isn't the right value. Move it up and down to set the on/off threshold.

You might find that you need < instead of > in the if command so things might be backwards the way I've written it.

GypsumFantastic:
OK, here's the code that adwsystems gave you. I've adapted it a bit to use your variable names and I inserted a couple of values.

if (FSRvalue > 300)

{ analogWrite(buzzer, 0); }
else
{ analogWrite(buzzer,512); }




Try using that in your code instead of this line: 



analogWrite(buzzer,FSRvalue/4); //sets the buzzer duty ratio




You'll probably find that 300 isn't the right value. Move it up and down to set the on/off threshold.

You might find that you need < instead of > in the if command so things might be backwards the way I've written it.

I could have done that. I don't give answers to the basic programming or electronics, because it defeats the learning curve.

You also probably find that 512 isn't the correct value either.

Oops yeah

AWOL:
You also probably find that 512 isn't the correct value either.

Oops yeah, I'll edit it to 128. Thanks for the catch.