running a stepper motor clockwise and anticlockwise only once inside the loop

this is my code

nclude <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const int trigPin = 2;
const int echoPin = 3;

void setup() {
// initialize serial communication:
Serial.begin(9600);
myStepper.setSpeed(60);

}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print(“cm”);
Serial.println();

switch(cm)
{
case 60:

Serial.println(“clockwise”);
myStepper.step(stepsPerRevolution);
delay(500);

// step one revolution in the other direction:
Serial.println(“counterclockwise”);
myStepper.step(-stepsPerRevolution);
delay(500);
break;

}

}
long microsecondsToInches(long microseconds)
{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.

return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

MY OBJECTIVE IS :IF THE VALUE"cm=60" IS DETECTED THE STEPPER MOTOR MUST ROTATE CLOCKWISE AND AFTER A DELAY IT MUST ROTATE TO ANTI CLOCKWISE .THIS MUST HAPPEN ONLY ONCE IF THE VALUE IS DETECTED…BUT THE THE MOTOR ROTATES CLOCKWISE AND ANTICLOCKWISE AGAIN AND AGAIN…WHAT SHALL I DO???
PLEASE HELP ME OUT

There is no need to SHOUT...

The behaviour is due to the nature of loop(), coming back to the switch...case again and again, so if the case is still 60, it does the movement sequence again. So what you need to do is have a boolean flag called say "moveDone" which you initialise false.

Then in the 60 case, you have your movement code inside an if(!moveDone), where the ! means not, so that translates as "if not moveDone". Then still inside that if, once you did the movement, you set that flag as true. Then next time through loop(), since the flag is true, the if(!moveDone) test fails and no movement.

BUT you need to set that flag back to false somewhere otherwise you'll never get any movement ever again, the next time the distance is 60. So when the distance is NOT 60, eg in say the switch case default section, you set the flag false.

So the sequence is this:

Initialise flag false Read distance, let's say it's not 60, no harm done, flag stays false Read distance, let's say it is 60, the "if" is good, do the move, set flag true Read distance, let's say it's still 60, "if" is failed, no movement Read distance, let's say it's not 60, flag set back to false Read distance, let's say it's not 60, flag stays false Read distance, let's say it is 60, the "if" is good, do the move, set flag true ....etc