title: How to make code repeat for a not predefined amount of times
body: <p>As a project for school I have a touch sensor as an on/off switch connected to an arduino.When touched once it starts the motors, the servo and the ultrasonic sensor that are connected.Then when the sensor is touched again they all stop.My problem is that i cant make to servo and the ultrasonic repeatthe process of turning and calculating the distance more then once and i can't use loop since i can't know for how long will the circuit be running and if i do after the button is pressed for the seccond time i will have to wait for the amount of loops i have inserted to end before everything also stops.</p>
  
  <p>Here is the code so far:</p>
  
  <pre><code> #include &lt;Servo.h&gt;
      Servo servo; 
      int pos = 0;
      const int trigPin = 4;
      const int echoPin = 5; 
      long duration;
      int distance; 
  
  
      #define TouchSensor 13 
      int relay = 2; 
       // motor one
      int enA = 10;
      int in1 = 6;
      int in2 = 7;
      // motor two
      int enB = 5;
      int in3 = 8;
      int in4 = 9;
  
      boolean currentState = LOW;
      boolean lastState = LOW;
      boolean RelayState = LOW;
  
      void setup() {
        Serial.begin(9600);
        pinMode(enA, OUTPUT);  
        pinMode(enB, OUTPUT);  
        pinMode(in1, OUTPUT);
        pinMode(in2, OUTPUT);
        pinMode(in3, OUTPUT);
        pinMode(in4, OUTPUT);
        pinMode(TouchSensor, INPUT);
        servo.attach(3);
        pinMode(trigPin, OUTPUT); 
        pinMode(echoPin, INPUT);
      }
  
      void loop() {
        currentState = digitalRead(TouchSensor);
          if (currentState == HIGH &amp;&amp; lastState == LOW){
          Serial.println("pressed");
          digitalWrite(in1, LOW);
          digitalWrite(in2, HIGH);  
          digitalWrite(in3, HIGH);
          digitalWrite(in4, LOW);
  
          servo.write(40);
       digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance= duration*0.034/2;
      Serial.print("Distance: ");
      Serial.println(distance);
       delay(500);
       servo.write(80);
       digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance= duration*0.034/2;
      Serial.print("Distance: ");
      Serial.println(distance);
       delay(500);
       servo.write(120);
       digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance= duration*0.034/2;
      Serial.print("Distance: ");
      Serial.println(distance);
       delay(500);
       servo.write(160);
       digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance= duration*0.034/2;
      Serial.print("Distance: ");
      Serial.println(distance);
      delay(500);
  
        delay(1);
  
          if (RelayState == HIGH){
            digitalWrite(relay, LOW);
            RelayState = LOW;
          digitalWrite(in1, LOW);
          digitalWrite(in2, LOW);  
          digitalWrite(in3, LOW);
          digitalWrite(in4, LOW); 
          } else {
            digitalWrite(relay, HIGH);
            RelayState = HIGH;
          }
        }
        lastState = currentState;
      }
  </code></pre>
  
  <p>Thanks in advance. Sorry for any mistakes, english is not my first language.</p>