Go Down

Topic: SERVO CODE SUGGESTIONS (Read 347 times) previous topic - next topic

shrek007

Hi, Ive recently started a new arduino project. I want to control two servos manually using two potentiometers(10k). I have an arduino uno. I based the code off one of the servo example codes already in the software(knob). The servos seems to work fine individually. The problem starts when i connect them both and try to control the servos. The board "on" light starts to flicker as if its losing power. The servos eventually become weaker. Is my code off or am I just not wiring up my system correctly? The wiring seems pretty straightforward. Suggestions would be appreciated. You can see my code below.
Code: [Select]

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo mysecondservo;



int potpin = A0; // analog pin used to connect the potentiometer
int potpin2= A5;
int val;    // variable to read the value from the analog pin
int val2;


void setup()
{
 
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 mysecondservo.attach(7);
}

void loop()
{

 val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);   // scale it to use it with the servo (value between 0 and 180)
 myservo.write(val);
 
 val2 =analogRead(potpin2);
 val2 =map(val2,0, 1023, 0, 179);    // sets the servo position according to the scaled value
 mysecondservo.write(val2);



 delay(15);                           // waits for the servo to get there
}

johnwasser

The little DC motors in servos can draw quite a bit of power.  Sounds like your system can't provide enough power to keep everything running.

The solution is to provide a separate power source for the two servos.  Connect the power and ground wires of both servos to the external power source.  Hook the ground wires also to the Arduino ground.  Hook each of the signal wires to an Ardiuno output pin.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up