I am setting up 3 servos (2 position servos and 1 '360' continuous rotational servo) with Arduino. but I failed to run these independently. It runs one after another. I wish to run it in loop independent to each other. Here is the code which I use to run positional servo and CR (360) servo. default servo positions are 90. *CR 360 servo should be running independently 90-60-120-90 (in loop) *Pos servo should be running one after another (in loop)
To run positional servos (tilt/pan):
//tilt servo
for (pos = 90; pos >= 60; pos -= 1) //90 ~ 60
{
tiltservo.write(pos);
delay(30);
}
for (pos = 60; pos <= 120; pos += 1) //60 ~ 120
{
tiltservo.write(pos);
delay(30);
}
for (pos = 120; pos >= 90; pos -= 1) //120 ~ 90
{
tiltservo.write(pos);
delay(30);
}
//pan servo
for (pos = 90; pos >= 60; pos -= 1) //90 ~ 60
{
panservo.write(pos);
delay(30);
}
for (pos = 60; pos <= 120; pos += 1) //60 ~ 120
{
panservo.write(pos);
delay(30);
}
for (pos = 120; pos >= 90; pos -= 1) //120 ~ 90
{
panservo.write(pos);
delay(30);
}
To run CR 360 servo:
legservo.writeMicroseconds(1600); //CW
delay(5000); //run for 5s
legservo.writeMicroseconds(1300); //CCW
delay(5000); //run for 5s
Tried this to independently run CR servos, but how to put some wait (3s-4s) in between FW and RW.
if (millisNow - lastMillis >= 10000)
{
lastMillis = millisNow;
if (crservoState == 0)
{
legservo1.writeMicroseconds(2150); //FW
crservoState = 1;
}
else
{
legservo1.writeMicroseconds(850); //RW
crservoState = 0;
}
}
Trial code after suggestions:
uint32_t milliSNow = millis();
if (milliSNow - lastTimeServoRotate >= 1000)
{
if (crservoState == 0)
{
if (milliSNow - lastTimeServoRotate >= 10000)
{
legservo1.writeMicroseconds(2150); //FW run
crservoState = 2;
lastTimeServoRotate = milliSNow;
}
}
if (crservoState == 1)
{
if (milliSNow - lastTimeServoRotate >= 10000)
{
legservo1.writeMicroseconds(850); //RW run
crservoState = 3;
lastTimeServoRotate = milliSNow;
}
}
if (crservoState == 2)
{
if (milliSNow - lastTimeServoRotate >= 3000)
{
legservo1.writeMicroseconds(1500); //FW STOP
crservoState = 1;
lastTimeServoRotate = milliSNow;
}
}
if (crservoState == 3)
{
if (milliSNow - lastTimeServoRotate >= 3000)
{
legservo1.writeMicroseconds(1500); //RW STOP
crservoState = 0;
lastTimeServoRotate = milliSNow;
}
}
}
lastMillis
variable with the code handling the positional servos?