Switch from Due to Zero

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???

analogRead() in general is a very slow function; on the order of 900us if I'm reading the code correctly. (48MHz, 512 prescaler, 10bits, 32clk sample time, run it twice.)
Figure our some other way of doing the acceleration!

The analogRead() works just fine on the Due. Is the Zero really that much slower?

You should write down a description what the final purpose of rotating the steppermotor is.

  • does your stepper-motor rotate a cooling-fan-propeller? (very likely that not)
  • does your stepper-motor simulate a dentist-drill ? ((very likely that not)

So please describe this final purpose and other circumstances.

As soon as the final purpose is well known really good suggestions can be made how to achieve what you want.

Without this project-overview everybody has to do a lot of speculation what might be possible and what not.

Does your stepper-motor really have to use 1/8microstepping?
For what reason do you want 1/8-microstepping?

Why is a DC-Motor or a brushless DC-motor not better suited?
etc. etc. etc. etc.

It looks that way to me. The ADC itself is about faster on the SAM3X processor, but the Zero software seems to be somewhere between "overly conservative" and "broken."

There was this somewhat-related bug: Speed up ADC (especially for SAMD51!) · Issue #51 · adafruit/ArduinoCore-samd · GitHub
And here, for a faster library: Fast analogRead 10/12 bit ADC for the Arduino Zero and Uno

Arduino as a whole doesn't seem to be particularly interested in performance improvements, unless A LOT of people complain. :frowning:
This "issue" has been open since 2018: Arduino Zero ADC Sample Time Too Long · Issue #327 · arduino/ArduinoCore-samd · GitHub

(Part of the problem is that while a lot of people can see ways to speed up the code, there are a lot fewer people who are capable of visualizing OR doing the testing that such a change would merit. :frowning: )

1 Like

Thank you. That was my suspicion. I think I'll stick with the Due. I've used it before and it seems to be a stable piece of kit.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.