arduino sketch to pan servo left and right

This is my current sketch. When i try to insert negative degrees in reference to zero degrees it wont turn in the other direction. I need for the servo to start at zero stop at 90 and then turn past zero to -90deg
any suggestions? for a sketch would be greatly appreciated.
Thanks :slight_smile:

//Creating for loop

//Add the servo library. This library is standart library
#include <Servo.h>

//Define our servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;

//servo position in degrees
int servoPos = 0;

void setup()
{
//Define serco signal inputs (Digital PWM 3-5-6-9-11)
servo1.attach(3);
servo2.attach(5);
servo3.attach(6);
servo4.attach(9);
servo5.attach(10);
servo6.attach(11);
}

void loop()
{

//scan from 0 to 180 degrees
for(servoPos = 0; servoPos < 90; servoPos++)
{
servo1.write(servoPos);
servo2.write(servoPos);
delay(100);
}

//now scan back from 180 to 0 degrees
for(servoPos =90; servoPos > 0; servoPos–)
{
servo1.write(servoPos);
servo2.write(servoPos);
delay(100);
}

//scan from 0 to 180 degrees
for(servoPos = 180; servoPos < 90; servoPos++)
{
servo1.write(servoPos);
servo2.write(servoPos);
delay(100);
}

//now scan back from 180 to 0 degrees
for(servoPos =90; servoPos > 0; servoPos–)
{
servo1.write(servoPos);
servo2.write(servoPos);
delay(100);
}
}

any suggestions?

//scan from 0 to 180 degrees
for(servoPos = 0; servoPos < 90; servoPos++)

Don’t write comments that are patently false.

When i try to insert negative degrees in reference to zero degrees

Where in the documentation does it say the Servo methods accept negative values?

Use code tags

Try counting 90 as the centre (which it is) and sweeping from 90 to 180 and back then from 90 to 0 and back, a bit like the original sweep sketch does.

I suppose if you really insist on your original numbering you could always map -90 to 90 to 0 to 180…but I can’t imagine why you would bother.

Steve