I am building a robot for a uni project in which the robot has to drive the length of a track, pick up a ball and take it back to the start. I am making it so the robot only goes forward and back in a straight line, and the claw which picks up the balls will be on an arm that rotates 180 vertically via a servo. I cant get my programming to rotate the arm and work the claw servo at the same time. I want the claw to close when the arm is at 0 and open when the arm is at 180.
My code so far is this:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo2; // a maximum of eight servo objects can be created
int pos = 0,pos2 = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(8); // attaches the servo on pin 8 to the servo object
}
void loop()
{
if(pos == 180)
{
for(pos2 = 10; pos2 < 50; pos2+=1) //claw closes
{
myservo2.write(pos2);
delay(15);
}
}
else if(pos == 0)
{
for(pos2 = 50; pos2>=11; pos2-=1) // claw opens
{
myservo.write(pos2);
delay(15);
}
}
for(pos = 180; pos>=11; pos-=1) // arm goes from 180 degrees to 0 degrees
{
myservo.write(pos);
delay(15);
}
for(pos = 10; pos < 180; pos += 1) // claw goes from 0 degrees to 180 degrees
{
myservo.write(pos);
delay(15);
}
}
I cant get my programming to rotate the arm and work the claw servo at the same time. I want the claw to close when the arm is at 0 and open when the arm is at 180.
Back to the actual question, your delays appear too short to allow the servos to actuall move to any position.
your delays appear too short to allow the servos to actuall move to any position.
15ms to move 1 degree is too short?
On the first pass through the loop, pos is 0, so the else if block should execute. After that the arm is moved from 180 to 10 and back to 180. So, pos is never again 0.
I ran the code with two servos atached. First the arm servo slowly rotates ~160 deg and then back to origional position. Next the claw servo quickly moves ~20 deg and then slowly back to the origional position. Is this what you intend for them to do?
zoomkat:
I ran the code with two servos atached. First the arm servo slowly rotates ~160 deg and then back to original position. Next the claw servo quickly moves ~20 deg and then slowly back to the original position. Is this what you intend for them to do?
No, this is what happens to mine too. i want it to shut at 0, the arm to rotate then it to open and the other end - 180. I have tried moving the if statements in to about 150 in case it wasn't moving the full 180 but got the same result.
Also with the delays being too short, how long would you recommend. and do i need delays in between the rotations or in the for statements that actually rotate the servos?
with arm down foward and claw open, the bot travels to the ball.
claw closes on the ball.
arm rotates 180 deg to the rear.
bot travels back to the start.
claw opens to release the ball.
As to the delays, they can be used to make the servos move slowly, or with the servos moving at max speed, they can allow the servo to complete a move prior to the next action. I'd start simple without making the servos move slowly, and just work on getting the servos to operate in the sequence needed.
with arm down foward and claw open, the bot travels to the ball.
claw closes on the ball.
arm rotates 180 deg to the rear.
bot travels back to the start.
claw opens to release the ball.
As to the delays, they can be used to make the servos move slowly, or with the servos moving at max speed, they can allow the servo to complete a move prior to the next action. I'd start simple without making the servos move slowly, and just work on getting the servos to operate in the sequence needed.
Yea thats exactly what i want it to do.
Whats the best way to get them to work in sequence. with big delays between each command or do you think stick with the if statement?
Whats the best way to get them to work in sequence. with big delays between each command or do you think stick with the if statement?
Below is a simple repeating sequence that might be useful as a start. I reduced the servo range as one of my servos was against its hard stop.
//open the serial monitor to see what is going on
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
Serial.println("servo robo test"); // so I can keep track of what is loaded
}
void loop() {
myservo1.write(160); //set arm servo position
myservo2.write(160); //set claw servo position
Serial.println("going to the ball"); //
delay(3000); //simulate going to the ball
//going to the ball here
myservo2.write(10); //close claw servo
Serial.println("closing the claw"); //
delay(2000); //simulate time to grip ball
myservo1.write(10); //rotate arm servo position
Serial.println("rotating arm to the rear"); //
delay(3000);
Serial.println("going back to the start");
delay(3000);//going to the start here
myservo2.write(160); //open claw servo
Serial.println("opening the claw"); //
delay(5000);
Serial.println("");
Serial.println("lets do it again"); //
delay(2000);
}
Thanks for that. Is it ok to hook the arduino straight up to a 9v battery or do we need a voltage regulator?
also do i plug both servos into the same 5v output?
You should be able to power the arduino for a while from a 9v battery connected to the arduino external power jack. The servos and setepper motors will need to be powered from a seperate power source.