Go Down

Topic: Help with AccelStepper (Read 1 time) previous topic - next topic

ThomasT

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);

}

tomasi

#1
Sep 19, 2011, 10:18 pm Last Edit: Sep 19, 2011, 10:29 pm by tomasi Reason: 1
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

MarkT

Please use the code button ('#') to include code in the postings, its much more readable that way...
[ I won't respond to messages, use the forum please ]

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy