Pages: [1]   Go Down
Author Topic: HOW? RUN STEPPER AND TWO SERVOS AT SAME TIME?  (Read 1788 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!!
Logged

Maitland, Australia
Offline Offline
Jr. Member
**
Karma: 1
Posts: 88
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No need to shout, 'eh? Let go of the CAPS LOCK!  smiley-wink
Logged

There are 3 kinds of people in the world. Those who are good at maths, and those who aren't.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I have a caps lock problem.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul. I'm going to try that out. That solves one hurdle.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you have this:

Code:
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:
void setup()
{
   Serial.begin(9600);
[glow]   Serial.println("Hello!");
[/glow]}

void loop()
{
}
Hello will print once.

Now, try that with your code.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()
{
 
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()
{
 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33328
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will try that Grumpy. Thanks.
Logged

Pages: [1]   Go Up
Jump to: