Jittery servos in my Kitty Candy Cannon

Hi!
First off, this is my first Arduino project and I'm in over my head with the electronics and could use some help.

The basic functions in my candy cannon is to randomize horizontal and vertical position, reload the cannon and fire a cat candy :slight_smile:

The components are:

  • Arduino Uno
  • Velleman Motor & Power-shield
  • Breadboard Power Supply Shield (5V/3.3V)
  • LEGO Power Functions M-Motor
  • LEGO Battery box (9V)
  • 2 x 800g micro servos
  • 1 x 4,1kg futaba servo
  • 1 x adafruit neopixel ring (24 diodes)

The positioning, firing and reloading all works but the servos jitter like crazy so I'm suspecting that my wiring is messed up somewhere.
I tried to make a fritzing sketech of what I've got. The left battery and motor is a lego motor and battery box:

And this is what it looks like:

The thing that annoys me most right now is that I've got three power sources. USB for the arduino, lego pack for the motor and a 9V battery for the breadboard. Is there any good way to get this down to one or two?

So in conlusion my problems are twitching servos and too many power sources. Would greatly epriciate any help!

And here's the code:

#include <Adafruit_NeoPixel.h>
#include <Button.h>
#include <Servo.h>

unsigned int buttonPin = 13;

unsigned int verticalServoPin = 6;
unsigned int horizontalServoPin = 5;
unsigned int reloadServoPin = 9;

const byte neoPixelPin = 10;
const byte neoPixelCount = 24;
const byte neoPixelBrightness = 100;

unsigned int motorPwm = 3;  //PWM control for motor output
unsigned int motorDirection = 2;  //direction control for motor output

Button startStop(buttonPin);

Servo horizontal;
Servo vertical;
Servo reload;

Adafruit_NeoPixel neoPixelRing = Adafruit_NeoPixel(neoPixelCount, neoPixelPin, NEO_BGR);

void setup()
{
	startStop.begin();

	neoPixelRing.begin();
	neoPixelRing.setBrightness(neoPixelBrightness);
	neoPixelRing.show();

	vertical.attach(verticalServoPin);
	horizontal.attach(horizontalServoPin);
	reload.attach(reloadServoPin);

	pinMode(motorPwm, OUTPUT);
	pinMode(motorDirection, OUTPUT);

	// Set starting position for cannon
	resetPosition();
}

void loop()
{
	if (startStop.pressed())
	{
		repositionAndFire();
	}
}

void repositionAndFire()
{
	spinNeoPixelRing();
	
	reloadCannon();

	moveServoRandom(vertical);
	moveServoRandom(horizontal);
	
	fireCannon();
	resetPosition();
}

void resetPosition()
{
	moveServo(vertical, 180);
	moveServo(horizontal, 90);
}

void reloadCannon()
{
	moveServo(reload, 30);
	moveServo(reload, 135);
}

void moveServoRandom(Servo servo)
{
	unsigned int degree = random(0, 180);

	moveServo(servo, degree);
}

void moveServo(Servo servo, int position)
{
	position = map(position, 0, 180, 20, 140);
	servo.write(position);
	delay(1500);
}

void fireCannon()
{
	// Run motor to pull spring back and fire
	digitalWrite(motorDirection, LOW);
	analogWrite(motorPwm, 255);

	// Approximately one revolution of the trigger wheel
	delay(2300);
	analogWrite(motorPwm, 0);
}

void spinNeoPixelRing()
{
	for (int i = 0; i < neoPixelCount; i++) 
	{
		neoPixelRing.setPixelColor(i, neoPixelRing.Color(255, 0, 0));
		neoPixelRing.show();
		delay(42);
	}

	for (int i = 0; i < neoPixelCount; i++)
	{
		neoPixelRing.setPixelColor(i, neoPixelRing.Color(0, 0, 0));
		neoPixelRing.show();
		delay(42);
	}
}

/Peter

but the servos jitter like crazy so I'm suspecting that my wiring is messed up somewhere.

It's more likely that the problem is an inadequate power supply for the servos. "Breadboard Power Supply Shield (5V/3.3V)" doesn't tell us near enough. What is it powering? How many amps is it capable of supplying? What voltage do the servos really want?

PaulS:
It's more likely that the problem is an inadequate power supply for the servos. "Breadboard Power Supply Shield (5V/3.3V)" doesn't tell us near enough. What is it powering? How many amps is it capable of supplying? What voltage do the servos really want?

It can deliver a max load of 700 mA and it's powering two 800g micro servos (4,8V) and one 4,1kg servo (4.8V - 6.0V) and the neopixel ring (5V)

But you think it's a power issue? That's a good place for me to start looking then, thanks!

The usual rule-of-thumb is to provide 1 A per servo. 3 servos == 3 A, not 3/4 A

The micro servos may need that much current. Without knowing which servos you really are using, it's hard to know exactly how much they draw.

On the other hand, more (current capability) that is not used is better than not enough.

I tried to ammend the power issue by only running one servo at a time but they're still quite jittey. Wouldn't the combined max draw only come into effect if I ran them all att once?

Wouldn't the combined max draw only come into effect if I ran them all att once?

No. Servos draw power all the time, not just when moving.

PaulS:
Without knowing which servos you really are using, it's hard to know exactly how much they draw.

The two small ones are Tower pro sg90 and the big one is a Futaba S3003.

PaulS:
No. Servos draw power all the time, not just when moving.

Sounds like I need to add more power somehow then.

And I'll try to include all details in my question from the start next time. Thanks for all your help!

/Peter

Hi,
Write a simple bit of code that makes one servo swing back and forth, stopping at 90 on each swing.
See if it is jittery,
If not then get two servos to do it, then three.
You are doing a lot in your code.

Did you write it a stage at a time.

  • Write code to control a servo, when it works.
  • Write code to get the other servo working, when that works.
  • Write code to control the motor, when that works.
  • Write code to control pixel ring, when that works.
  • Combine the servo and motor code, when that works.
  • Combine the pixel ring code, get that working.

I know it sounds long and involved, but it will shorten your development time and lower the frustration of debugging.

At what stage did the servo begin to jitter.

Tom.... :slight_smile: