Inverting direction of Easydriver problems

Hi, I'm trying to make a stepper motor rotate clockwise or counter clock wise when a button is held.
I'm not using a library because I thought not using one would simplify the code.

For testing purposes I've tried reworking some code from one of the examples that is provided on the website of EasyDriver (the ic I'm using), however I'm unable to switch the direction pin. Can anyone give me some pointers on how to proceed?

Thank you taking the time to teach a coding newbie! :slight_smile:

/
#define RPMS                204.0
#define STEP_PIN                9
#define DIRECTION_PIN           8
#define GO_PIN                  3
#define GO_PIN2                 2

#define STEPS_PER_REV         200
#define MICROSTEPS_PER_STEP     8
#define MICROSECONDS_PER_MICROSTEP   (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 60))

uint32_t LastStepTime = 0;
uint32_t CurrentTime = 0;

void setup() {                
  pinMode(STEP_PIN, OUTPUT);     
  pinMode(DIRECTION_PIN, OUTPUT);
  digitalWrite(STEP_PIN, LOW);
  digitalWrite(DIRECTION_PIN, LOW);
  pinMode(GO_PIN,INPUT);
  pinMode(GO_PIN2,INPUT);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(GO_PIN) == LOW)
  {
    CurrentTime = micros();
    if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
    {
      LastStepTime = CurrentTime;
      digitalWrite(STEP_PIN, HIGH);
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
      digitalWrite(STEP_PIN, LOW); 
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
   //   Serial.println(LastStepTime);
      
    }
  if (digitalRead(GO_PIN2) == LOW)
  {
    CurrentTime = micros();
    if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
    {
      LastStepTime = CurrentTime;
      digitalWrite(STEP_PIN, HIGH);
      digitalWrite(DIRECTION_PIN, LOW);
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
      digitalWrite(STEP_PIN, LOW); 
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
   //   Serial.println(LastStepTime);
}
  }
  }
}

How are the switches wired? Do you have external pullup or plldown resistors on the switch inputs?

Have you tried writing DIRECTION_PIN HIGH?

groundFungus:
How are the switches wired? Do you have external pullup or plldown resistors on the switch inputs?

both buttons are wired with a resistor in between, 1 direction works, but switching the direction doesn't.

JCA79B:
Have you tried writing DIRECTION_PIN HIGH?

yes I did, but it doesn't work. Could that have something to do with the fact that the direction is not inserted in here?

void loop() {
  if (digitalRead(GO_PIN) == LOW)
  {
    CurrentTime = micros();
    if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
    {
      LastStepTime = CurrentTime;
      digitalWrite(STEP_PIN, HIGH);
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
      digitalWrite(STEP_PIN, LOW); 
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
   //   Serial.println(LastStepTime);

It seems pinging the direction does work after all, but only after declaring 'direction' for both code blocks.

Here's the working code!

/* This example assumes a step/direction driver with Step on pin 9, Direction on pin 8
 * And an input switch on pin 3. The switch is a switch to ground, with pin 3 pulled
 * high with a pullup resistor. When the switch is turned on (closed, i.e. goes low)
 * the the sepper motor steps at the rate specified (104 RPM in this code, with
 * 1/8th microstepping of a 200 steps/rev motor)
 */
 
#define RPMS                250.0
#define STEP_PIN                9
#define DIRECTION_PIN           8
#define CW_PIN                  3
#define CCW_PIN                 2

#define STEPS_PER_REV         200
#define MICROSTEPS_PER_STEP     8
#define MICROSECONDS_PER_MICROSTEP   (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 40))

uint32_t LastStepTime = 0;
uint32_t CurrentTime = 0;

void setup() {  
//  Serial.begin(9600);              
  pinMode(STEP_PIN, OUTPUT);     
  pinMode(DIRECTION_PIN, OUTPUT);
  digitalWrite(STEP_PIN, LOW);
  digitalWrite(DIRECTION_PIN, LOW);
  pinMode(CW_PIN,INPUT);    // button 1
  pinMode(CCW_PIN, INPUT);  // button 2
}

void loop() {
  if (digitalRead(CW_PIN) == LOW)
  {
    CurrentTime = micros();
    if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
    {
      LastStepTime = CurrentTime;
      digitalWrite(STEP_PIN, HIGH);
      digitalWrite(DIRECTION_PIN, LOW);
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
     // Serial.println(CurrentTime);
      digitalWrite(STEP_PIN, LOW); 
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
     // Serial.println(CurrentTime);
    }
  }

  if (digitalRead(CCW_PIN) == LOW)
  {
    CurrentTime = micros();
    if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
    {
      LastStepTime = CurrentTime;
      digitalWrite(STEP_PIN, LOW);
      digitalWrite(DIRECTION_PIN, HIGH);
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
     // Serial.println(CurrentTime);
      digitalWrite(STEP_PIN, HIGH); 
      delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
    }
  }
}

There's still a problem though, how come when I write the Serial.println(CurrentTime);
to the serial monitor the rpms becomes really low?

Also, the code is put together in a way that the CurrentTime keeps on counting up, is there an easy way to make it countdown?

If you increase the baud rate, Serial.print will take less time. 115200 baud will take 1/10 the time and you can go even faster. Remember to set the baud rate in serial monitor.

You cannot make micros() (currentTime = micros()) run backwards.

I did what you said and up'd the baud rate to the fastest setting, to 2 million baud. I also managed to modify the code and I now able the program is able to set a home position (by the press of a microswitch) which cannot be surpassed and while holding button 1 the stepper rotates clockwise and if you hold button 2 the stepper rotates counterclockwise (while the integer 'steps' ramps up or down) :slight_smile:

However, there's still something strange happening with the rpm of the motor. The code below makes the stepper rotate 30k steps after homing but the RPM of the motor does not remain constant.
There's a deceleration happening when I move from 0 to 30k, this happens around 2k and 10k steps when the stepper travels in clockwise or counter clockwise direction. Can anyone tell me why?

/* This example assumes a step/direction driver with Step on pin 9, Direction on pin 8
 * And an input switch on pin 3. The switch is a switch to ground, with pin 3 pulled
 * high with a pullup resistor. When the switch is turned on (closed, i.e. goes low)
 * the the sepper motor steps at the rate specified (104 RPM in this code, with
 * 1/8th microstepping of a 200 steps/rev motor)
 */
 
#define RPMS                250.0
#define STEP_PIN                9
#define DIRECTION_PIN           8
#define CW_PIN                  3
#define CCW_PIN                 2
#define home_switch            10

#define STEPS_PER_REV         200
#define MICROSTEPS_PER_STEP     8
#define MICROSECONDS_PER_MICROSTEP   (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 60))

uint32_t LastStepTime = 0;
uint32_t CurrentTime = 0;
int steps; //toegevoegd als afstandsmeter

void setup() {  
  Serial.begin(2000000);              
  pinMode(STEP_PIN, OUTPUT);     
  pinMode(DIRECTION_PIN, OUTPUT);
  digitalWrite(STEP_PIN, LOW);
  digitalWrite(DIRECTION_PIN, LOW);
  pinMode(home_switch, INPUT_PULLUP);
  pinMode(CW_PIN,INPUT);    // button 1
  pinMode(CCW_PIN, INPUT);  // button 2
  
 /////// Start Homing procedure of Stepper Motor at startup /////////
  
  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(DIRECTION_PIN, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(STEP_PIN, HIGH);
    delay(5);                       // Delay to slow down speed of Stepper
    digitalWrite(STEP_PIN, LOW);
    delay(5);   
}
  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(DIRECTION_PIN, LOW); 
    digitalWrite(STEP_PIN, HIGH);
    delay(10);                       // More delay to slow even more while moving away from switch
    digitalWrite(STEP_PIN, LOW);
    delay(10);
  }
  steps=0;  // Reset position variable to zero
  Serial.println(steps);
  
} ///// End of homing procedure which is executed before pressing buttons for control.. /////

void loop() {
  
byte cwVal = digitalRead(CW_PIN);
byte ccwVal = digitalRead(CCW_PIN);

 if( cwVal == LOW) {

    if (steps > 0) {  //  To make sure the Stepper doesn't go beyond the Home Position
      digitalWrite(DIRECTION_PIN, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(STEP_PIN, HIGH);
      //delay(1);
      delayMicroseconds(10);
      digitalWrite(STEP_PIN, LOW);
      //delay(1);
      delayMicroseconds(10);
      steps--;   // Decrease the number of steps taken
      Serial.println(steps);
    }
  }

   if (ccwVal == LOW){
      if (steps < 30000) {      // Maximum steps the stepper can move away from the Home Position
        digitalWrite(DIRECTION_PIN, LOW);
        digitalWrite(STEP_PIN, HIGH);
       // delay(1);
         delayMicroseconds(10);
         digitalWrite(STEP_PIN, LOW);
        //delay(1);
         delayMicroseconds(10);
        steps++;  // Increase the number of steps taken
        Serial.println(steps);
      }
   } 
}

Here's the wiring by the way.

Aansluitschema_stepper_2buttons_bb.pdf (838 KB)

Hi,
Ops image.

Tom... :slight_smile: