I've been developing a stepper project on a Due. See my 'slow stepper' thread.
The good folks here solved that issue.
Now, I was hoping to move the program onto a Zero.
Here's the original code:
```cpp
/* Example sketch to control a stepper motor with TB6600 stepper motor driver
and Arduino without a library: continuous rotation.
More info: https://www.makerguides.com */
// Define stepper motor connections:
const int ena = 13; //Enable Pin
const int Switch = 3;
int D = 55; //Delay
// int spd = A0;
//int pd = 500
#define stepPin 11
#define dirPin 12
#define enaPin 13
//#define stepsPerRevolution 1600
void setup() {
pinMode(Switch, INPUT_PULLUP);
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enaPin, OUTPUT);
// Set the spinning direction CW/CCW:
digitalWrite(dirPin, HIGH);
digitalWrite(enaPin, LOW);
}
//Main Program.
void loop() {
digitalWrite(ena, HIGH); // Set enable high
if (digitalRead(Switch) == LOW)
digitalWrite(ena, LOW); // Set enable low
Run();
}
//Run
void Run() {
while (digitalRead(Switch) == LOW) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(D); //324 rpm @ 55 @ 1600
digitalWrite(stepPin, LOW);
delayMicroseconds(D);} //343 rpm @ 50 @ 1600
}
Turns out, the problem there was lack of acceleration.
So, I found this code that adds a pot so it doesn't try to hit full speed all at once:
```cpp
/*
Stepper Motor Test
stepper-test01.ino
Uses MA860H or similar Stepper Driver Unit
Has speed control & reverse switch
DroneBot Workshop 2019
https://dronebotworkshop.com
*/
// Defin pins
int reverseSwitch = 2; // Push button for reverse
int driverPUL = 11; // PUL- pin
int driverDIR = 12; // DIR- pin
int spd = A0; // Potentiometer
// Variables
int pd = 100; // Pulse Delay period
boolean setdir = LOW; // Set Direction
// Interrupt Handler
void revmotor (){
setdir = !setdir;
}
void setup() {
pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
}
void loop() {
pd = map((analogRead(spd)),0,1023,2000,24);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
So, the original code runs exactly the same on the Zero that it did on the Due -- too slow.
And, the code that solved the issue on the Due, doesn't run at all on the Zero.
Thoughts on things to try???