Servos not responding to Arduino

I simply want 4 servos to move according to a few potentiometers (as shown in the code below). I’m using a mega2560.

Basically this is what I want to happen:

  1. servo1 and servo2 both move to angle 135.
  2. servo3 and servo4 to move to angle 90.
  3. then I want servo1 and servo2 to move in opposite angles according to the value of the potentiometer.
  4. same for servo3 and servo4.

This is was I get: (the number 1. from last point relates to 1. in this point and so on)

  1. servo1 goes to angle 0 or 270 (its either extreme), servo2 goes to 135.
  2. both servos 3 and 4 take micro angles back and forth and start heating up.
  3. they don’t respond.
  4. they dont respond.

I would like to know if there is an error in the code or if I must check the wiring.
thanks

#include <Servo.h>
int val;
#define potservo1 A2
#define potservo3 A3
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int val2;
int val3;
int angleServo1 = 135;
int angleServo2 = 135;
int angleServo3 = 90;
int angleServo4 = 90;


//using mega2560
void setup() {
  // put your setup code here, to run once:
  servo1.attach(8);
  servo1.write(angleServo1);
  servo2.attach(9);
  servo3.attach(10);
  servo3.write(angleServo3);
  servo4.attach(11);
  servo4.write(angleServo4);


}


void loop() {
  // put your main code here, to run repeatedly:
  val2 = analogRead(potservo1);            
  val2 = map(val2, 0, 1023, 60, 180);  
  servo1.write(val2);  
  servo2.write(-(val2));
  delay(15);
  


  val3 = analogRead(potservo3);
  val3 = map(val3, 0, 1023, 0, 180);
  servo3.write(val3);
  servo4.write(-(val3));
  delay(15);




}

It would help all of us if you included a schematic, not a frizzy thing. Is there an error, Yes/No depends on what you intended the code to do. Turn it on and determine if it is operating properly or if not what the problem is.

I'll run a test code tomorrow to verify if my wiring is the issue, although after checking with a DMM, by measuring the resistance, it all looked ok.

I simply want to get the servos moving based off a potentiometer, I'll solve for the angles after I get them moving.

I already stated all the issue that were occurring, I'll reword it then:

None of the servos are moving based off the input of the potentiometers.

What happens once I turn on power:
servo1 moves to angle either 0 or 270, then doesn't move after that.
servo2 moves to some angle close to 135 if not 135 and then doesn't move after that.
servo3 and 4 just vibrate as if they are going back and forth and heat up.

thanks

Connect a wire from the servo power supply negative (-) to Arduino GND. You don't have a return path for the signals from Arduino to servo.

Servo.write() does not take negative angles. If you want opposite movements that's usually write(x) and write(180-x). That's one immediate problem, there may be others.

Steve

JCA34F:
Connect a wire from the servo power supply negative (-) to Arduino GND. You don’t have a return path for the signals from Arduino to servo.

Thanks, by connecting all the potentiometer to the GND of the Arduino, it worked. Before I just had them connected to the GND on the power supply that was powering the Arduino.

slipstick:
Servo.write() does not take negative angles. If you want opposite movements that’s usually write(x) and write(180-x). That’s one immediate problem, there may be others.

Steve

Thanks, I changed that to 180-x, and it works as it should.
Thanks everyone for you help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.