1

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.

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();
} 
1
  • It would be good @Salma if you accepted an answer that works for your question. To let others know which answer solved your problem. Thanks.
    – st2000
    Commented May 26 at 1:11

2 Answers 2

1

You have all your code inside the loop() function. This function - as its name suggests - gets called over and over again in a loop. When you call return inside this function, you leave the function. But almost immediately the function loop() is called again, starting your code all over.

You have two options:

  • Replace the return statement with an infinite loop, which does nothing. Something like:

      while(true);
    
  • Move all your code into the function setup(). This function is only called by the Arduino core once at startup. Leave the loop() function empty.

Both options are effectively the same: Executing your code after startup and then do an infinite empty loop. The first option is how people do it outside of the Arduino framework. The second option is the Arduino way of doing this. For readability I would suggest the second option.

0

In normal C programming the entry point is main(). That is, main() is called once and execution continues from there. If your program was renamed main() and dropped into a normal C programming environment it has a good chance to work as expected.

In the Arduino paradigm there are 2 entry points. A function called setup() which is called once. And a function called loop() which is called repetitively. The assumption is that the latter method is easier for beginner programmers to understand.

What you have done is created and initialized your key variables which track the state of your program in the Arduino loop() function. This function will be called over and over again by the included Arduino software uploaded each time with your software to the processor. This causes your key variables to be recreated and reinitialized each time the function loop() is called.

Consider moving the creation and initialization of your key variables to the setup() function. In this way they will only be created and initialized once.

setup()
{
  // Move your one time initialization code here from your
  // existing loop() function, 
  //
  // like this:

  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
}

loop()
{
  // Leave the remainder of your code in the loop() function:

  while (enterServo1Loop || enterServo2Loop) {

   // Control Servo 1
   if (enterServo1Loop && currentCycle1 < cycles) {
   // ... rest of your code.
}
1
  • I corrected the answer WRT what you said @thebusybee.
    – st2000
    Commented May 22 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.