I controlling a GWS Pico Naro Servo motor in a sweeping motion from 0 - 180 degrees and also using PWM on Pin 9 to control a DC motor. The problem I have is that when I call servo.attach(), it causes the PWM to not work. If I comment out the attach statement I a correct multimeter reading off the PWM pin but once I include the statement, there is no PWM and I get a reading of 0V.
Here’s my code:
#include <Servo.h>
Servo servo1;
int servoPosition = 0;
boolean scanIncrement = true; //increase position?
byte servoIncrementValue = 6;
byte servoDecrementValue = 6;
void setup()
{
//servo1.attach(14);
//servo1.write(0);
delay(1500);
pinMode(9, OUTPUT);
}
void loop()
{
scan();
analogWrite(9, 200);
}
void scan()
{
scanIncrement ? servoPosition += servoIncrementValue : servoPosition -= servoDecrementValue; //increment or decrement current position
if (servoPosition >= 180)
{
scanIncrement = false;
servoPosition = 180;
}
else if (servoPosition <= 1)
{
scanIncrement = true;
servoPosition = 1;
}
servo1.write(servoPosition);
delay(15);
}
Can anyone please explain why this happens and how I can fix it? thanks