Code not looping properly

Hey All,

I am having some trouble with my code and I could use some advice as to how I need to proceed. The overview of the project is that I have two boxes, one box with four servos and an ultrasonic sensor in it and another box with two servos and the board inside of it. The board is a Mega 2560.

The four servo box is supposed to open four doors, wait 2500 milliseconds, and then close and wait for 2900 milliseconds. It should do this continually until the ultrasonic sensor detects something close at which point the four servo box should close, and the two servo box will open and close fast (around 1000 milliseconds). Then I want it to cycle back to opening and closing the four servo box until the sensor is tripped again.

The problem that I am having is that after a couple of cycles of tripping the sensor and the two servo box rapidly opening and closing, the four servo box starts "freaking out" and will open and close rapidly a few times and then the servos stay open for a few seconds (10-15) and then it shuts. It will not do anything more until I unplug it from all power and then plug it back in or hit the reset button.

This is my first post on the forum, so I hope that I have inserted the code correctly and I appreciate any help that you can give me.


    // Include the Servo library   
#include <Servo.h> 
   
    //declare pins 
int servoPin1 = 52; 
int servoPin2 = 50;
int servoPin3 = 48;
int servoPin4 = 46;
int servoPin5 = 44;
int servoPin6 = 42;

int trigPin=24;
int echoPin=22;
int pingTravelTime;

    //rename servos
Servo Servo1; 
Servo Servo2;
Servo Servo3;
Servo Servo4;
Servo Servo5;
Servo Servo6;

    //initialize and attach components
void setup() { 
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
   Servo1.attach(servoPin1);
   Servo2.attach(servoPin2);
   Servo3.attach(servoPin3);
   Servo4.attach(servoPin4); 
   Servo5.attach(servoPin5);
   Servo6.attach(servoPin6);
}

    // running continous code
void loop()
{ 
   long duration, distance;
   digitalWrite(trigPin,HIGH);
   delay(15);
   digitalWrite(trigPin,LOW);
   duration=pulseIn(echoPin,HIGH);
   distance =(duration/2)/29.1;
   delay(15);

   pinMode(echoPin,INPUT);
   duration = pulseIn(echoPin,HIGH);

   Serial.print(distance);
   Serial.println();

if (distance > 30)
{
  Servo1.write(0);
   Servo3.write(0);
   Servo2.write(0);
   Servo4.write(0);
   delay(2500);

   Servo1.write(50);
   Servo3.write(50);
   Servo2.write(50);
   Servo4.write(50);
   delay(2900);
}
 else
{
   Servo1.write(0);
   Servo3.write(0);
   Servo2.write(0);
   Servo4.write(0);
   delay(1000); 

   Servo5.write(0);
   Servo6.write(0);
   delay(1000);

   Servo5.write(50);
   Servo6.write(50);
   delay(1000);

      Servo5.write(0);
   Servo6.write(0);
   delay(1000);

   Servo5.write(50);
   Servo6.write(50);
   delay(1000);

      Servo5.write(0);
   Servo6.write(0);
   delay(1000);

   Servo5.write(50);
   Servo6.write(50);
   delay(1000);

      Servo5.write(0);
   Servo6.write(0);
   delay(1000);

   Servo5.write(50);
   Servo6.write(50);
   delay(1000);

      Servo5.write(0);
   Servo6.write(0);
   delay(1000);

   Servo5.write(50);
   Servo6.write(50);
   delay(1000);

      Servo5.write(0);
   Servo6.write(0);
   delay(1000);

   Servo5.write(50);
   Servo6.write(50);
   delay(1000);

      Servo5.write(0);
   Servo6.write(0);
   delay(1000);

   Servo5.write(50);
   Servo6.write(50);
   delay(1000);

      Servo5.write(0);
   Servo6.write(0);
   delay(1000);

   Servo5.write(50);
   Servo6.write(50);
   delay(10000);
}
}

How are your servos powered?

Hey, sorry I forgot to include it in my post. I am using the wall plug to power the board.

OK, but where exactly are the servos getting their power from ?

Sorry, maybe I am not understanding correctly. the four servo box has the servos running to a small breadboard where I have run the ground jumper and the 5v jumper to. Then the signal cord for each of the servos, plus the single 5v wire and a single Ground wire run about 3ft to the two servo box where they connect to the Mega Board. I hope that makes sense, and helps you understand. If you need, I can draw out a diagram of how its all connected.

What we're poking at is that the Arduino cannot provide enough current to power servos. They need a separate power supply. Connect the grounds.

Ok, thank you for all of your help. I really appreciate it. I'll setup some separate power through a 9volt and a resistor and see what happens.

Depends what you mean by a nine volt. If it's a battery that you might use in a smoke detector, that's right out too.

Oh, ok. What would you suggest I use as a power source then ?

I thought that since each of the mini servos ran off of 5v, that I would be able to connect a 9v to each of their individual rails with a resistor in between to step down the power. Is that not possible ?

Rule of thumb for servos is an Amp each. It sounds like yours don't pull quite that much, but you need a supply that can provide 5V and at least 4A I would guess. More current capability would be nice.

Smoke detector batteries provide very little current at all, and even if they could power a single servo, it wouldn't be for very long.

If you're determined to use batteries, a pack of AAs is a much better bet, assuming your servos can tolerate 6V.

Well, this may have answered several other questions that I have been running into over the past couple of months..... I really appreciate your help !

I have been trying to run multiple servos by powering the board with the wall plug and assuming the problems I have had have been from the code. I just assumed that the plug was providing ample power, not taking into consideration how much power they each used. Thank you very much for all your help !

You can not use resistors to reduce the voltage for power hungry devices like motors. It only works for signals (data), not power.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the IDE 1.x category.

Thanks for using code tags in your first post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.