Pages: [1]   Go Down
Author Topic: Help with AccelStepper  (Read 1246 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've written a sketch using the Adafruit motor shield to take a picture, move a mirror at constant stepper speed 296 steps, take a 2nd picture, and return. I'd like to have the mirror accelerate and deaccelerate instead of running at constant speed so I can get it in position more quickly.  I've read and tried all of the AccelStepper examples but I can't figure out how to just do a single accel/deaccel movement followed by other code followed by another movement.

Also I have a bipolar motor and I think the AccelStepper code defaults to unipolar and I can't figure out I tell it my motor is bipolar??
Here is my code and I would like to just replace the current motor movement with  AccelStepper code.  Are there examples I can copy?  Where would I find them?

#include <AFMotor.h>

AF_Stepper motor(400, 2);//400 steps per rev, port 2 M3-M4
byte camtrig=13;//set output 13 to trigger camera
int prepulse=125;
int campulse=55;
int picdelay=215;
int flipdelay=50;//delay pic1 to start flip
float rate=1;//multiplier for speed of machine
byte mirrorPos=0;//flag for mirror position
byte button=2;//button to trigger cycle
int sensor=1;// flip mirror sensor
int delta=14;// mirror adj to home position

void setup() {
  Serial.begin(9600);// set up Serial library at 9600 bps
  Serial.flush();//clear serial line
  motor.release();//motor off
  pinMode(button,INPUT);
  digitalWrite(button,LOW);
  pinMode(camtrig,OUTPUT); //set pin 13 as output
  motor.setSpeed(20);//motor speed at initialize
}
void loop(){
 
// check if button is pressed 
  byte state2 = digitalRead(button);
  if (state2 == HIGH ) {
// Call the function pic flip and pic
  PicFlipPic();}}

void PicFlipPic(){
//Take pic1
  digitalWrite(camtrig,HIGH);//pretrigger 1
  delay(prepulse);
  digitalWrite(camtrig,LOW);
  delay(picdelay);
  digitalWrite(camtrig,HIGH);//picture 1
  delay(campulse);
  digitalWrite(camtrig,LOW);
  delay(flipdelay);
//End pic1

  motor.setSpeed(80); //80 rpm
  motor.step(296, BACKWARD, DOUBLE);//#steps, direction, steptype
  mirrorPos=2;

//Take pic2
  digitalWrite(camtrig,HIGH);//pretrigger 2
  delay(prepulse);
  digitalWrite(camtrig,LOW);
  delay(picdelay);
  digitalWrite(camtrig,HIGH);//picture 2
  delay(campulse);
  digitalWrite(camtrig,LOW);
  delay(flipdelay);
//End pic2

  motor.setSpeed(40);  //40 rpm   
  motor.step(296, FORWARD, SINGLE);
  mirrorPos=1;
  delay (1000);

}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi ThomasT,
Im studying this library too. I found your issue on this line:
motor.step(296, FORWARD, SINGLE);
If you want to change the stepper for Bipolar, you must change every  SINGLE to DOUBLE.
Actually AFMotor.h has these settings ( i dont know how to name it , I'm not a coder).

Make a test and see what happens.


cheers
« Last Edit: September 19, 2011, 03:29:06 pm by tomasi » Logged

0
Offline Offline
Shannon Member
****
Karma: 162
Posts: 10496
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please use the code button ('#') to include code in the postings, its much more readable that way...
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: