Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servo motor reacts to other things

Tags:

arduino

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;
    }
 }
like image 882
Sinan Samet Avatar asked Jan 14 '13 11:01

Sinan Samet


1 Answers

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:

  1. controlling power to the servo through something you can turn off/on (PWM, MOSFET someone else help here?)
  2. having a secondary power manual switch you can toggle once your controller is up and running.

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.

like image 50
Matthew Avatar answered Oct 13 '22 06:10

Matthew