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(10speed);
} 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;
}