Hello all, my first post here. I am making a scale model of a vehicle for work. It is a military vehicle and it has two features that need to move when a switch is thrown. So I have two micro servos attached to said features. Lets say these features are little arms. the movement I am trying to replicate is this. One arm is down, and one arm is up. Then one arm goes down to the other arm. Then the other arm goes up. Rinse and repeat. Using the library and the sample sweep program ihave two moving arms that alternate movements. I just need to place some timing in the prgram so one stays until the other gets there before moving. How can I do that. Everythig I have tried seems to give me something that is non-compilable or messes with the movement I already have established. Here is what I wrote.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.attach(10); // attaches the servo on pin 10 to the servo object
}
void loop()
{ myservo.attach(9);
for(pos =0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
myservo.attach(9);
for(pos = 90; pos>=0; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(25);
// waits 25ms for the servo to reach the position
}
myservo.attach(10);
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
myservo.attach(10);
for(pos = 90; pos>=1; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
}
Am I going about this the right way or is there a better way to do this? Thanks
You are controlling two servos, so you should have two servo objects. Also you only need to perform the servo.attach statement once for each servo in the setup function, get rid of the rest. Every servo has a different speed so all you can do is play with your delay times till it works like you want.
Try this, compiled but not tested:
#include <Servo.h>
Servo myservo1; // create servo object to control servo #1
Servo myservo2; // create servo object to control servo #2
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 10 to the servo object
myservo1.write(0); //position to known starting position
myservo2.write(0); // same
delay(500); // give them time to get there
}
void loop()
{
for(pos =0; pos < 90; pos++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
for(pos = 90; pos>=0; pos--) // goes from 90 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the posi
}
for(pos = 0; pos < 90; pos++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
for(pos = 90; pos>=1; pos--) // goes from 90 degrees to 0 degrees
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
}
I tried the code. I am getting the same movement as before. what I really need is to throw the servo one direction, then stop...nothing. Then throw the other direction and stop. The way we have it now changing the value in the delay line for each throw just adjusts the speed. it only pauses momentarily at the end of one throw and for a long time at the end of the other throw. Sorry I'm nto much help here. This is my first project and try at coding from scratch. Any help would be much apprciated.
Sounds like your problem is that you don't want the action to repeat, but because your code is in "loop", as soon as "loop" returns, it gets called again, so the code gets executed again..
Try putting for (;;) ; before the final closing brace of "loop", or put all the code into "setup" and leave "loop" blank.
curlrup:
I tried the code. I am getting the same movement as before. what I really need is to throw the servo one direction, then stop...nothing. Then throw the other direction and stop. The way we have it now changing the value in the delay line for each throw just adjusts the speed. it only pauses momentarily at the end of one throw and for a long time at the end of the other throw. Sorry I'm nto much help here. This is my first project and try at coding from scratch. Any help would be much apprciated.
Thanks
Curly
How long do you want it to stop motion at the end of each travel before starting the next motion?
Here is how to add 2 second delays at the end of each travel command.
#include <Servo.h>
Servo myservo1; // create servo object to control servo #1
Servo myservo2; // create servo object to control servo #2
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 10 to the servo object
myservo1.write(0); //position to known starting position
myservo2.write(0); // same
delay(500); // give them time to get there
}
void loop()
{
for(pos =0; pos < 90; pos++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
delay(2000);
for(pos = 90; pos>=0; pos--) // goes from 90 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the posi
}
delay(2000); //pause for two seconds
for(pos = 0; pos < 90; pos++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
delay(2000); //pause for two seconds
for(pos = 90; pos>=1; pos--) // goes from 90 degrees to 0 degrees
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
delay(2000); //pause for two seconds
After a ton of research. Re-reading my books, and trial and error this is what I came up with which works perfectly.
#include <Servo.h>
#define POS_DOWN 0 // position for Down
#define POS_UP 20 // position for Up
/ // NOTE: POS_DOWN must be less than POS_UP
#define POS_DELAY 25 // delay time for each 'step' up or down
#define UP_TIME 150 // delay time for 'up' position [ UP_TIME * delay(25) ]
#define DOWN_TIME 150 // delay time for 'down' position [ DOWN_TIME * delay(25) ]
Servo myservo1; // create servo object to control servo #1
Servo myservo2; // create servo object to control servo #2
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int dly = 0; // variable to store the up/down delay time
void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 10 to the servo object
myservo1.write(0); //position to known starting position
myservo2.write(0); // same
delay(500); // give them time to get there
}
void loop()
{
/*
for(pos =0; pos < 60; pos++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
for(pos = 60; pos>=0; pos--) // goes from 90 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the posi
}
for(pos = 0; pos < 60; pos++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
for(pos = 60; pos>=1; pos--) // goes from 90 degrees to 0 degrees
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(25); // waits 25ms for the servo to reach the position
}
*/
// State 1: Down-Down
myservo1.write(POS_DOWN); // tell servo to go to position in variable 'pos'
myservo2.write(POS_DOWN); // tell servo to go to position in variable 'pos'
for(dly = 0; dly < DOWN_TIME; dly++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
delay(25); // waits 25ms for the servo to reach the position
}
// State 2: Down-MovingUp
myservo1.write(POS_DOWN); // tell servo to go to position in variable 'pos'
myservo2.write(POS_DOWN); // tell servo to go to position in variable 'pos'
for(pos = POS_DOWN; pos < POS_UP; pos++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
// myservo1.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(POS_DELAY); // waits 25ms for the servo to reach the position
}
// State 3: Down-Up
myservo1.write(POS_DOWN); // tell servo to go to position in variable 'pos'
myservo2.write(POS_UP); // tell servo to go to position in variable 'pos'
for(dly = 0; dly < UP_TIME; dly++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
delay(25); // waits 25ms for the servo to reach the position
}
// State 4: Down-MovingDown
myservo1.write(POS_DOWN); // tell servo to go to position in variable 'pos'
myservo2.write(POS_UP); // tell servo to go to position in variable 'pos'
for(pos = POS_UP; pos > POS_DOWN; pos--) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
// myservo1.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(POS_DELAY); // waits 25ms for the servo to reach the position
}
// State 5: Down-Down
myservo1.write(POS_DOWN); // tell servo to go to position in variable 'pos'
myservo2.write(POS_DOWN); // tell servo to go to position in variable 'pos'
for(dly = 0; dly < DOWN_TIME; dly++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
delay(25); // waits 25ms for the servo to reach the position
}
// State 6: MovingUp-Down
myservo1.write(POS_DOWN); // tell servo to go to position in variable 'pos'
myservo2.write(POS_DOWN); // tell servo to go to position in variable 'pos'
for(pos = POS_DOWN; pos < POS_UP; pos++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
// myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(POS_DELAY); // waits 25ms for the servo to reach the position
}
// State 7: Up-Down
myservo1.write(POS_UP); // tell servo to go to position in variable 'pos'
myservo2.write(POS_DOWN); // tell servo to go to position in variable 'pos'
for(dly = 0; dly < UP_TIME; dly++) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
delay(25); // waits 25ms for the servo to reach the position
}
// State 8: MovingDown-Up
myservo1.write(POS_UP); // tell servo to go to position in variable 'pos'
myservo2.write(POS_DOWN); // tell servo to go to position in variable 'pos'
for(pos = POS_UP; pos > POS_DOWN; pos--) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
// myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(POS_DELAY); // waits 25ms for the servo to reach the position
}
}