Hello there,
For a project me and my wife are building a "drumming" monkey using servo motors and an Arduino Uno board.
I've got a background in PLC/industrial programming and one day I also saw some C# code somewhere, so I decided to help her.
Basically, we've got 4 servo motors with very light wooden sticks attached, moving in a certain rhythm.
Our biggest problem is that the servo's shake violently, especially when idle between movements.
This wasn't the case, or maybe just not noticable when the servo's didn't have anything attached.
I've done my homework before posting here and tried various methods (most found on this forum), such as:
-connecting the grounds of my external power supply & Arduino
-trying to put various capacitors between the power supply + and -, also between the - and servo signal
-attaching/detaching the servo's when possible (only improved idle shaking, not while moving)
I'll post my code below, but there's been many changes in it for fixing other problems we ran into, so it's not very clean and there might be things we've overlooked.
I looked up most of the code online and changed it so it would fit our project. Most of it is just actual movements, nothing about them is definitive, this was mostly for testing.
It's a rather simple program but seeing as we're both very new to this, any help would be GREATLY appreciated!
Thanks in advance!
Components used;
- Arduino UNO
- External power supply (5V 2A (reused Philips charger/adapter)
- 4 Servo motors (6V) Gotron | Analog micro servo 1.8kg/cm 120° rotation. | Elektronicaspecialist
- A potentiometer to change the speed of the cycle
- A start button (we removed the stop function during some issues, and now simply stop using the reset pin)
- A 3-way switch to choose between 2 different rhythms/modes
#include <VarSpeedServo.h>
#include <ezButton.h>
VarSpeedServo servoArmL;
VarSpeedServo servoArmR;
VarSpeedServo servoBeenL;
VarSpeedServo servoBeenR;
int TEN = 10;
int DER = 30;
int ZES = 60;
int HND = 100;
const int buttonPin2 = 11;
int buttonState1 = 0;
int buttonState2 = 0;
const int analogInPin = A0;
const int analogOutPin = 9;
int sensorValue = 0;
int potValue = 0;
ezButton button(2);
void setup() {
servoArmL.attach(3);
servoArmR.attach(5);
servoBeenL.attach(6);
servoBeenR.attach(8);
pinMode (buttonPin2, OUTPUT);
while (!button.isPressed())
button.loop();
}
void loop() {
buttonState2 = digitalRead(buttonPin2);
sensorValue = analogRead(analogInPin);
potValue = map(sensorValue, 0, 1023, 100, 255);
analogWrite(analogOutPin, potValue);
delay(2);
if (buttonState2 ==HIGH) {
for (int a = 0; a <= 2; a++) { // 2x cycle
for (int b = 0; b <= 4; b++) {// 4x part 1 of cycle
servoArmR.write(ZES, potValue);
servoBeenL.write(ZES, potValue);
delay(150);
servoArmR.write(TEN, potValue);
servoBeenL.write(TEN, potValue);
servoArmL.write(ZES, potValue);
servoBeenR.write(ZES, potValue);
delay(150);
servoArmL.write(TEN, potValue);
servoBeenR.write(TEN, potValue);
delay(150);
}
for (int c = 0; c <= 2; c++) { //....
servoArmR.write(ZES, potValue);
servoBeenR.write(ZES, potValue);
servoArmL.write(TEN, potValue);
servoBeenL.write(TEN, potValue);
servoArmR.wait();
delay(10);
servoArmR.write(TEN, potValue);
servoBeenR.write(TEN, potValue);
servoArmL.write(ZES, potValue);
servoBeenL.write(ZES, potValue);
servoArmL.wait();
delay(10);
}
for (int d = 0; d <= 1; d++) {
servoArmR.write(ZES, potValue);
servoArmL.write(TEN, potValue);
servoArmR.wait();
delay(10);
servoArmR.write(TEN, potValue);
servoArmL.write(ZES, potValue);
servoArmR.wait();
delay(10);
}
}
for (int e = 0; e <= 2; e++) {
servoArmR.write(ZES, potValue);
servoArmL.write(TEN, potValue);
delay(10);
servoArmR.write(TEN, potValue);
servoArmR.write(ZES, potValue);
delay(10);
}
for (int f = 0; f <= 4; f++) {
servoArmR.write(ZES, potValue);
servoBeenR.write(ZES, potValue);
servoArmL.write(TEN, potValue);
servoArmR.wait();
delay(10);
servoArmR.write(TEN, potValue);
servoBeenR.write(TEN, potValue);
servoArmL.write(ZES, potValue);
servoBeenL.write(ZES, potValue);
servoArmL.wait();
delay(10);
servoArmR.write(TEN, potValue);
servoBeenL.write(TEN, potValue);
}
}else{
for (int q = 0; q <= 2; q++) {
for (int i = 0; i <= 2; i++) {
servoArmR.write(TEN, potValue);
servoArmL.write(HND, potValue);
servoArmL.wait();
delay(150);
servoArmR.write(HND, potValue);
servoArmL.write(TEN, potValue);
servoArmR.wait();
delay(150);
}
for (int k = 0; k <= 3; k++) {
servoArmR.write(TEN, potValue);
servoBeenR.write(TEN, potValue);
servoArmL.write(HND, potValue);
delay(150);
servoArmR.write(HND, potValue);
servoBeenR.write(ZES, potValue);
servoArmL.write(TEN, potValue);
delay(150);
servoBeenR.write(TEN, potValue);
}
for (int l = 0; l <= 3; l++) {
servoArmR.write(TEN, potValue);
servoBeenR.write(TEN, potValue);
delay(150);
servoArmR.write(HND, potValue);
servoBeenR.write(ZES, potValue);
delay(150);
servoBeenR.write(TEN, potValue);
}
}
}
Shake_shake_shake.ino (4.85 KB)