Help With if/else Statement

Hi there, I have written written this code:



if ((distance < 50)&&(currentState=0))
{
currentState = 1;
}
else if ((distance < 50)&&(currentState=1))
{
currentState = 0;



What I am trying to say is if “distance” is less than 50, then turn “currentState” to 1 if it was already 0 and turn it to 0 if it was already 1.

Clearly, it is wrong because it’s not working.

Can someone help me?

Thanks,

Ardalan

Study these two pages:
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/assignment/
https://www.arduino.cc/reference/en/language/structure/comparison-operators/equalto/
Now do you see the problem?

From your description it seems to me that the code should be:

f (distance < 50)
 {
 currentState = 1;
 }
else
 {
 currentState = 0;
}

Thanks pert. I did read those resources and I am still largely confused.

Boylesg,

Thanks, but that is not what I wanted. Your code does accomplish something in that my light turns on when I pass through the sensor but then immediately turns off after I leave.

I need a code that will trigger currentState to alternate between 0 and 1 and stay there every time I pass through the ultrasound.

Thanks again,

Ardalan

It probably is bouncing on and off really quickly while the sensor is triggered.

Put a Serial.println("on") and a Serial.println("off") on the lines right after switching current states to 1 and 0. Start it up, pull up a serial monitor and watch what happens.

You need to watch for the state change from "not triggered" to "triggered."

Ardalan:
Thanks pert. I did read those resources and I am still largely confused.

Boylesg,

Thanks, but that is not what I wanted. Your code does accomplish something in that my light turns on when I pass through the sensor but then immediately turns off after I leave.

I need a code that will trigger currentState to alternate between 0 and 1 and stay there every time I pass through the ultrasound.

Thanks again,

Ardalan

Perhaps this then:

bool bCurrentstate = false;
.
.
.
.
.

if (distance < 50)
 {
 bCurrentstate = !bCurrentstate ;
 }

}

Check = versus == in your conditions.

Thanks for the help everyone,

Do I put this:

bool bCurrentstate = false;
.
.
.
.
.

if (distance < 50)
{
bCurrentstate = !bCurrentstate ;
}

}

alongside the first code you gave me

if (distance < 50)
{
currentState = 1;
}
else
{
currentState = 0;
}

Or do I replace it?

Also, what are the vertical dots for?

 if ((distance < 50)&&(currentState=0))

Are you testing the value of currentState or setting the value of currentState ?

See post #6

Ardalan:
Also, what are the vertical dots for?

if (distance < 50)
 {
 bCurrentstate = !bCurrentstate ;
 }

Is an alternative to what you were doing.

Effectively what you are doing with your original code is toggling a the ‘state’ (assuming my understanding of your intentions is correct.

The above code achieves the same as your original code but with less typing.

And the … is my lazy way of saying whatever other code you need in place of the …'s

Yes. I want to toggle “currentState” between 0 and 1 for every 1 time distance becomes less than 50.

Can you please help me incorporate that into the whole code?


#include <LiquidCrystal.h>
#define trigPin 6
#define echoPin 7
LiquidCrystal lcd(11,10,9,2,3,4,5);

int currentState = 0;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(13,OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(4, 0);
lcd.print(“counter”);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(60);
digitalWrite(trigPin, HIGH);
delayMicroseconds(60);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance < 50)
{
//I don’t know what to do here

lcd.setCursor(4,1);
lcd.print(currentState);
if(currentState > 0) digitalWrite(13,HIGH); else digitalWrite(13,LOW);
delay(500);
}

Thanks. This is only my 2nd project.

Ardalan:
Can you please help me incorporate that into the whole code?

Funny how quick help me turns to write it for me. You won’t even bother to read the forum instructions and post your code right. Why should anyone be writing out your code for you if you thumb your nose at us?

if (distance < 50)
{
//I don't know what to do here

How about:

if (distance < 50)
{
currentState = 1 - currentState;

Think about the math on that one until it makes sense. You only need kindergarten math to do it.

Based on what you have described, this is what you need:

#include <LiquidCrystal.h>
#define trigPin 6
#define echoPin 7
LiquidCrystal lcd(11,10,9,2,3,4,5);

bool bCurrentState = false;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(13,OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(4, 0);
lcd.print("counter");
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(60);
digitalWrite(trigPin, HIGH);
delayMicroseconds(60);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance < 50)
{
    bCurrentState = !bCurrentState ;

lcd.setCursor(4,1);
lcd.print(bCurrentState);
if(bCurrentState) digitalWrite(13,HIGH); else digitalWrite(13,LOW);
delay(500);
}

If duration is int then shouldnt be conversion to float this way?

(duration/2.0)/29.1;

Cos if we expand it will look:
Step1: duration/2 = int//loosing data
Step2: int/float=float

Delta_G. Thanks, that made it work. Sorry if I bothered you.

Delta_G:
Funny how quick help me turns to write it for me. You won’t even bother to read the forum instructions and post your code right. Why should anyone be writing out your code for you if you thumb your nose at us?

if (distance < 50)

{
//I don’t know what to do here




How about:


if (distance < 50)
{
currentState = 1 - currentState;




Think about the math on that one until it makes sense. You only need kindergarten math to do it.

‘currentState = 1 - currentState;’ is a logic operation, i.e. toggling the value of currentState.

IMHO, if it is a logic operation, then you should use the correct data type of bool.

currentState =!currentState where currentState is declared as bool currentState;

Otherwise it may not be immediately clear to others what currentState = 1 - currentState; is doing.

boylesg:
'currentState = 1 - currentState;' is a logic operation, i.e. toggling the value of currentState.

IMHO, if it is a logic operation, then you should use the correct data type of bool.

currentState =!currentState where currentState is declared as bool currentState;

Otherwise it may not be immediately clear to others what currentState = 1 - currentState; is doing.

You're assuming that currentState can't have any other value anywhere else in the code.

I just wanted to demonstrate a math concept.