Need Hep Within an If...Else Statement, to only Run Else Command Once

I'm new to Arduino, and am looking for a solution to only run my "Else" command one time...

I have an IR Sensor wired up with a stepper motor, and want the program to make the stepper motor make two revolutions when the sensor is reading HIGH.

The problem I run into is that as the sensor is reading high, the motor does it's 2 revolutions, and then continues back through the loop again and will continue to rotate until the sensor is switched to LOW.

What I want is for when the sensor reads HIGH, the motor does 2 revolutions, and will not rotate again until the sensor switches to LOW and then back to HIGH again.

Another way to explain it, is that I want the motor to rotate twice when I pass my hand in front of the sensor, and then to sit there until I pass my hand in front of it again. If I hold my hand in front of it, I still want ONLY two rotations.

Here's my code. I tried playing with some If statements to now avail, and currently have a Boolean command that also isn't working.

// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int sensor = 2;
boolean hasrun = false;
void setup() {
Serial.begin(9600);
pinMode(sensor,INPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);

}
void loop() {

if(digitalRead(sensor)==LOW) {
Serial.println("No Product Detected");
}
else
{
Serial.println("Motor Activated");
if(hasrun == false) {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
for(int x = 0; x < 400; x++) // 200 pulses = one full cycle rotation
{ digitalWrite(stepPin,HIGH);
delayMicroseconds(700);
digitalWrite(stepPin,LOW);
delayMicroseconds(700);
boolean (hasrun == true);
Serial.println("1True");}}
else
{
(hasrun == true);
Serial.println("True");}

}
}

Thanks for any help in advance

Use a global variable Two2Revolutions.

Set it to true when two revolutions has occurred.

Do revolutions only when it is false.

Contrary to what you say, you do not want the motor to run "when the sensor is reading HIGH" but when the sensor has changed from LOW to HIGH. Check out the code on the State Detection tutorial, it is written for a button but should be easy to adapt to your case.

BTW you are confusing test for equality (==) with assignment (=).

This is a comparison, not an assignment:

boolean (hasrun == true);

It is also local to the for() loop

This ends both the if statement and the for() loop:

Serial.println("1True");}}

This doesn't really do much of anything:

(hasrun == true);

Thank you all for the help. I was able to achieve the first part of my program needs using the state detection tutorial that Blue Eyes recommended.

My next question (and maybe this needs a new thread), is if there is a piece of code that can time how long since another piece of code has been executed, and then used in an if statement... I know that doesn't really sound clear, so I'll post what I mean in the code below...

// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int sensor = 2;

// Variables will change:
int SensorCounter = 0; // counter for the number of button presses
int SensorState = 0; // current state of the button
int lastSensorState = 0; // previous state of the button

void setup() {
Serial.begin(9600);
pinMode(sensor,INPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}

void loop() {
// read the sensor input pin:
SensorState = digitalRead(sensor);

// compare the SensorState to its previous state
if (SensorState != lastSensorState) {
// if the state has changed, increment the counter
if (SensorState == HIGH) {
// if the current state is HIGH then the button went from off to on:
SensorCounter++;
Serial.println("Motor Activated");
Serial.print("number of button pushes: ");
Serial.println(SensorCounter);
{
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
for(int x = 0; x < 200; x++) // 200 pulses = one full cycle rotation
{ digitalWrite(stepPin,HIGH);
delayMicroseconds(700);
digitalWrite(stepPin,LOW);
delayMicroseconds(700); }
delay(10); }

} else {
// if the current state is LOW then the button went from on to off:
Serial.println("No Product Detected");
}
// Delay a little bit to avoid bouncing
delay(5);
}
// save the current state as the last state, for next time through the loop
lastSensorState = SensorState;

//ALL OF THE ABOVE WORKS AS I WOULD LIKE IT TO WORK. THE MOTOR SPINS ONE REVOLUTION WHEN THE SENSOR CHANGES FROM OFF TO ON.

//NEXT STEP IS TO HAVE THE MOTOR SPIN 4 REVOLUTIONS IN THE OPPOSITE DIRECTION AFTER A SET TIME WITHOUT THE SENSOR COUNTER CHANGING.
//RIGHT NOW IT IS DOING THE 4 REVOLUTIONS IN THE OPPOSITE DIRECTION AFTER THE FOUR SINGLE REVOLUTIONS FROM THE ABOVE CODE.
//IS THERE A WAY TO RUN THE CODE BELOW IF THE SENSOR COUNTER DOES NOT CHANGE FOR A PERIOD OF ## SECONDS?
if (SensorCounter == 4) {{
delay(1500); }
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
for(int x = 0; x < 800; x++) // 200 pulses = one full cycle rotation
{ digitalWrite(stepPin,HIGH);
delayMicroseconds(700);
digitalWrite(stepPin,LOW);
delayMicroseconds(700); }
SensorState = 0;
SensorCounter = 0;}
}