Go Down

Topic: HOW? RUN STEPPER AND TWO SERVOS AT SAME TIME? (Read 3892 times)previous topic - next topic

SPACEMILK

Oct 12, 2010, 08:55 pm
HELLO,
IF SOMEONE COULD HELP ME OUT. I'VE BEEN STUCK ON THIS PROBLEM FOR A FEW WEEKS.

**MY MAIN GOAL IS TO GET A SKETCH THAT I CAN MODIFY THAT WILL RUN A STEPPER MOTOR AND TWO SERVOS AT THE SAME TIME.

I AM BUILDING A MOTION CONTROL RIG FOR A CAMERA WITH A STEPPER FOR TRACKING. ONE SERVO FOR PAN. ONE SERVO FOR TILT.

I AM RUNNING EVERYTHING ON THE ADAFRUIT MOTOR SHIELD THAT CONNECTS ON TOP OF THE ARDUINO.

I HAVE SUCCESSFULLY GOT THE STEPPER TO RUN ON ITS OWN. WITH THIS CODE.

#include <AFMotor.h>

AF_Stepper motor(200, 2);

void setup() {
Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println("Stepper test!");

motor.setSpeed(50);  // 10 rpm
}

void loop() {
Serial.println("Single coil steps");
motor.step(3000, BACKWARD, SINGLE);
delay(500);
motor.step(3000, FORWARD, SINGLE);
delay(500);

}

AND THE SERVO TO MOVE ON ITS OWN WITH THIS CODE

// Sweep

#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
}

void loop()
{
for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
{                                  // in steps of 1 degree
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
{
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}
}

ALSO, HOW DO YOU GET THE PROGRAM TO RUN ONCE... INSTEAD OF LOOPING OVER AND OVER AGAIN?

ANY HELP WOULD BE GREATLY APPRECIATED!!!

#1
Oct 12, 2010, 09:13 pm
No need to shout, 'eh? Let go of the CAPS LOCK!
There are 3 kinds of people in the world. Those who are good at maths, and those who aren't.

SPACEMILK

#2
Oct 12, 2010, 09:14 pm
Sorry, I have a caps lock problem.

PaulS

#3
Oct 12, 2010, 09:26 pm
Code that is put in setup() runs once. Code that is put in loop() runs over and over. If you want the code to run once, it should (now) be obvious where to put it.

Put the code from setup() in each sketch into a new sketch, in the setup() function.

Put the code from loop() in each sketch into the new sketch, in the loop() function.

SPACEMILK

#4
Oct 12, 2010, 09:28 pm
Thanks Paul. I'm going to try that out. That solves one hurdle.

SPACEMILK

#5
Oct 12, 2010, 09:43 pm
Paul.
I am a bit of a beginner with the script (sketch) writing.

What does this mean?>

Put the code from setup() in each sketch into a new sketch, in the setup() function.

Put the code from loop() in each sketch into the new sketch, in the loop() function.

I tried to put the code from the loop() into the setup(). but it wont run. what do i do with the loop? i deleted it. and... it wont run. all these errors come up. I tried some different variations.

PaulS

#6
Oct 12, 2010, 09:51 pm
If you have this:

Code: [Select]
`void setup(){   Serial.begin(9600);}void loop(){[glow]   Serial.println("Hello!");[/glow]}`
Hello will be printed over and over again.

If you change it to this:
Code: [Select]
`void setup(){   Serial.begin(9600);[glow]   Serial.println("Hello!");[/glow]}void loop(){}`
Hello will print once.

Now, try that with your code.

SPACEMILK

#7
Oct 12, 2010, 09:55 pm
This worked like a charm!
Servo only moved once.
Thank you PaulS

// Sweep

#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
for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
{                                  // in steps of 1 degree
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
{
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}
}

void loop()
{

}

SPACEMILK

#8
Oct 12, 2010, 10:09 pm
I got this code to work. It moves one servo...then moves the stepper.
How do i move them both at the same time?

// Sweep and step

#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

#include <AFMotor.h>

AF_Stepper motor(200, 2);

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
{                                  // in steps of 1 degree
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
{
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}

Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println("Stepper test!");

motor.setSpeed(50);  // 10 rpm

Serial.println("Single coil steps");

motor.step(2000, FORWARD, SINGLE);
delay(500);
}

void loop()
{

}

PaulS

#9
Oct 13, 2010, 11:37 am
You are telling the servo to move in 1 degree increments, from 0 to 179, with a delay after each move starts.

Once the servo is all the way to the end, you tell it to move in 1 degree increments back to 0, with a delay after each move starts.

Then, you initialize the serial port, and write a message.

Then, you tell the stepper to move 2000 steps.

If you want the servo and stepper to move at the same time, you have to break the stepper stepping into smaller chunks, and move some steps each time the servo moves.

Grumpy_Mike

#10
Oct 13, 2010, 12:21 pm
Look at the blink without delay example and copy that technique to break down the movement into small steps that will then appear to be happening at the same time.

SPACEMILK

#11
Oct 13, 2010, 07:19 pm
I will try that Grumpy. Thanks.

Go Up

Please enter a valid email to subscribe