If sensore is triggered perform Commands 'X' amount of times

The operation i require is as follows;

  1. Run Coneyor belt.
  2. If sensor detects 0 inches stop conveyor belt.
  3. Robot arm picks object up and carries it to parts bin.
  4. Robot arm returns to start position and conveyor belt starts again.

The following code achieves this operation however i want this task performed only 5 times and then the robot will perform another task after.

#include <Servo.h>

Servo waistservo;
Servo wristelevationservo; //Servo name to control servo
Servo shoulderservo; //Servo name to control servo
Servo elbowservo; //Servo name to control servo
Servo wristrotationservo; //Servo name to control servo
Servo gripperservo; //Servo name to control servo

const int pingPin = 7; //Pin number of sensor output
const int motorPin = 2; //Pin number of motor output
int pos = 0; //Set position as an integer

void setup()
{
Serial.begin(9600); // initialize serial communication:
waistservo.attach(3); // attaches the servo on pin 3 to the servo object
wristelevationservo.attach(5);
shoulderservo.attach(6);
elbowservo.attach(9); // attaches the servo on pin 9 to the servo object
wristrotationservo.attach(10);
gripperservo.attach(11); // attaches the servo on pin 5 to the servo object

pinMode(motorPin, OUTPUT); // set motor pin as an output
digitalWrite(motorPin, HIGH); // set motorPin to high

{
waistservo.write(90); // Sets servo to initial position
delay(2000);
wristelevationservo.write(120); // Sets servo to initial position
delay(2000);
shoulderservo.write(50); // Sets servo to initial position
delay(2000);
elbowservo.write(150); // Sets servo to initial position
delay(2000);
wristrotationservo.write(80); // Sets servo to initial position
delay(2000);
gripperservo.write(1); // Sets servo to initial position
delay(2000);
}
}

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

long duration, inches, cm;

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

// The same pin is used to read the signal from the PING))): 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(pingPin, INPUT);
duration = pulseIn(pingPin, 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();

delay(100);

// If sensor detects 0 inches the conveyor belt will stop, once stopped
// the robot arm picks up the object and carries it to the parts bin.

if (inches == 0) // if sesnor detects inches = 0 turn conveyor off

{
digitalWrite(motorPin, LOW); // Conveyor belt is OFF
wristelevationservo.write(80); // Rotate servo 80*
delay(2000);
shoulderservo.write(10); //Rotate servo 10*
delay(2000);
elbowservo.write(105); //Rotate servo 100*
delay(2000);
gripperservo.write(88); //Rotate servo 88*
delay(2000);
elbowservo.write(125); //Rotate servo 110*
delay(2000);
waistservo.write(180); //Rotate servo 180*
delay(2000);
gripperservo.write(1); //Rotate servo 1*
delay(2000);
waistservo.write(90); //Rotate servo 90*
delay(1000);

}

delay(1000);

if (inches >0) // if inches greater than 0 turn motor on
{
digitalWrite(motorPin, HIGH);
}

}

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.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
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;
}

Hello Luke,
Welcome to the forum. What do you think 'how to use this forum - please read' means?

When you've worked that out and read it, come back and edit your post appropriately.

loop {
for( int i = 5; i; i-- ) {
// your code as published
}

// code for another robot task
}

Luke2431:
The operation i require is as follows;

  1. Run Coneyor belt.
  2. If sensor detects 0 inches stop conveyor belt.
  3. Robot arm picks object up and carries it to parts bin.
  4. Robot arm returns to start position and conveyor belt starts again.

At the completion of the 4th task increment a count variable. Don't start the series of tasks if the count is greater than 5.

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R