Poll
Question: Is my code correct?
Yes - 1 (50%)
No - 1 (50%)
Total Voters: 2

Pages: [1]   Go Down
Author Topic: SERVO CODE SUGGESTIONS  (Read 277 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: