Assigning variable value once

Hi everyone, newbie here! I'm trying to assign the value of millis() to a couple variables after input from an ultrasonic sensor. My code looks something like this

if (inches <= 2) {
x = millis()
if (inches > 2) {
y = millis() }}

The problem is the first "if" just keeps looping until the second "if" is true, so the value of x is constantly being overridden--I just want it to be assigned a value once when the condition is FIRST met. Is there a way to do this?

Thank you!

Add a flag that says your variable has been initialized and don't put the second if within the first one, use else if (or just a plain second if statement whatever makes sense.)

boolean xIsNotInitialized = true;

... 

if (xIsNotInitialized && inches <= 2) {
   x = millis();
   xIsNotInitialized = false;
} else ....

If your code won't run for days, then instead of the Boolean you could just check if x is == to 0 then it has not been initialized yet (there could be a slight chance if you are very unlucky that after ~50 days millis rolls back to zero and this is the value you would want to capture... that would be extremely bad luck, but good programs don't rely on luck, hence the Boolean)

Thinking that since he's looking to update "x" after a sensor read, could this not also be handled by something like

if(oldSensorValue != newSensorValue){
    x = millis();
    oldSensorValue = newSensorValue;
}

Edit: Re-thought this through. Probably wouldn't be a good way as newSensorValue could be constantly changing. No idea how often his sketch reads the sensor or how often he needs x to update. So, scratch my idea and go with J-M-L's method.

Thank you for the input all, I'll give it a shot!

Okay, so still having issues. Although I get the gist of the it, I'm not experienced enough to fully understand the logic flow. The first "if" loop is still repeating itself and the code doesn't seem to be entering the second "if" loop, which is nested in the first. I will attach my code.

Photogate.ino (1.31 KB)

Never call Serial.begin() every time through the loop.

Also this

  boolean aIsNotInitialized = true;
  boolean bIsNotInitialized = true;

at the top of the loop nullifies any changes you made to those variables later on in the loop, as soon as the loop repeats.

I have no idea what the "for" loops you refer to are. I don't see any.

Hi,
What is the application.
If you are trying to measure distance using this ultrasonic sensor.
F90JYNWH7UR7RCS.MEDIUM.jpg

There is a library that will do most of the work.

google HC-SR04 arduino library

Tom... :slight_smile:

F90JYNWH7UR7RCS.MEDIUM.jpg

aarg:
Never call Serial.begin() every time through the loop.

Also this

  boolean aIsNotInitialized = true;

boolean bIsNotInitialized = true;



at the top of the loop nullifies any changes you made to those variables later on in the loop, as soon as the loop repeats.

I have no idea what the "for" loops you refer to are. I don't see any.

Meant "if" loops, fixed it in my original post. And thanks for pointing that out

TomGeorge:
Hi,
What is the application.
If you are trying to measure distance using this ultrasonic sensor.
F90JYNWH7UR7RCS.MEDIUM.jpg

There is a library that will do most of the work.

google HC-SR04 arduino library

Tom... :slight_smile:

I'm essentially trying to create a makeshift photo gate with two ultrasonics.