Hi There,
I'm working on a school project. I have a problem with my servo motors. My servo motor rotates 360 degrees, then turn the opposite. this can be done either manually or in auto. problems appears when I combine the two programs (manual / auto) into one. servo motor can not rotate.
here's my code :
Auto code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
myservo.write(45); // rotate counterclockwise full speed
delay(2000);
myservo.write(90); // stop
delay(100);
myservo.write(135); // rotate clockwise full speed
delay(2000);
myservo.write(90); // stop
delay(100);
}
Manual code :
#include <Servo.h>
Servo myservo;
void setup()
{
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
myservo.attach(9);
}
void loop()
{
byte buta=digitalRead(5);
byte butb=digitalRead(6);
if (buta==HIGH)
{
myservo.write(135);
}
else if (butb==HIGH)
{
myservo.write(45);
}
else
{
myservo.write(90);
}
}
Combine Auto/Manual :
#include <Servo.h>
Servo myservo;
int datamanual;
int dataauto;
void setup()
{
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
myservo.attach(9);
}
void loop()
{
if (Serial.available()>0)
{
datamanual=Serial.read();
dataauto=Serial.read();
if (dataauto==49)
{
myservo.write(45); // rotate counterclockwise full speed
delay(2000);
myservo.write(90); // stop
delay(100);
myservo.write(135); // rotate clockwise full speed
delay(2000);
myservo.write(90); // stop
delay(100);
}
else if (datamanual=='m')
{
byte buta=digitalRead(5);
byte butb=digitalRead(6);
if (buta==HIGH)
{
myservo.write(135);
}
else if (butb==HIGH)
{
myservo.write(45);
}
else
{
myservo.write(90);
}
}
}
}
any advice would be very helpful. thank you in advance..