I am working with Arduino, I connected a servo motor and a normal motor. They both work but when I start the normal motor script, the servo motor does small spastic stuff. Could anyone help me with this?
// Includes
#include <Servo.h>
// Aanmaken van de variabelen voor in de code
int ledPin = 13;
const int motorPin = 2;
int usbnumber = 0;
Servo stuurServo; // create servo object to control a servo
int pos = 90; // variable to store the servo position
// De eerste setup maken
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(motorPin, OUTPUT);
stuurServo.attach(12);
Serial.begin(9600);
stuurServo.write(pos);
}
void loop()
{
if (Serial.available() > 0) {
usbnumber = Serial.read();
}
if (usbnumber > 0) {
if (usbnumber == 1){ // Lampje knipperen
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(500);
}else if(usbnumber == 2){ // Motor aan voor 5 seconden
digitalWrite(motorPin, HIGH);
delay(20000);
digitalWrite(motorPin, LOW);
}else if(usbnumber == 3){ // stuur servo +10 graden
if(pos != 180){
pos + 10;
stuurServo.write(pos);
}
}else if(usbnumber == 4){ // stuur servo -10 graden
if(pos != 0){
pos - 10;
stuurServo.write(pos);
}
}else if(usbnumber == 5){ // stuur servo liks
pos = 0;
stuurServo.write(pos);
}else if(usbnumber == 6){ // stuur servo rechts
pos = 180;
stuurServo.write(pos);
}else{
delay(500);
}
usbnumber = 0;
}
}
Most (hobby) servo motors will twitch or give a little jerk when they are powered up, especially if you power the motor before driving the servo (providing a position control signal). The solution is to write to the control line to the servo before allowing power to it. Some easy solutions include:
There is basically nothing you can do in code without some way to have the circuit start up with no power to the Servo until you are driving the position control line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With