Currently I am working on a bird-like robot that will mimic simple bird motion (goal is just to mimic not fly...yet). Anyways i have ordered three servos one for the left wing one for the right and one for the rear(all of which are 180 degree servos). If you see my attached pictures of the skeleton i formed you can easily tell where the servos will be placed. I am fairly new to the coding side of this business and was hoping for a little help. As you can see in my code below i hoped to sweep servos as fast as they can go instead of up by +=1 degree at a time. To do this i wrote for example:
for(pos = 20; pos < 140; pos +=120) //go from 20 degrees to 140 by adding the full 120 degrees it needed to travel
{
wing1.write(pos);
wing2.write(pos);
delay(500);
}
My question is will this work or is += the only option. My birds wings should rotate to a peak 20 degrees and low of 140 degrees totaling at a 140 degree full flap centered within the 180 degree servo space. This leaves an extra 20 degrees of room for the servo so it should be enough and it makes no sense to have a birds wings flap from 0 to 180 straight up and down.
Take a look at the attached pics for a visual representation of what im trying to achieve it makes waaaay more sense.
Thanks for any help!
code here:
#include <Servo.h>
//Declare servo objects; left wing 1, right wing 2, rear wing r.
Servo wing1;
Servo wing2;
Servo rwing;
//Attach servos to appropriate pins.
void setup()
{
wing1.attach(9);
wing2.attach(10);
rwing.attach(11);
}
//Begin loop to list functions. Single count is to make sure it stops after one cycle.
void loop()
{
int s;
int f = 1;
for(s = 0; s < f; s+=1)
{
centerServo();
delay(30000); //Delay 30 seconds to allow me to get into position.
flapTen();
leftTurn();
}
}
//Centers servos before flight.
void centerServo()
{
int cpos;
for(cpos = 0; cpos < 90; cpos +=1)
{
wing1.write(cpos);
wing2.write(cpos);
rwing.write(cpos);
delay (1000);
}
}
//Flaps wings ten times and then centers them for glide.
void flapTen()
{
int i;
int n = 10;
int pos;
for(pos = 90; pos > 20; pos -=70)
{
wing1.write(pos);
wing2.write(pos);
delay(500);
}
for(i = 0; i < n; i+=1)
{
for(pos = 20; pos < 140; pos +=120) //160 equals peak wing depth. 140 equals full wing span.
{
wing1.write(pos);
wing2.write(pos);
delay(500);
}
for(pos = 140; pos > 20; pos -=120)
{
wing1.write(pos);
wing2.write(pos);
delay(500);
}
for(pos = 20; pos < 90; pos +=65)
{
wing1.write(pos);
wing2.write(pos);
delay(500);
}
}
}
//Turns bird left in mid-flight.
void leftTurn()
{
int lpos;
int rpos;
for(lpos = 90; lpos > 45; lpos-=45)
{
rwing.write(lpos);
delay(300);
}
for(rpos = 85; rpos > 45; rpos-=40)
{
wing2.write(rpos);
delay(500);
}
for(lpos = 45; lpos < 90; lpos+=45)
{
rwing.write(rpos);
delay(500);
}
for(rpos = 45; rpos > 85; rpos+=40)
{
wing2.write(rpos);
delay(500);
}
}
or(pos = 20; pos < 140; pos +=120) //go from 20 degrees to 140 by adding the full 120 degrees it needed to travel
{
wing1.write(pos);
wing2.write(pos);
delay(500);
}
"go from 20 degrees to 140 ".
No, sorry, it doesn't.
20, 40, 60, 80, 100, 120
If you want the servo to move from where it is to another position as fast as possible, just tell it to go there. No for loop needed. No delay(), either.
Anyways if this sets it directly to that position the i can just flip the numbers and re-write it to the servos that would create the flap motion i am looking for. 20-140 140-20 and so on.
I am unsure how you are mounting the l/r servos , if back to back or side by side, you will need the reverse direction for one of them, unless i'm mistaken?
If you want to create a flapping motion then you will need to control the flapping frequency. You can tell the servos to move as fast as you want, but if you're commanding them faster than they can physically move then they'll just sit there twitching.
Given your project I suspect that you don't really want 'as fast as possible' and do actually want to sweep the servos at a controlled speed and frequency, so writing incremental positions to the servo as you were doing would be the way to achieve that. If the servos are moving too slowly then you can reduce the delay between positions and increase the movement between positions to get the speed as high as you want. Note that you'll still be limited by how fast the servo can physically move.
If you don't want smooth controlled movements and really just want the servo to jump between two positions as fast as it's capable, you can do that by writing one end positin to the servo, waiting for long enough for the servo to move to that position, then writing the other end position to the servo. I suppose you could think of these as an extreme example of the previous approach, where you only use a single step per wing movement.
Hey guys so here is my updated code after realizing one wing needed to have flipped angles because it will be the other way. I have yet to add delays after telling my two wing servos or the wing and rear to do something because i need the actual servos hooked up to decide how long they need before the next command( should arrive tomorrow!). With this code will they move as fast as possible if at a constant voltage? also i plan on powering my rear servo (a small one) with the Arduino's 5v output but for he other two i want to split a 9V to power them separately, whats the best way to do so? Sorry for the electrical question on a programing forum.
#include <Servo.h>
//Declare servo objects; left wing 1, right wing 2, rear wing r.
Servo wing1;
Servo wing2;
Servo rwing;
//Attach servos to appropriate pins.
void setup()
{
wing1.attach(9);
wing2.attach(10);
rwing.attach(11);
}
//Begin loop to list functions. Single count is to make sure it stops after one cycle.
void loop()
{
int s;
int f = 1;
for(s = 0; s < f; s+=1)
{
centerServo();
delay(30000); //Delay 30 seconds to allow me to get into position.
flapTen();
leftTurn();
}
}
//Centers servos before flight.
void centerServo()
{
int cpos;
for(cpos = 0; cpos < 90; cpos +=1) //Centers all servos at 90
{
wing1.write(cpos);
wing2.write(cpos);
rwing.write(cpos);
delay (1000);
}
}
//Flaps wings ten times and then centers them for glide.
void flapTen()
{
int i;
int n = 11;
int pos;
int pos2;
for(pos = 90; pos = 20;) //Set wings to 20
{
wing1.write(pos);
}
for(pos2 = 90; pos2 = 140;) //Set wings to 20 (aka 140 flipped)
{
wing2.write(pos2);
}
for(i = 0; i < n; i+=1) //Repeat ten times then stop
{
for(pos = 20; pos = 140;) //Flap left wing down
{
wing1.write(pos);
}
for(pos2 = 140; pos2 = 20;) //Flap right wing down
{
wing2.write(pos2);
}
for(pos = 140; pos = 20;) //Flap left wing up
{
wing1.write(pos);
}
for(pos2 = 20; pos2 = 140;) //Flap right wing up
{
wing2.write(pos2);
}
}
for(pos = 20; pos = 85;) //Set left wing to 85 for glide
{
wing1.write(pos);
}
for(pos2 = 140; pos2 = 95;) //Set right wing to 95 (aka 85 flipped) for glide
{
wing2.write(pos2);
}
}
//Turns bird left in mid-flight.
void leftTurn()
{
int wpos;
int rpos;
for(wpos = 95; wpos = 135;) //Raise right wing to 135
{
wing2.write(wpos);
}
for(rpos = 90; rpos = 45;) //Rotate rear wing to 45 degree angle
{
rwing.write(rpos);
}
delay(4000);
for(wpos = 135; wpos = 95;) //Set right wing back to 95 (aka 85 flipped) for glide
{
wing2.write(wpos);
}
for(rpos = 45; rpos = 90;) //Set rear wing back to 90 degrees for glide
{
rwing.write(rpos);
}
}
"loop ()" is a function with the return type "void".
Yes, you have to have a "loop()" function (it doesn't have to contain anything, but it has to be there).
The "loop()" function is not the same as a "for" loop, which can be contained in any function, but doesn't have to be there at all.
You do not have to have a "for" loop (but it makes a lot of things easier).
You really should read up on the syntax of a for loop. There is no increment (or decrement) in this statement. It won't compile, let alone run.
You've been told several times that you don't need a for loop to set a wing position. If you want the wing somewhere, just tell it to go there.
Seeing a whole series of movement commands that all basically do the same thing is almost physically painful. Functions were invented so we would not have to write the same code over and over.
Make yourself a function called flap. In that function, do something like this