Need help with servo motors and arduino nano

Hey everyone, so I have built a robot with two mini servo motors an arduino nano and a ultrasonic distance measuring sensor. so I have the robot running like a front wheel drive car at the moment with one swivel back tire. However I need the motors to be going in the same direction and they are not. and when I put in the code for the servo motors to go in the opposite direction that motor just stops working.

This is my code for a robot that avoids obstacles. Please let me know if you can help me get the servo motors going in the right direction.

thank you

#include <Servo.h>

Servo servo1;
Servo servo2;
int pin_trig = 11;
int pin_echo = 12;

void setup(){
servo1.attach(7);
servo2.attach(8);
pinMode (pin_trig, OUTPUT);
pinMode (pin_echo, INPUT);

Serial.begin(9600);
}

void loop() {

long duration, distance;
digitalWrite(pin_trig, LOW);
delayMicroseconds(2);
digitalWrite(pin_trig, HIGH);

delayMicroseconds(10);
digitalWrite(pin_trig, LOW);
duration = pulseIn (pin_echo, HIGH);
distance = (duration/2) / 29.1;
if (distance < 25){
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
servo1.write (0);
servo2.write (360);
}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
servo1.write(-360);
servo2.write(360);
}

}

 servo1.write(-360);
  servo2.write(360);

Usually, you would use 0 to rotate one direction, and 180 to rotate in the other.

Negative "degrees" probably clip to zero anyway - you'd have to check the source.

TolpuddleSartre:

 servo1.write(-360);

servo2.write(360);




Usually, you would use 0 to rotate one direction, and 180 to rotate in the other.

Negative "degrees" probably clip to zero anyway - you'd have to check the source.

but if I put 0 it just turns off the motor completely

Try a range of values - a sweep, from say, 45 to 135 "degrees" .
See how the motor reacts.

TolpuddleSartre:
Try a range of values - a sweep, from say, 45 to 135 "degrees" .
See how the motor reacts.

tried them unless im inputting them improperly not much happens pretty much im just trying to get one motor to go in the opposite direction and I cant seem to figure out how to do it

The Servo library has a sweep example sketch.
Put some print statements into it, and slow it down a bit, and you should be able to see what values control your motors.

Sounds, to me, like the servos are "continuous rotation servos" which are not servos at all. They are gear motors whose speed and direction is controlled by servo pulses. 90 degrees being stop, 0 degrees = full speed reverse and 180 degrees full speed forward.

To run 1 motor forward and the other in reverse:

servoLeft.write(speed);
servoRight.write(180 - speed);

What happens if you run the Servo sweep example using one of the servos ?

How are the servos powered? Can you show us a wiring diagram?
Pencil, paper and a camera are good enough if you include proper detail (like pin numbers for the pins you are using)
Read through this handy Image Guide.

a few photos as you can see I have all the wires and board inside the apple box, and ill have my motors glued to the outside with tires on them, and there will be a wheel in the back not connected to anything.

Use this sketch to test your continuous rotation servos 1 at a time on pin 9, might help find the center (standstill) point and the high and low points where maximum speed occurs.
Don't try to power servos from the 5V pin, it cannot supply enough current for servos. Either use a 5 volt plug in "wall wart" supply with a current rating of at least 1 Amp per servo or 4 AA batteries. Be sure to connect the external power supply - (negative) lead to Arduino GND.

/*
 Try this test sketch with the Servo library to see how your
 continous rotation servo responds to different settings, type
 a position (544 to 2400) in the top of serial monitor and hit
 [ENTER], start at 1472 (stop position) and work your way
 toward zero (544) 50 micros at a time, then toward 2400. 
*/
#include <Servo.h>
Servo servo;

void setup() {
  // initialize serial:
  Serial.begin(9600); // set serial monitor baud rate to match
                  // set serial monitor line ending to Newline
  servo.writeMicroseconds(1472);
  servo.attach(9);
  prntIt();
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0)
  {
    // look for the next valid integer in the incoming serial stream:
    int speed = Serial.parseInt();
    // look for the newline. That's the end of your sentence:
    if (Serial.read() == '\n') {}
    servo.writeMicroseconds(speed);
    prntIt();
  }
}
void prntIt()
{
  Serial.print("microseconds =  ");
  Serial.println(servo.readMicroseconds());
}

used the code and moved it to pin 9, servos still running like junk, im starting to think they're busted

From the looks, I have those same servos. I spent a lot of time thinking they're bad and then realized I was not powering them correctly. As already stated in this thread. How are they powered? Also the ones I have that look exactly like that (SG90?) They're not continuous rotation.

jbarth200:
From the looks, I have those same servos. I spent a lot of time thinking they're bad and then realized I was not powering them correctly. As already stated in this thread. How are they powered? Also the ones I have that look exactly like that (SG90?) They're not continuous rotation.

powering them with a dollar store power bank, and I took the notches out of the motors so they can have continuous rotation.

What're the specs on the power supply? Maybe they are bad as they have been modified.

I took the notches out of the motors so they can have continuous rotation.

Did you also remove the internal pot ?