Skip to main content
removed irrelevant text
Source Link
jsotola
  • 1.6k
  • 2
  • 13
  • 22

My name is Salma and I need a help in my code for my graduation project. I made code to make 2 servos work. the sequence of working is to make the servo 1 start rotate 2 cycles then stop and the second servo start rotate for another 2 cycles then detach both of them. I tried so hard to stop the loop, but it stuck inside and didn't get out. It continued to rotate the first, then the second, then back to the first, and so on.

here is the code,

My name is Salma and I need a help in my code for my graduation project. I made code to make 2 servos work. the sequence of working is to make the servo 1 start rotate 2 cycles then stop and the second servo start rotate for another 2 cycles then detach both of them. I tried so hard to stop the loop, but it stuck inside and didn't get out. It continued to rotate the first, then the second, then back to the first, and so on.

here is the code,

I made code to make 2 servos work. the sequence of working is to make the servo 1 start rotate 2 cycles then stop and the second servo start rotate for another 2 cycles then detach both of them. I tried so hard to stop the loop, but it stuck inside and didn't get out. It continued to rotate the first, then the second, then back to the first, and so on.

Applied code format to code section to make it more readable.
Source Link
st2000
  • 7.5k
  • 2
  • 13
  • 20

''' void loop() {

const int totalLoopTime = 32001; // Time in milliseconds for the entire loop (adjust as needed)

// Control variables bool enterServo1Loop = true; bool enterServo2Loop = false; int currentCycle1 = 0; int currentCycle2 = 0;

// Target cycles const int cycles = 2; // Adjust this value as needed

// Servo control parameters const int servoPin1 = 9; // Adjust pin numbers as necessary const int servoPin2 = 6; const int openPosition = 45; // Adjust for your servo's open position const int closePosition = 135; // Adjust for your servo's closed position const int openTime = 4000; // Adjust open time in milliseconds const int closeTime = 4000; // Adjust close time in milliseconds

while (enterServo1Loop || enterServo2Loop) {

void loop() {

  const int totalLoopTime = 32001; // Time in milliseconds for the entire loop (adjust as needed)

  // Control variables
  bool enterServo1Loop = true;
  bool enterServo2Loop = false;
  int currentCycle1 = 0;
  int currentCycle2 = 0;

  // Target cycles
  const int cycles = 2; // Adjust this value as needed

  // Servo control parameters
  const int servoPin1 = 9;    // Adjust pin numbers as necessary
  const int servoPin2 = 6;
  const int openPosition = 45; // Adjust for your servo's open position
  const int closePosition = 135;  // Adjust for your servo's closed position
  const int openTime = 4000;   // Adjust open time in milliseconds
  const int closeTime = 4000;  // Adjust close time in milliseconds


 


  while (enterServo1Loop || enterServo2Loop) {

    // Control Servo 1
    if (enterServo1Loop && currentCycle1 < cycles) {
      servo1.attach(servoPin1);
      servo1.write(openPosition);
      delay(openTime);
      servo1.write(closePosition);
      delay(closeTime);
      currentCycle1++;
    } else if (enterServo1Loop) { // Detach after finishing cycles
      servo1.detach();
      enterServo1Loop = false;
      enterServo2Loop = true; // Start Servo 2 loop after finishing Servo 1
      Serial.print("Time servo 1: ");  // Add space after colon for readability
      Serial.println(millis());
      

    }

    // Control Servo 2
    if (enterServo2Loop && currentCycle2 < cycles) {
      servo2.attach(servoPin2);
      servo2.write(openPosition);
      delay(openTime);
      servo2.write(closePosition);
      delay(closeTime);
      currentCycle2++;
    } else if (enterServo2Loop) { // Detach after finishing cycles
      servo2.detach();
      enterServo2Loop = false;
      enterServo1Loop = false;
      servo1.detach();

      Serial.print("Time servo 2: ");  // Add space after colon for readability
      Serial.println(millis());
    
    }

// if(millis()>=50000) { // servo1.detach(); // servo2.detach(); } }

if (millis() >= loopStartTime + totalLoopTime) { servo1.detach(); // Detach both servos when time limit is reached servo2.detach(); bool enterServo1Loop = false; bool enterServo2Loop = false; Serial.print("Time last: "); // Add space after colon for readability Serial.println(millis());

  
   // if(millis()>=50000)  {
     //   servo1.detach();
       // servo2.detach();  }
  }

  if (millis() >= loopStartTime + totalLoopTime) {
    servo1.detach();  // Detach both servos when time limit is reached
    servo2.detach();
    bool enterServo1Loop = false;
    bool enterServo2Loop = false;
      Serial.print("Time last: ");  // Add space after colon for readability
      Serial.println(millis());

    return;  // Exit the loop immediately
  }


  servo1.detach(); 
  servo2.detach();
} 

}

servo1.detach(); servo2.detach(); } '''

''' void loop() {

const int totalLoopTime = 32001; // Time in milliseconds for the entire loop (adjust as needed)

// Control variables bool enterServo1Loop = true; bool enterServo2Loop = false; int currentCycle1 = 0; int currentCycle2 = 0;

// Target cycles const int cycles = 2; // Adjust this value as needed

// Servo control parameters const int servoPin1 = 9; // Adjust pin numbers as necessary const int servoPin2 = 6; const int openPosition = 45; // Adjust for your servo's open position const int closePosition = 135; // Adjust for your servo's closed position const int openTime = 4000; // Adjust open time in milliseconds const int closeTime = 4000; // Adjust close time in milliseconds

while (enterServo1Loop || enterServo2Loop) {

// Control Servo 1
if (enterServo1Loop && currentCycle1 < cycles) {
  servo1.attach(servoPin1);
  servo1.write(openPosition);
  delay(openTime);
  servo1.write(closePosition);
  delay(closeTime);
  currentCycle1++;
} else if (enterServo1Loop) { // Detach after finishing cycles
  servo1.detach();
  enterServo1Loop = false;
  enterServo2Loop = true; // Start Servo 2 loop after finishing Servo 1
  Serial.print("Time servo 1: ");  // Add space after colon for readability
  Serial.println(millis());
  

}

// Control Servo 2
if (enterServo2Loop && currentCycle2 < cycles) {
  servo2.attach(servoPin2);
  servo2.write(openPosition);
  delay(openTime);
  servo2.write(closePosition);
  delay(closeTime);
  currentCycle2++;
} else if (enterServo2Loop) { // Detach after finishing cycles
  servo2.detach();
  enterServo2Loop = false;
  enterServo1Loop = false;
  servo1.detach();

  Serial.print("Time servo 2: ");  // Add space after colon for readability
  Serial.println(millis());

}

// if(millis()>=50000) { // servo1.detach(); // servo2.detach(); } }

if (millis() >= loopStartTime + totalLoopTime) { servo1.detach(); // Detach both servos when time limit is reached servo2.detach(); bool enterServo1Loop = false; bool enterServo2Loop = false; Serial.print("Time last: "); // Add space after colon for readability Serial.println(millis());

return;  // Exit the loop immediately

}

servo1.detach(); servo2.detach(); } '''

void loop() {

  const int totalLoopTime = 32001; // Time in milliseconds for the entire loop (adjust as needed)

  // Control variables
  bool enterServo1Loop = true;
  bool enterServo2Loop = false;
  int currentCycle1 = 0;
  int currentCycle2 = 0;

  // Target cycles
  const int cycles = 2; // Adjust this value as needed

  // Servo control parameters
  const int servoPin1 = 9;    // Adjust pin numbers as necessary
  const int servoPin2 = 6;
  const int openPosition = 45; // Adjust for your servo's open position
  const int closePosition = 135;  // Adjust for your servo's closed position
  const int openTime = 4000;   // Adjust open time in milliseconds
  const int closeTime = 4000;  // Adjust close time in milliseconds


 


  while (enterServo1Loop || enterServo2Loop) {

    // Control Servo 1
    if (enterServo1Loop && currentCycle1 < cycles) {
      servo1.attach(servoPin1);
      servo1.write(openPosition);
      delay(openTime);
      servo1.write(closePosition);
      delay(closeTime);
      currentCycle1++;
    } else if (enterServo1Loop) { // Detach after finishing cycles
      servo1.detach();
      enterServo1Loop = false;
      enterServo2Loop = true; // Start Servo 2 loop after finishing Servo 1
      Serial.print("Time servo 1: ");  // Add space after colon for readability
      Serial.println(millis());
      

    }

    // Control Servo 2
    if (enterServo2Loop && currentCycle2 < cycles) {
      servo2.attach(servoPin2);
      servo2.write(openPosition);
      delay(openTime);
      servo2.write(closePosition);
      delay(closeTime);
      currentCycle2++;
    } else if (enterServo2Loop) { // Detach after finishing cycles
      servo2.detach();
      enterServo2Loop = false;
      enterServo1Loop = false;
      servo1.detach();

      Serial.print("Time servo 2: ");  // Add space after colon for readability
      Serial.println(millis());
    
    }
  
   // if(millis()>=50000)  {
     //   servo1.detach();
       // servo2.detach();  }
  }

  if (millis() >= loopStartTime + totalLoopTime) {
    servo1.detach();  // Detach both servos when time limit is reached
    servo2.detach();
    bool enterServo1Loop = false;
    bool enterServo2Loop = false;
      Serial.print("Time last: ");  // Add space after colon for readability
      Serial.println(millis());

    return;  // Exit the loop immediately
  }


  servo1.detach(); 
  servo2.detach();
} 
Source Link
Salma
  • 11
  • 1

Can't exit while loop

My name is Salma and I need a help in my code for my graduation project. I made code to make 2 servos work. the sequence of working is to make the servo 1 start rotate 2 cycles then stop and the second servo start rotate for another 2 cycles then detach both of them. I tried so hard to stop the loop, but it stuck inside and didn't get out. It continued to rotate the first, then the second, then back to the first, and so on.

here is the code,

''' void loop() {

const int totalLoopTime = 32001; // Time in milliseconds for the entire loop (adjust as needed)

// Control variables bool enterServo1Loop = true; bool enterServo2Loop = false; int currentCycle1 = 0; int currentCycle2 = 0;

// Target cycles const int cycles = 2; // Adjust this value as needed

// Servo control parameters const int servoPin1 = 9; // Adjust pin numbers as necessary const int servoPin2 = 6; const int openPosition = 45; // Adjust for your servo's open position const int closePosition = 135; // Adjust for your servo's closed position const int openTime = 4000; // Adjust open time in milliseconds const int closeTime = 4000; // Adjust close time in milliseconds

while (enterServo1Loop || enterServo2Loop) {

// Control Servo 1
if (enterServo1Loop && currentCycle1 < cycles) {
  servo1.attach(servoPin1);
  servo1.write(openPosition);
  delay(openTime);
  servo1.write(closePosition);
  delay(closeTime);
  currentCycle1++;
} else if (enterServo1Loop) { // Detach after finishing cycles
  servo1.detach();
  enterServo1Loop = false;
  enterServo2Loop = true; // Start Servo 2 loop after finishing Servo 1
  Serial.print("Time servo 1: ");  // Add space after colon for readability
  Serial.println(millis());
  

}

// Control Servo 2
if (enterServo2Loop && currentCycle2 < cycles) {
  servo2.attach(servoPin2);
  servo2.write(openPosition);
  delay(openTime);
  servo2.write(closePosition);
  delay(closeTime);
  currentCycle2++;
} else if (enterServo2Loop) { // Detach after finishing cycles
  servo2.detach();
  enterServo2Loop = false;
  enterServo1Loop = false;
  servo1.detach();

  Serial.print("Time servo 2: ");  // Add space after colon for readability
  Serial.println(millis());

}

// if(millis()>=50000) { // servo1.detach(); // servo2.detach(); } }

if (millis() >= loopStartTime + totalLoopTime) { servo1.detach(); // Detach both servos when time limit is reached servo2.detach(); bool enterServo1Loop = false; bool enterServo2Loop = false; Serial.print("Time last: "); // Add space after colon for readability Serial.println(millis());

return;  // Exit the loop immediately

}

servo1.detach(); servo2.detach(); } '''