Hi All,
First post here, I'm an Arduino beginner and would love to get some helping hand.
My project is a simple telescope mount control (1 axis motor). I found a nice code on the web that does micro-stepping with a similar setup to mine - here is the link:
The code works nicely and i'm using the quarter step portion of the code to step the motor (it provides Full, Half, and several micro-stepping options).
I tried adding some code to control the speed of the motor by 2 push buttons and display the speed on a 4 digit display.
The problem - while my code works perfectly alone, once I comment-in the function to tun the motor, the buttons stops responding..
Here is the full sketch i'm using after cleaning up the portions of stepping I don't use:
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 7
#define DIO 6
int A = 8;
int B = 9;
int C = 10;
int D = 11;
long duty = 50;
int waitMicroSeconds = 279.052328; // Actual calculated is 279052.328;
int pulseCount = 1000;
int pinButtonL = 2;
int pinButtonR = 3;
int stateMotorSpeed = waitMicroSeconds;
int stateButtonL;
int stateButtonR;
long time = 0;
long debounce = 200;
// Create display object of type TM1637Display:
TM1637Display display(CLK, DIO);
void setup() {
pinMode(pinButtonL, INPUT_PULLUP);
pinMode(pinButtonR, INPUT_PULLUP);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
}
void one(){
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
}
void two(){
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
}
void three(){
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
}
void four(){
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
}
void oneB(){
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
}
void twoB(){
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
}
void threeB(){
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
}
void fourB(){
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
}
// main routine to microstep
void doStep(int st){
long dt1 = waitMicroSeconds * duty / 100;
long dt2 = waitMicroSeconds * (100-duty) / 100;
for (int j = 0; j < pulseCount; j++){
switch (st){
case 1: one();break;
case 2: two();break;
case 3: three();break;
case 4: four();break;
case 11: oneB();break;
case 12: twoB();break;
case 13: threeB();break;
case 14: fourB();break;
case 21: one();break;
case 22: two();break;
case 23: three();break;
case 24: four();break;
case 31: oneB();break;
case 32: twoB();break;
case 33: threeB();break;
case 34: fourB();break;
}
delayMicroseconds(dt1);
switch (st){
case 1: one();break;
case 2: two();break;
case 3: three();break;
case 4: four();break;
case 11: oneB();break;
case 12: twoB();break;
case 13: threeB();break;
case 14: fourB();break;
case 21: oneB();break;
case 22: twoB();break;
case 23: threeB();break;
case 24: fourB();break;
case 31: two();break;
case 32: three();break;
case 33: four();break;
case 34: one();break;
}
delayMicroseconds(dt2);
}
}
// disable motor
void motorOff(){
/* Important note:
Turning off the motor will make it go into a 'rest' state.
When using microsteps (or even full steps), this may not be the last active step.
So using this routine may change the position of the motor a bit.
*/
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
}
// microstepping 16 steps :
void do16Steps(int cnt, boolean forwards){
const int list[] = {1,21,11,31,2,22,12,32,3,23,13,33,4,24,14,34};
for (int i = 0; i < cnt; i++){
duty = 50;
if (forwards)
{for (int j = 0; j <= 15; j++){doStep(list[j]);}}
else
{for (int j = 15; j >= 0; j--){doStep(list[j]);}}
}
}
void loop() {
// Set the display brightness (0-7):
display.setBrightness(0);
// uncomment this to disable motor
// motorOff();return;
/* control the speed of the motor with a waitMicroseconds/pulseCount pair
e.g. waitMicroSeconds / pulseCount
500 / 5 --> one step takes 2500 microseconds
50 / 50 --> one step also takes 2500 microseconds
but in second pair the fequency of the wave is 20kHz, not audible..
in 500 / 5 you may hear a high tone in the motor in microsteps
some motors may not respond well on higher frequencies
note: these parameters also control speed in normal stepping (full or half) although there's no pulses in those cases,
setting pulseCount = 1 might be more readable
*/
waitMicroSeconds -= stateMotorSpeed; // Actual calculated is 279052.328
waitMicroSeconds = (waitMicroSeconds < 0)? 0 : waitMicroSeconds;
pulseCount = 1000; // (gear ratio is 3.44:1)
//Button L procedure
stateButtonL = digitalRead(pinButtonL);
if(stateButtonL == LOW && millis() - time > debounce)
{
stateMotorSpeed = stateMotorSpeed-10;
time = millis();
}
//Button R procedure
stateButtonR = digitalRead(pinButtonR);
if(stateButtonR == LOW && millis() - time > debounce)
{
stateMotorSpeed = stateMotorSpeed+10;
time = millis();
}
int motorspeeddisplay = stateMotorSpeed*10;
display.showNumberDec(motorspeeddisplay, false, 4, 0);
/*
two parameters:
1. number of steps
2. forwards = true --> move forwards / forwards = false --> move backwards
note: there's no speed correction for different modes, so do4Steps makes the motor go twice as fast as do8Steps and so on...
use waitMicroseconds and pulseCount to adapt speed when changing modes
*/
// do16Steps(24, true);//Anti-Clockwise
do16Steps(24, false);//Clokwise
// motorOff(); // give it a rest (see note in motorOff about losing steps)
// delay(1000);
}
So, when I comment out this:
do16Steps(24, false);//Clokwise
The speed control is great, but together it doesn't respond. I initially thought its a timing issue as I don't really understand how the waitMicroSeconds + Pulse combo works and if it affects the loop digitalread somehow.
Any help to steer me in the right direction will be highly appreciated.
Thanks!