control a stepper motor with rotary encodeer

hi, please I need help with combing two codes, I have little knowledge of programming, I have a code that works with my stepper motor and a different code that works with my rotary encoder. But I need the rotary encoder to control the stepper motor. I am using TMC260 driver, for the rotary encoder.

Here is the driver code:

#include <SPI.h>
#include <TMC26XStepper.h>

//we have a stepper motor with 200 steps per rotation, CS pin 2, dir pin 6, step pin 7 and a current of 800mA
TMC26XStepper tmc26XStepper = TMC26XStepper(200,6,4,5,800);
int curr_step;
int speed = 0;
int speedDirection = 100;
int maxSpeed = 1000;

void setup() {
Serial.begin(9600);
Serial.println("==============================");
Serial.println(“TMC26X Stepper Driver Demo App”);
Serial.println("==============================");
//set this according to you stepper
Serial.println(“Configuring stepper driver”);
//char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement
tmc26XStepper.setSpreadCycleChopper(2,24,8,6,0);
tmc26XStepper.setRandomOffTime(0);

tmc26XStepper.setMicrosteps(128);
tmc26XStepper.setStallGuardThreshold(4,0);
Serial.println(“config finished, starting”);
Serial.println(“started”);
}

void loop() {
if (!tmc26XStepper.isMoving()) {
speed+=speedDirection;
if (speed>maxSpeed) {
speed = maxSpeed;
speedDirection = -speedDirection;
} else if (speed<0) {
speedDirection = -speedDirection;
speed=speedDirection;
}
//setting the speed
Serial.print("setting speed to ");
Serial.println(speed);
tmc26XStepper.setSpeed(speed);
//we want some kind of constant running time - so the length is just a product of speed
Serial.print("Going ");
Serial.print(10speed);
Serial.println(" steps");
tmc26XStepper.step(10
speed);
} else {
//we put out the status every 100 steps
if (tmc26XStepper.getStepsLeft()%200==0) {
Serial.print("Stall Guard: ");
Serial.println(tmc26XStepper.getCurrentStallGuardReading());
}
}
tmc26XStepper.move();
}

Here is the encoder code;
/***************************************************
Two phase quadrature encoder(Incremental)


To determine motor with encode (CW OR CCW)

@author Dong
@version V1.0
@date 2016-5-26
All above must be included in any redistribution

  • **************************************************/
    #define A_PHASE 2
    #define B_PHASE 3
    unsigned int flag_A = 0; //Assign a value to the token bit
    unsigned int flag_B = 0; //Assign a value to the token bit
    /
    * */
    void setup() {
    pinMode(A_PHASE, INPUT);
    pinMode(B_PHASE, INPUT);
    Serial.begin(9600); //Serial Port Baudrate: 9600
    attachInterrupt(digitalPinToInterrupt( A_PHASE), interrupt, RISING); //Interrupt trigger mode: RISING
    }
    void loop() {

Serial.print("CCW: ");
Serial.println(flag_A);
Serial.print("CW: ");
Serial.println(flag_B);
delay(1000);// Direction judgement

}
void interrupt()// Interrupt function
{ char i;
i = digitalRead( B_PHASE);
if (i == 1)
flag_A += 1;
else
flag_B += 1;
}

CS pin 2

define A_PHASE 2

You have a pin conflict there.

Combining codes More combining codes

Give it a try and, if you have trouble, post the combined code and we can help.

i made this, but it doesnt seem to be working, could you please let me know whats wrong with it

/* Rotary encoder with attachInterrupt
Counts pulses from an incremental encoder and put the result in variable counter.
Taking also into account the direction and counts down when the rotor rotates in
the other direction.
This code is used attachInterrupt 0 and 1 which are pins 2 and 3 moust Arduino.
For more information about attachInterrupt see:
http://arduino.cc/en/Reference/AttachInterrupt
created 2014
by Ben-Tommy Eriksen

*/
// Encoder connect to digitalpin 2 and 3 on the Arduino.

#include <SPI.h>
#include <TMC26XStepper.h>
TMC26XStepper tmc26XStepper = TMC26XStepper(200,6,4,5,800);
int curr_step;
int speed = 0;
int speedDirection = 100;
int maxSpeed = 1000;

volatile unsigned int counter = 0; //This variable will increase or decrease depending on the rotation of encoder

void setup() {

Serial.begin (9600);
pinMode(2, INPUT); // set pin to input
pinMode(3, INPUT); // set pin to input
digitalWrite(2, HIGH); // turn on pullup resistors
digitalWrite(3, HIGH); // turn on pullup resistors

//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);

//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);

Serial.println("==============================");
Serial.println(“TMC26X Stepper Driver Demo App”);
Serial.println("==============================");
//set this according to you stepper
Serial.println(“Configuring stepper driver”);
//char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement
tmc26XStepper.setSpreadCycleChopper(2,24,8,6,0);
tmc26XStepper.setRandomOffTime(0);

tmc26XStepper.setMicrosteps(128);
tmc26XStepper.setStallGuardThreshold(4,0);
Serial.println(“config finished, starting”);
Serial.println(“started”);
}

void loop() {
// Send the value of counter
Serial.println (counter);
}

void ai0() {

// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(3)==LOW) {
counter++;
if (counter++){
if (speed>maxSpeed) {
speed = maxSpeed;
speedDirection = -speedDirection;
} else if (speed<0) {
speedDirection = -speedDirection;
speed=speedDirection;
}
//setting the speed
Serial.print("setting speed to ");
Serial.println(speed);
tmc26XStepper.setSpeed(speed);
//we want some kind of constant running time - so the length is just a product of speed
Serial.print("Going ");
Serial.print(10speed);
Serial.println(" steps");
tmc26XStepper.step(10
speed);
}else{
counter–;
}
tmc26XStepper.move();
}

void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(2)==LOW) {
counter–;

}else{
counter++;
}
}

"It doesn't seem to work" conveys no useful information to enable us to help. The code does something. What does the code do? How is that different than what you want.

Interrupt service routines (ISR) should be kept a short as possible. One cannot use functions that depend on interrupts in an ISR (Serial.print for instance).

Here is example 2: I want that whenever the rotary encoder rotates, the stepper motor should move clockwise only in one direction and stops when the rotary encoder is not rotating. (it drives a 300mm actuator linear slider)

/* Arduino Rotary Encoder Tutorial
*

*/
#include <SPI.h>
#include <TMC26XStepper.h>

//we have a stepper motor with 200 steps per rotation, CS pin 2, dir pin 6, step pin 7 and a current of 800mA
TMC26XStepper tmc26XStepper = TMC26XStepper(200,6,4,5,800);
int curr_step;
int speed = 0;
int speedDirection = 100;
int maxSpeed = 1000;

#define outputA 2
#define outputB 3

int counter = 0;
int aState;
int aLastState;

void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);

Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);

Serial.println("==============================");
Serial.println(“TMC26X Stepper Driver Demo App”);
Serial.println("==============================");
//set this according to you stepper
Serial.println(“Configuring stepper driver”);
//char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement
tmc26XStepper.setSpreadCycleChopper(2,24,8,6,0);
tmc26XStepper.setRandomOffTime(0);

tmc26XStepper.setMicrosteps(128);
tmc26XStepper.setStallGuardThreshold(4,0);
Serial.println(“config finished, starting”);
Serial.println(“started”);
}

void loop() {
aState = digitalRead(outputA); // Reads the “current” state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
(!tmc26XStepper.isMoving());
speed+=speedDirection;
if (speed>maxSpeed) {
speed = maxSpeed;
speedDirection = -speedDirection;
} else if (speed<0) {
speedDirection = -speedDirection;
speed=speedDirection;
}

//setting the speed
Serial.print("setting speed to ");
Serial.println(speed);
tmc26XStepper.setSpeed(speed);
//we want some kind of constant running time - so the length is just a product of speed
Serial.print("Going ");
Serial.print(10speed);
Serial.println(" steps");
tmc26XStepper.step(10
speed);
}
} else {
counter --;
//we put out the status every 100 steps
if (tmc26XStepper.getStepsLeft()%200==0) {
Serial.print("Stall Guard: ");
Serial.println(tmc26XStepper.getCurrentStallGuardReading());
}
Serial.print("Position: ");
Serial.println(counter);

aLastState = aState; // Updates the previous state of the outputA with the current state
tmc26XStepper.move();}

}

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please post a link to the documentation for your stepper library.

…R
Stepper Motor Basics
Simple Stepper Code

Okay
Here are the code and the link: https://www.dfrobot.com/wiki/index.php/TMC260_Stepper_Motor_Driver_Shield_SKU:_DRI0035

 /*     Arduino Rotary Encoder Tutorial
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  
 */
 #include <SPI.h>
 #include <TMC26XStepper.h>

 //we have a stepper motor with 200 steps per rotation, CS pin 2, dir pin 6, step pin 7 and a current of 800mA
 TMC26XStepper tmc26XStepper = TMC26XStepper(200,6,4,5,800);
 int curr_step;
 int speed =  0;
 int speedDirection = 100;
 int maxSpeed = 1000;

 #define outputA 2
 #define outputB 3

 int counter = 0; 
 int aState;
 int aLastState;  

 void setup() { 
   pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   
   Serial.begin (9600);
   // Reads the initial state of the outputA
   aLastState = digitalRead(outputA); 

  Serial.println("==============================");
  Serial.println("TMC26X Stepper Driver Demo App");
  Serial.println("==============================");
  //set this according to you stepper
  Serial.println("Configuring stepper driver");
  //char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement
  tmc26XStepper.setSpreadCycleChopper(2,24,8,6,0);
  tmc26XStepper.setRandomOffTime(0);
  
  tmc26XStepper.setMicrosteps(128);
  tmc26XStepper.setStallGuardThreshold(4,0);
  Serial.println("config finished, starting");
  Serial.println("started");
 } 

 void loop() { 
   aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){     
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) { 
       counter ++;
       (!tmc26XStepper.isMoving()); 
         speed+=speedDirection;
          if (speed>maxSpeed) {
            speed = maxSpeed;
            speedDirection = -speedDirection;
          } else if (speed<0) {
            speedDirection = -speedDirection;
            speed=speedDirection;
          }
     
     //setting the speed
    Serial.print("setting speed to ");
    Serial.println(speed);
    tmc26XStepper.setSpeed(speed);
    //we want some kind of constant running time - so the length is just a product of speed
    Serial.print("Going ");
    Serial.print(10*speed);
    Serial.println(" steps");
    tmc26XStepper.step(10*speed);
        }
     } else {
       counter --;
       //we put out the status every 100 steps
      if (tmc26XStepper.getStepsLeft()%200==0) {
      Serial.print("Stall Guard: ");
      Serial.println(tmc26XStepper.getCurrentStallGuardReading());
     }
     Serial.print("Position: ");
     Serial.println(counter);
   
   aLastState = aState; // Updates the previous state of the outputA with the current state
   tmc26XStepper.move();}
   
 }

TMC26XStepper Libraries.zip (18.6 KB)

The section headed "Protocol/Library Explantion" does not seem to be complete. For example I could not see an explanation for move().

I'm afraid I can't make sense of your code without complete documentation.

I'm not even sure exactly what you want to happen. For example should be motor move one step for every pulse from the encoder?

It would also be a BIG help if you use the AutoFormat tool to indent your code consistently - it makes it much easier to see what parts go together.

...R

It would help everybody help you, if you could minimize the code you have trouble with. Focus on what's going wrong, delete as much as possible from your code while keeping the part that makes it go wrong. The moment you delete something that will lead to the code working ok, you have found where the error is. Good chance is you find the bug yourself! If not, post the code here, inside the code tags, together with your circuit.