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
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