Go Down

Topic: [SOLVED] Help with code for DRV8825 driver (Read 4780 times) previous topic - next topic

thailoz

Oct 14, 2015, 12:20 pm Last Edit: Oct 20, 2015, 03:18 pm by thailoz
Hi i just got a DRV8825 driver and a 23km-C051-07V stepper motor wired as per pic but with an Uno and on pins 3 and4,
and have got it working on the following sketches;
Code: [Select]

int dirPin = 4;
int stepperPin = 3;
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
}
void step(boolean dir,int steps){
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++){
  digitalWrite(stepperPin, HIGH);
  delayMicroseconds(800);
  digitalWrite(stepperPin, LOW);
  delayMicroseconds(800);
}
}
void loop(){
step(true,1600);
delay(500);
step(false,1600*5);
delay(500);
}




and :


*
A simple test code for running a bipolar stepper motor with Pololu A4988 carrier.
*/

const int stepPin = 3;    
const int dirPin =  2;    
// The number of steps in one full motor rotation
const int stepsInFullRound = 200;

// Set pins
void setup() {
 pinMode(stepPin, OUTPUT);      
 pinMode(dirPin, OUTPUT);
 digitalWrite(stepPin, LOW);
 digitalWrite(dirPin, LOW);  
}

// Runs the motor according to a chosen direction, speed (rounds per seconds) and the number of steps
void run(boolean runForward, double speedRPS, int stepCount) {
 digitalWrite(dirPin, runForward);
 for (int i = 0; i < stepCount; i++) {
   digitalWrite(stepPin, HIGH);
   holdHalfCylce(speedRPS);
   
   holdHalfCylce(speedRPS);
 }
}

// A custom delay function used in the run()-method
void holdHalfCylce(double speedRPS) {
 long holdTime_us = (long)(1.0 / (double) stepsInFullRound / speedRPS / 2.0 * 1E6);
 int overflowCount = holdTime_us / 65535;
 for (int i = 0; i < overflowCount; i++) {
   delayMicroseconds(65535);
 }
 delayMicroseconds((unsigned int) holdTime_us);
}

// Runs the motor once in forward direction and once to the opposite direction.
// Holds the motor still for 1 sec after both movements.
void runBackAndForth(double speedRPS, int rounds) {
 run(true, speedRPS, stepsInFullRound * rounds);
 delay(1000);
 run(false, speedRPS, stepsInFullRound * rounds);
 delay(1000);
}

// Tests various speeds for 10 full rotations
void loop(){
 runBackAndForth(1, 10);
 runBackAndForth(5, 10);
 runBackAndForth(7, 10);
}



But my question is all i want to do is be able to move it x amount of steps forward then x amount of steps backwards to control a left to right shuttle



thanks

Loz



Code: [Select]

Robin2

#1
Oct 14, 2015, 12:51 pm Last Edit: Oct 14, 2015, 12:51 pm by Robin2
please post your program using the code button </>
Code: [Select]
int dirPin = 4;
int stepperPin = 3;
void setup() {
    pinMode(dirPin, OUTPUT);
    pinMode(stepperPin, OUTPUT);
}

void step(boolean dir,int steps){
    digitalWrite(dirPin,dir);
    delay(50);
    for(int i=0;i<steps;i++){
        digitalWrite(stepperPin, HIGH);
        delayMicroseconds(800);
        digitalWrite(stepperPin, LOW);
        delayMicroseconds(800);
    }
}

void loop(){
    step(true,1600);        // <------------------
    delay(500);
    step(false,1600*5);   //<---------------
    delay(500);
}


This is the first example from your Original Post.

The amount the motor moves is specified in the lines I have marked <----. You can change the number 1600 to whatever you need.

Or have I misunderstood your question.

...R
Stepper Motor Basics
Two or three hours spent thinking and reading documentation solves most programming problems.

thailoz

Thank you that helped now how do i get it to go in reverse please

Robin2

Thank you that helped now how do i get it to go in reverse please
The code you posted should make it go first in one direction and then in the other direction. The values of true and false should achieve that. True is the same as 1 or HIGH and false is the same as 0 or LOW. so, in the function  step() the line digitalWrite(dirPin,dir); will set that pin HIGH or LOW for the different directions depending on whether it is sent true or false.

If it does NOT move in both directions with that code please make a pencil drawing of how everything is connected and post a photo of the drawing. A photo of your hardware is not suitable.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

You don't need to wait 50ms after setting the dir pin, a couple of microseconds is plenty, which
is faster than digitalWrite() itself (on the Uno etc).

This is the sort of code I use to drive a stepper chip:
Code: [Select]

void step (bool direction)
{
  digitalWrite (dirpin, direction);
  delayMicroseconds (5) ;
  digitalWrite (steppin, HIGH) ;
  delayMicroseconds (10) ;
  digitalWrite (steppin, LOW) ;
}


5/10us is more than plenty for a directly connected chip,  but is enough for most opto-coupled drivers
to allow the pulse to register.  You can call this upto about 50,000 times a second if you want microstepping
performance note.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

thailoz

Thanks yes it works ok but i am struggling to to work out how i can press a button it moves to a set number of steps and waits till the next input   i.e another button is pushed or a certain amount of time has delayed,

Robin2

It will be much easier to help you if you have a try at getting it to work. Then post your code if you still need help. That way we will be able to focus on the parts you don't understand.

You may find some useful stuff in Planning and Implementing a Program

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

thailoz

#7
Oct 16, 2015, 01:57 am Last Edit: Oct 16, 2015, 01:46 pm by thailoz
Thank you ounce again and Robin thank you for the link very useful reference .I apologize for just posting the question with no code but i was so frustrated (i am sure many of you have been there),

after reading some more posts and doing more thinking i have come up wit this.

Code: [Select]
#include <AccelStepper.h>
// Define a stepper and the pins it will use'
AccelStepper stepper(1);
const int buttonPin = 7;  
int x = 1;
// variables will change:
int buttonState = 0;        
int oldButtonState = LOW;
void setup()
{
  
  pinMode(buttonPin, INPUT);
  stepper.setMaxSpeed(600.0);
  stepper.setAcceleration(500.0);
}
void loop() {
  // put your main code here, to run repeatedly:

  // Get the current state of the button
  
  int newButtonState = digitalRead(buttonPin);

  // Has the button gone high since we last read it?
  
  if (newButtonState == HIGH && oldButtonState == LOW) {  
        if (x == 0){
      // Toggle on
      right();  // Move to Right
       x = 1; }
    else   {
      // Toggle off  
    left(); // Move to Left  
      x = 0;
    }
  }

  // Store the button's state so we can tell if it's changed next time round
  oldButtonState = newButtonState      
}
void right()
{stepper.runToNewPosition(400);
}
void left()
{
  stepper.runToNewPosition(-400);
}





How ever although this works fine . It really is not what i need

i would like to push the button ounce have it latch until for example the motor moves to the right
on pushing another button moves to another set position,


the else statement needs to reset the button ready for the next time it is pushed not run another function.

Robin2

The code you posted seems to have two copies of itself. I presume that is just an error in pasting. But please correct it so that we know exactly what you are doing.

And please remove unnecessary blank lines as they make the code hard to read.

I don't undertand what you mean by
Quote
i would like to push the button ounce have it latch until for example the motor moves to the right
on pushing another button moves to another set position,
or by
Quote
the else statement needs to reset the button ready for the next time it is pushed not run another function.
because you have not described how the program actually behaves.

From looking at the code I think it should do what is described in the first of theses quotes.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

thailoz

Hi thanks again for all your replies.

after a few days scratching i have it working as i intended i made a simple wiring error  ......OOOPS!!

but now i have another problem . I am trying to add one limit switch so on startup it centers the carriage along the rail.

i've searched the forum and used Robin's simple code to test but to no avail just a horrific mess of code which honestly is not worth  posting as it does not work.

I would like to use Accelstepper library.
I would be very grateful if some one can post a working sketch to Do the following:-

1 check limit switch state
2 move towards the limit switch until switch is pressed. set as 0 position
3 move back to the center (for example 2000 steps) set as new ) position
4 exit loop


Thanks

 

   

thailoz

Ok I've solved it with the following function i call in setup;-

Code: [Select]

void stepperHome() { //this routine should run the motor to home

  digitalWrite(sleeppin, HIGH);
  hBval = digitalRead(LimitPin);
  while (hBval == HIGH)
  {
    //backwards slowly till it hits the switch and stops
    stepper.moveTo(-3200);
    stepper.setSpeed(600);
    stepper.run();
    hBval = digitalRead(LimitPin);
  }
  stepper.setCurrentPosition(0); //should set motor position to zero at limit switch
  delay(1000);
  digitalWrite(sleeppin, HIGH);
  stepper.moveTo(-800); // move to center
  stepper.runToPosition();
  stepper.setCurrentPosition(0);//should set motor position to zero and go back to main routine
  delay(1000);
  digitalWrite(sleeppin, LOW);
}


its all working great thanks again for the pointers .

I dont know how to mark as solved

Robin2

I dont know how to mark as solved
Glad to hear you have a solution.

If you modify your Original Post you can add "[SOLVED]" to the title.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up