stepper.moveTo(STOP);

933 views
Skip to first unread message

Ralph Hulslander

unread,
Feb 11, 2022, 3:50:13 PM2/11/22
to accelstepper
I am not a programmer. I have a lot of ACCellStepper and other Stepper programs running.
I pretty muchly understand what the programs are doing and I sometimes can successfully change a program.

I have been following this group for a few years, now I have a actual project.

I need to program a stepper on my Z leadscrew on my lathe.
I will be making random moves so I cannot use stepper.moveTo(10000); or other set distance.
I want to do stepper.moveTo(STOP);
STOP being a variable instigated by a switch thrown during a move.

So how would I do a stop during a move.
How would I store the position of the stop to a variable STOP?

Once I have the variable STOP I "should" be able to do a stepper.moveTo(STOP).

I know I will not get a instantaneous stop when I throw the switch so I'll be coming back
with questions on generating a JOG to get exact positioning.

Ralph


 





Jim Larson

unread,
Feb 11, 2022, 5:39:07 PM2/11/22
to accelstepper
Let me see if I understand. You will command your Z leadscrew motor to move in a given direction. Then you will hit a switch to stop it. Does the position get recorded at this point, then the motor actually stops and returns to the position where the switch was hit? Is it OK if the motor runs past the stop point and then returns to that point? Once I understand exactly what behavior you want, I can possibly help more.
In the mean time, you may find this helpful:

And see the example in the 7th step here:
That example shows one way to use an interrupt to stop a motor.

Ralph Hulslander

unread,
Feb 11, 2022, 6:05:17 PM2/11/22
to accels...@googlegroups.com
Thanks Jim, I have you Missing Manual, thank you very much it is very informative.

You never address stopping the stepper.

Yes the position gets recorded when the motor stops.

For now I do not need the motor to return to the point of the switch being thrown.
I just need to record where the motor stops.

I assume there is a counter in AccellStepper.cpp that counts the number of pulses while doing a moveTo.
I need to save that count.

The point I throw the switch will be close to the actual end point I need but does not need to be exact.
That is where an eventual JOG function will come in which will need to record the final position.
I assume the jogTo position is cumulative (added on to the STOP position).

Ralph

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/accelstepper/04e0a039-b7fe-4cde-8d42-549c4067db12n%40googlegroups.com.

Jim Larson

unread,
Feb 11, 2022, 8:32:35 PM2/11/22
to accelstepper
The stop() function is described as the last item in the Setup Functions. Basically, using stop() will not actually stop the motor. It calculates the minimum number of steps required to stop the motor and sets that as the target position. Further calls to the run() or runToPosition() functions will stop the motor. Reading the currentPosition at that point will tell you the location the motor stopped at. Check out the Information Functions section.
Also do look at the sketch discussed in Step 7 of the second reference. I think it may be a lot like what you need.

How will you know where to jog to? If you know the position, it's easy to use moveTo() and run() to get there. You can set a low value of maximum speed and acceleration to move slowly there. Otherwise, how will you know how much to jog?

Ralph Hulslander

unread,
Feb 11, 2022, 9:38:34 PM2/11/22
to accels...@googlegroups.com
Thanks again, re: " How will you know where to jog to?".
Remember I am talking about positioning a tool on a lathe.
I can mark the piece I am turning and jog to the mark either to the left or to the right depending on where the motor stops.
How would I read currentPosition?

Ralph 


Jim Larson

unread,
Feb 11, 2022, 11:57:42 PM2/11/22
to accelstepper
OK. Maybe I understand better now. You will run the motor in one direction until it gets close to the mark on your work piece. You will hit the button and stop() will actually stop the motor. This point may be on either side of your mark. You then want to jog (that is, move one step at a time) until you get the motor (or the tool mounted on it) in exactly the right position. You want to record this position, presumably so you can have the tool move back and forth between the recorded position and some start point. Sound right?

If that's right, I'll suggest a scheme to provide that control.

Ralph Hulslander

unread,
Feb 12, 2022, 9:36:24 AM2/12/22
to accelstepper
Yes!!

I am not sure where " sketch discussed in Step 7 of the second reference " is.


Ralph

Jim Larson

unread,
Feb 12, 2022, 7:02:06 PM2/12/22
to accelstepper
Ralph -

OK! Let me play a bit and see what I can come up with. I'm thinking of three buttons: STOP, JOG RIGHT, JOG LEFT. STOP will be an interrupt; the other two will be in a single step mode section. Maybe a RUN button will be useful also. Assuming you have a known start location and a maximum limit that can't be exceeded, then the RUN button could start the motor moving from start to the limit and wait for the STOP to be pressed. The motor would be stopped and the JOG buttons would become active. When the correct position is reached, the RUN is pressed, the currentLocation is recorded, and the motor starts running back and forth between the start position and the recorded location. STOP could be used to stop the action and allow re-adjustment.

The sketch I'm referring to is discussed here in Step 7.

The sketch itself is UnoAccelStepperForum_1.ino found here:

Ralph Hulslander

unread,
Feb 12, 2022, 9:00:10 PM2/12/22
to accels...@googlegroups.com
That is really good.

Definitely need a RUN (START) button.
The START location is random, I have a 36" arear to turn stock.
I will eventually need a fixed travel limit (maximum). I have a rather heavy NEMA 34 stepper motor on my Z axis.
That motor could do some damage if it crashed into my chuck. I was thinking of using the ENABLE to kill the motor
when it hits the limit switch. I can manually back the carriage off the limit switch.

re: " then the RUN button could start the motor moving from start to the limit and wait for the STOP to be pressed" ?

The first pass:
Button push START (0) Run until STOP switch thrown. (STOP switch SPST (single pole single throw) momentary)
JOG left/right for final POSITION.
POSITION copied to variable STOP.
Return to START (0).
Manually adjust the depth of cut (X).

The second pass:
Button push START (0) Run to STOP
Return to START (0).
Manually adjust the depth of cut (X).
etc. on and on over and over.

Eventually I will add an X axis stepper.
X is on a 10 TPI leadscrew and would only move .001" - .005" for turning.
When facing X could move 12".
Don't need to worry/think about X now I do not even have the motor mounted.





Ralph Hulslander

unread,
Feb 13, 2022, 11:58:50 AM2/13/22
to accelstepper
Thanks for that link! https://hackaday.io/project/183713/instructions I was going to https://hackaday.io/project/183279-accelstepper-the-missing-manual/
which does not have any Instructions links you might want to update  the home page.
I had not seen/read that section.

Ralph

Ralph Hulslander

unread,
Feb 15, 2022, 3:05:08 PM2/15/22
to accelstepper
Ok I am getting dangerous here.
I modified the ConstantSpeed sketch to use the ENA- on the driver.
Surprise surprise this actually works the motor stops as long as the switch is pressed.

Now I am "trying" to go home back to 0:

Is there code tags in this forum?
[code]
// Include the AccelStepper Library
#include <AccelStepper.h>

const int dirPin = 6;
const int stepPin = 7;

int Limit_1 = 11;
int ENA = 13;

AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);           // works for a4988 (Bipolar, constant current, step/direction driver)

void setup()
{  
   myStepper.setMaxSpeed(12495);   // this limits the value of setSpeed(). Raise it if you like. 15000 does not work
   //myStepper.setSpeed(50);           // runSpeed() will run the motor at this speed - set it to whatever you like.
   myStepper.setSpeed(12000);   // [12495 works with METRO 12500 does not work]
    pinMode(Limit_1,INPUT);      // sets the digital pin 13 as input
    pinMode(ENA, OUTPUT);    // sets the digital pin 11 as onput
}

void loop()
{  
  if (digitalRead(Limit_1 = LOW))
  {
    digitalWrite(ENA, HIGH);       //sometimes stalls motor when starting
     
      myStepper.moveTo(0);  // now return to home position does not work
      //myStepper.setCurrentPosition(0);
     
      myStepper.setAcceleration(2000.0);  
      //myStepper.move(-myStepper.currentPosition()); // This should work also
  }
   myStepper.runSpeed();   // This will run the motor forever.
   
   //val = digitalRead(ENA);   // read the input pin
   //digitalWrite(ledPin, val);  // sets the LED to the button's value
}
[/code]

Ralph Hulslander

unread,
Feb 15, 2022, 7:39:15 PM2/15/22
to accelstepper
I tried to a stop:

 digitalWrite(ENA, HIGH);       //sometimes stalls motor when starting
      myStepper.setAcceleration(200.0);  // this makes motor stop much quicker!
      myStepper.stop();
      myStepper.runToPosition();  // brings to a stop!

      myStepper.moveTo(0);  // now return to home position
      myStepper.setAcceleration(500.0);
      //myStepper.moveTo(0);  // now return to home position
      //myStepper.setCurrentPosition(0);
     
      //myStepper.setAcceleration(2000.0);  

      //myStepper.move(-myStepper.currentPosition()); // This should work also
  }
   myStepper.runSpeed();   // This will run the motor forever.

The motor does not run.
It is interesting if I remove all of my code the motor does run.

Ralph

Jim Larson

unread,
Feb 15, 2022, 9:12:37 PM2/15/22
to accelstepper
A couple of things of note.
First, if you are using runSpeed() (as you are), then the motor will run at the speed set by the last call to setSpeed(). If you call move() or moveTo(), the speed will be set to 0. That's why your motor won't try to run home.
Second, if you are at 0 when you call stop(), nothing will happen.

Finally, I'm working on some more example code for you. It'll take me few days more - sorry for the delay.

Keep experimenting!

Ralph Hulslander

unread,
Feb 15, 2022, 10:23:44 PM2/15/22
to accels...@googlegroups.com
Thanks Jim, you have been so helpful. I wanted to try. Thanks for the explanation of why the motor stopped working.

Ralph

Ralph Hulslander

unread,
Feb 18, 2022, 1:51:25 PM2/18/22
to accelstepper
Damm, now my code that did stop the motor no longer works.

I was also not able to figure out how to go HOME (0) when it was working.

Ralph

Jim Larson

unread,
Feb 18, 2022, 8:37:07 PM2/18/22
to accelstepper
Hi Ralph -
You can't imagine how frustrated I am about not having code for you. It's almost working, but I've run into an interesting problem getting the jog function to work right.I think I have a work-around nearly ready and I need to create a test case to show the problem so others can show me my error.
  -jim

Ralph Hulslander

unread,
Feb 18, 2022, 8:48:46 PM2/18/22
to accels...@googlegroups.com
Thanks Jim, I am totally lost with trying to return to Home (0).

Oh and just to complicate things, I'll need to use a pot to set the speed once I have the STOP location.
I will not have a computer so I cannot use hard coded movement except for the first move to set STOP
that movement can be hardcoded.

Ralph

You received this message because you are subscribed to a topic in the Google Groups "accelstepper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/accelstepper/J_KCkMmPGDM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to accelstepper...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/accelstepper/05bf7a7c-48e8-45f7-a8d9-d8be77a9930cn%40googlegroups.com.

Jim Larson

unread,
Feb 19, 2022, 5:50:20 PM2/19/22
to accelstepper
Hi Ralph -
You can see the question I posted to the forum about the problem I'm seeing. Hopefully, someone will have some insight! I think I have a work-around, but there may be a better way.

As to the pot you want to use to set speed, how do you envision it working? What feedback do you expect to get from it? Will you have a display of some sort? I'm expecting that you will set the speed when the motor is stopped, but I'm not sure.
       -jim

Ralph Hulslander

unread,
Feb 20, 2022, 11:11:19 AM2/20/22
to accels...@googlegroups.com
I will have no way of setting speed.

On the first pass the motor can run full speed (12495) until the STOP button is hit, setting the STOP location and returning to 0 (HOME) with a switch throw.

On subsequent passes the speed needs to come from the POT. 

Travelling from HOME (0) to STOP with variable speed from POT.

I will be testing your code, thank you so much.

Ralph

Ralph Hulslander

unread,
Feb 20, 2022, 12:16:27 PM2/20/22
to accelstepper
Jim, I downloaded your code.

The motor does not run.

I see the speed and currentPosition in Serial Monitor but the motor is not running.

I have had problems running other AccelStepper sketches.

Ralph

Ralph Hulslander

unread,
Feb 20, 2022, 2:23:49 PM2/20/22
to accelstepper
Gee, I had a wire loose. The motor runs!!

I changed the maxSpeed and setAcceleration and set the Speed using L: as was suggested.

 //myStepper.setMaxSpeed(400.);
  myStepper.setMaxSpeed(12495L);
  //myStepper.setAcceleration(50.0);
  myStepper.setAcceleration(500L);

I no longer get the slow run.
 done with run
23.39  0
530.55  281
1028.31  1057
1524.58  2324
2019.21  4077
1959.91  6159
1465.05  7854
969.25  9061
471.68  9778
done with run
-23.39  10000
-530.55  9719
-1027.83  8944
-1523.93  7678
-2017.98  5928
-1960.42  3843
-1466.07  2149
-969.76  940
-471.68  222
done with run

Ralph Hulslander

unread,
Feb 20, 2022, 2:32:17 PM2/20/22
to accelstepper
Here is the run with JOG:

done with run
23.39  0
530.55  281
1027.83  1056
1523.93  2322
2017.98  4072
1960.42  6157
1466.41  7850
970.28  9059
472.74  9777
done with run
jog right
jogging
jog right
jogging
jog right
jogging
19.77  10003
-191.05  9975
-688.09  9538
-1185.50  8606
-1680.57  7187
-2174.67  5282
-1806.46  3264
-1311.25  1720
-814.53  664
-317.01  101
done with run

Ralph Hulslander

unread,
Feb 20, 2022, 2:51:03 PM2/20/22
to accelstepper
Hi Jim, re: " Will you have a display of some sort?" 

I had not mentioned a display because I had one in mind but had not "thought" it out 
on just what I wanted to display.

I "think" (always a dangerous sign) that I will want to display SFM (Surface Feet per Minute).

Using a 8 TPI (Thread per Inch) I could calculate the SFM or because it does not have to be absolutely
accurate I was thinking of just using a lookup table.

If I used fast acceleration I could build a table for myStepper.setMaxSpeed at various speeds of Z.
I would also have to have spindle rotation speed (RPM).

So the display gets complicated.

Ralph 

Jim Larson

unread,
Feb 21, 2022, 7:17:35 PM2/21/22
to accelstepper
Attached is the code that should support jogging both ways (add a button on A3) and the interrupt button (Hook to pin D2, tie to ground with a 10K resistor, Other side to 5V when button is pressed.) Don't forget to fix the pin numbers for the stepper and change the increment for jogging. Let me know how it works! Sorry it took so long.
  -jim
UnoAccelStepperForum_4.ino

Ralph Hulslander

unread,
Feb 22, 2022, 5:48:05 PM2/22/22
to accelstepper

Well I am running the newest code.

The Run code runs very fast the motor does not ever turn, that I can perceive.
I tried making the end really long to see if the motor would run.
If I hold the Run switch down for continuous operation the Stop switch does nothing.
Same for the Jog, the motor does not run. In the previous code I was able to lengthen the jog
but I do not see where to do that yet.

-5
stopping
find stop
start: -5
end: 300000
After set -- start: -5
end: 300000
stopping
back to start
start: -5
end: 300000
After set -- start: -5
end: -4
stopping
find stop
start: -5
end: -4
After set -- start: -5
end: -4
stopping

Jim Larson

unread,
Feb 22, 2022, 6:37:54 PM2/22/22
to accelstepper
Ralph - it appears the stop interrupt is happening immediately. Be sure you have the line from D2 tied low with a 10K resistor and that the stop button connects to 5V when pressed. That is, D2 should be low and go high when stop is pressed.

Ralph Hulslander

unread,
Feb 22, 2022, 7:24:53 PM2/22/22
to accelstepper
WOW!! It almost works! Strange I changed the Jog move:
//myStepper.move (-1 );
        myStepper.move (100); for Right and Left jog.

Now the motor runs and the Limit Stop stops!

But the jog keeps the motor running after a couple of jogs. 

I had to hit the reset button to stop the motor.

-5

find stop
start: -5
end: 300000
After set -- start: -5
end: 300000
7.40  -5
60.41  31
110.22  116
159.84  250
209.87  435
259.70  669
309.75  954
359.64  1288
stopping
jog right
2089
jogging
jog right
2189
jogging
jog left
2189
2289
jogging
-5
 
Ralph

Ralph Hulslander

unread,
Feb 22, 2022, 7:35:06 PM2/22/22
to accelstepper
I wish I could edit previous post, like on most other forums.

It is just left jog that keeps the motor running, 

      if ( digitalRead(JogLt) == LOW )
      {
        Serial.println("jog left");
        Serial.println(myStepper.currentPosition());

        //myStepper.move (-1 );
        myStepper.move (100);    
        Serial.println(myStepper.targetPosition());
        myStepper.setSpeed(-100.);
        state = JOG_CMD;
      }
Ralph

Jim Larson

unread,
Feb 22, 2022, 7:59:39 PM2/22/22
to accelstepper
Make that a -100 in the JogLeft command, not 100. You need a negative direction to go left.

Ralph Hulslander

unread,
Feb 22, 2022, 8:27:16 PM2/22/22
to accelstepper
Thanks, DUH!

Now for something really weird:
The Limit Stop works!!
Because of what I had setup my switch is going LOW not HIGH.
My first effort at Fritzing:
Stop.png

But it works!!

Ralph

Ralph Hulslander

unread,
Feb 22, 2022, 8:33:49 PM2/22/22
to accelstepper
Jim, 1 of the reasons I need to have POT control over speed on subsequent passes.

Is because as I am turning (reducing the diameter) of a piece I need to slow the Z axis motion.

I "should" be able copy your POT code from other sketches. Ha

I will also need a Reset for the Arduino if I want to make stepped turnings.

Ralph

Ralph Hulslander

unread,
Feb 22, 2022, 8:43:51 PM2/22/22
to accelstepper
I have the Reset working, there is a Reset pin. Switch the Reset pin to GRN and the Arduino Resets!!

Ralph

Ralph Hulslander

unread,
Feb 24, 2022, 1:38:46 PM2/24/22
to accelstepper
Well I am "trying" to use a Pot to set the speed:

      if ( digitalRead(Run) == LOW)
      {
        //*                                                DOES NOT WORK!!
        // Use Pot to set myStepper.setMaxSpeed(?????);
             // read the sensor value:
               int sensorReading = analogRead(A0);
             // map it to a the maximum speed range
               int motorSpeed = map(sensorReading, 0, 1023, 5, 4000);
               myStepper.setMaxSpeed(motorSpeed);
             // set the motor speed:
             Serial.print(sensorReading);
             // Serial.print(++++);
             Serial.println(motorSpeed);


       //*/
       
        // if searching for the target limit, set it and then run back to START. Otherwise, just run and wait for STOP

Since I added this code the motor runs slower but not at a variable speed.
Also none of the Serial.print are showing up, so it seems this code is being ignored.

Ralph

Ralph Hulslander

unread,
Feb 25, 2022, 4:28:10 PM2/25/22
to accelstepper
Finally figured out where to place the Pot code:

 case RJSTR:
      //Serial.println("running");
      if (printTime >= 1000) {    // happens once per second
        printTime = 0;


        //*                                                DOES NOT WORK!!
        // Use Pot to set myStepper.setMaxSpeed(?????);
             // read the sensor value:
               int sensorReading = analogRead(A0);
             // map it to a the maximum speed range
               int motorSpeed = map(sensorReading, 0, 1023, 5, 4000);
               myStepper.setMaxSpeed(motorSpeed);
             // set the motor speed:
             Serial.print(sensorReading);
//             Serial.print(++++);
             Serial.println(motorSpeed);


       //*/
               
        Serial.print(myStepper.speed());
        Serial.print("  ");
        Serial.println(myStepper.currentPosition());
      }

Wow, now this program is doing exactly what I had pictured when I asked the original question.

I only have one more function to implement:

So far we have used a virtual limit switch, which works great.

Now I need to have an actual limit switch.
I am picturing the limit switch tripping the ENA-  pin on the driver.
Making ENA- positive kills the motor instantly.
Making ENA- positive stops the motor and removes all power to the motor so that the motor is relatively free.
Then I need to do a Stop and return to 0.
Returning to O is really optional I just have to stop the motor.
I can manually hit the Reset button and return the carriage to the Start area
and start over.

The limit switch is to stop the the carriage travel from colliding with the lathe chuck.
I have a rather heavy NEMA34 stepper motor on my leadscrew and could do some really serious damage
if the carriage and chuck collide.

Not being a programmer I do know where to put the code so that this code overrides any other code.
Possible I have to put it in every section.

Ralph 

Jim Larson

unread,
Feb 25, 2022, 8:03:54 PM2/25/22
to accelstepper
Great solution to the Pot adjustment! Obvious when I see it, but I don't think I would have thought of it. Glad you did!

Regarding the limit switch, there are a couple of ways to go. The safest way is to have the limit switch just disable the motor directly. Now, no matter what else happens, the motor will be stopped if it hits the limit. You would have to reset to get things in control again.
The other way works if you really trust the electronics. You could use Interrupt 1 on D3 as an immediate stop. This would make sure you continued to have software control, You can copy the code and concept from the STOP button, and I'll be glad to look it over if you like.

BTW, Sorry for the slow responses. Wife had surgery and we have a kitchen remodel starting Tuesday. It's under control, but I'm kinda slammed.
      -jim

Ralph Hulslander

unread,
Feb 25, 2022, 9:29:08 PM2/25/22
to accels...@googlegroups.com
Oh Jim, I have taken up so much of your time.
I really appreciate all that you have done.
I tried different locations for the Pot code, this location seems to work! I do not know if there might be a better method.

I want to use the ENA- because that will unpower the motor.

I have working code, well it did work last week.

I am "thinking" that it should go in the case RJSTR: like the Pot code.

I will try tomorrow.

Ralph

Ralph Hulslander

unread,
Mar 4, 2022, 2:43:17 PM3/4/22
to accelstepper
Ok, I have working code to respond to a Limit Switch that stops the motor and  disables the coils.

Now, when I back off from the Limit Switch I need to return to (0). That should not be hard.
But to complicate things I want to return to (0) fast.
In fact I need to return to (0) fast every time I return.

I might have the POT set to a low speed when I encounter the Limit Switch.

Actually I do not see anyway using the current script that I would ever hit the Limit Switch BUT.

Some people even scoff at the idea of having limit switches on their lathes.

I am sure more questions will be coming. 

Jim how's your wife doing after the surgery?

And the kitchen remodel?

Ralph


 

Ralph Hulslander

unread,
Mar 5, 2022, 4:09:46 PM3/5/22
to accelstepper
This is strange (oh dear I am repeating myself).

The motor does not run. I can hear some pulses when I push the run button:


It was running last week but when I was working on turning off the ENA pin
I messed with the wiring.

Now I have totally redone the wiring (much cleaner).
The button press pulse say the button is wired correctly.

I can run the motor using the Constant Speed sketch, so the motor is good.

Ralph


Ralph Hulslander

unread,
Mar 5, 2022, 4:28:54 PM3/5/22
to accelstepper
I do not know if this will help.
This is what I am getting in Serial Monitor:

end: 114
After set -- start: 113
end: 114
stopping
find stop
start: 113
end: 114
After set -- start: 113
end: 114
stopping
back to start
start: 113
end: 114
After set -- start: 113
end: 114
stopping

Ralph

Ralph Hulslander

unread,
Mar 5, 2022, 8:11:58 PM3/5/22
to accelstepper
This old code works! The motor runs!

// Include the AccelStepper Library
#include <AccelStepper.h>
#include <elapsedMillis.h>

// Motor Connections (constant current, step/direction bipolar motor driver)

const int dirPin = 6;
const int stepPin = 7;

AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);           // works for a4988 (Bipolar, constant current, step/direction driver)

// State definitions
#define READ_BUTTONS 01
#define RJSTR 02
#define JOG_CMD 03
// State variable
long STOP;
int state;
elapsedMillis printTime;

long int myStart = 0;
long int endLimit = 10000;
bool findLimit = true;
//Button pin assignments
int JogRt = A1;
int Run = A3;
int GoToStop = A4;
int Home =A2;

void setup () {
  Serial.begin(115200);

  state = READ_BUTTONS;   // initial state is to read buttons
  myStepper.setMaxSpeed(400.);
  myStepper.setAcceleration(50.0);
 
  // Buttons
  pinMode(JogRt, INPUT_PULLUP);
  pinMode(Run, INPUT_PULLUP);
  pinMode(GoToStop, INPUT_PULLUP);
  pinMode(Home, INPUT_PULLUP);

 
}

void loop () {
  switch (state) {
    case READ_BUTTONS:
      if ( digitalRead(JogRt) == LOW )
      {
        Serial.println("jog right");
        myStepper.move (100 );    // right is assumed positive
        STOP = myStepper.currentPosition();                       // !! WORKS adds to STOP
        state = JOG_CMD;

      }
      if ( digitalRead(Run) == LOW)
      {
        if (findLimit) {
          myStepper.moveTo(endLimit);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = false;
         
        }
        else
        {
          STOP = myStepper.currentPosition();        // WORKS records stop position but not JOG position.
         
          myStepper.moveTo(myStart);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = true;
        }      
        state = RJSTR;
      }
      delay(200);  // button response delay
      break;
    case JOG_CMD:
      Serial.println("jogging");
      myStepper.setSpeed(100);   // this will be the jogging speed
      while (myStepper.distanceToGo() > 0) { // jog one step before proceeding
        myStepper.runSpeed();
       
        //STOP = myStepper.currentPosition();           // Prints 300
      }
      state = READ_BUTTONS;   // read buttons again.
      break;

    case RJSTR:
      //Serial.println("running");
      if (printTime >= 1000) {    // happens once per second
        printTime = 0;
        Serial.print(myStepper.speed());
        Serial.print("  ");
        Serial.println(myStepper.currentPosition());
        //STOP = myStepper.currentPosition();
        Serial.print(STOP);
        Serial.print("--- ");
      }
      if (!myStepper.run()) {
        state = READ_BUTTONS;
        Serial.println("done with run");
      }
      break;
   }
  }

Ralph Hulslander

unread,
Mar 5, 2022, 8:14:58 PM3/5/22
to accelstepper
I have run this sketch a hundred times and never seen this error:

UnoAccelStepperForum_4:56:22: error: 'rising' was not declared in this scope

   attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2

I downloaded the sketch now I am getting that error trying to compile the original sketch.

Ralph

Ralph Hulslander

unread,
Mar 5, 2022, 8:37:01 PM3/5/22
to accelstepper
I loaded a copy of the sketch and no not get that error:

UnoAccelStepperForum_4:56:22: error: 'rising' was not declared in this scope

   attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2

Jim Larson

unread,
Mar 5, 2022, 8:44:23 PM3/5/22
to accelstepper
Ralph - Looking at the code you  posted a couple of postings above, it looks like you chopped off the interrupt service routine. Add it back in and the error will go away.

 -jim

Ralph Hulslander

unread,
Mar 6, 2022, 12:10:19 PM3/6/22
to accelstepper
I am slowly trying to match the full downloaded code (which does not run) with the modified code I posted above (which) does run. I am compiling and test every little change. 

Once again I am getting:
     error: 'rising' was not declared in this scope

     attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2

                      ^~~~~~

adding the internet service routine fails!

Ralph

Ralph Hulslander

unread,
Mar 6, 2022, 12:15:16 PM3/6/22
to accelstepper
Attached is the full code that is failing with the 'rising' was not declared in this scope error.

I have no idea where this code came from but at least the motor ran.
Z_axis.ino

Ralph Hulslander

unread,
Mar 6, 2022, 3:15:20 PM3/6/22
to accelstepper
Stupid Arduino IDE did not save the sketch I was working on, so the uploaded sketch compiles.
Here is another version that gets the error.
Z_axis.ino

Chris Toop

unread,
Mar 6, 2022, 4:09:10 PM3/6/22
to accelstepper
Hello Ralph, You are missing a 'curly' closing bracket at the end of the void loop, so loop tries to overflow into void rising. You have also not initialised enabFlag to a type (possibly int) and STOP_NOW should that be STOP?

Hope that helps

Chris..

Jim Larson

unread,
Mar 6, 2022, 5:12:23 PM3/6/22
to accelstepper
The variable STOP is necessary -for setting position. The problem is that, in addition to removing the curly (thanks, Chris - right on!), Ralph also removed the STOP_NOW state code. Needs to be added in also!
                 -jim

Ralph Hulslander

unread,
Mar 6, 2022, 7:28:58 PM3/6/22
to accels...@googlegroups.com
Thanks Chris, I added the missing bracket.
I am still getting the same error!

error: 'rising' was not declared in this scope

Ralph

Peter Wright

unread,
Mar 6, 2022, 7:43:15 PM3/6/22
to accels...@googlegroups.com
Is rising supposed to be in Capitals ie. RISING. ?

Sent from my iPad

On 7 Mar 2022, at 11:28 am, Ralph Hulslander <rhuls...@gmail.com> wrote:



Jim Larson

unread,
Mar 6, 2022, 7:58:04 PM3/6/22
to accelstepper
Peter - both rising (as an ISR name) and RISING (as an interrupt condition) are needed and are distinct. The name of the ISR was unfortunate - I should have chosen differently. I think I copied some code and wasn't thinking.

Ralph -
Please post your latest code. I'll try to straighten it out and repost it.
 -jim

Ralph Hulslander

unread,
Mar 6, 2022, 8:45:51 PM3/6/22
to accelstepper
Jim, this was my feeble attempt to modify working code to fix your original code.
Your original code used to work.
Now the Jog functions work but the motor does not Run.
I have attached the version that used to work.
Of course this is now getting a compiling error which it was not doing this morning so I messed it up.
VERSION-4.ino

Chris Toop

unread,
Mar 7, 2022, 3:33:57 AM3/7/22
to accelstepper
Hello Ralph, Please see the attached rework of your code, it now compiles. What I do to find errors from a good piece of code to a bad one is use a program called MELD which compares 2 or 3 files and identifies the differences, I use it a lot.

Chris..

VERSION-4.ino

Ralph Hulslander

unread,
Mar 7, 2022, 3:33:05 PM3/7/22
to accels...@googlegroups.com
Thanks Chris, the file complies!!

But the motor still does not run:


The motor just pulses.

Ralph


Ralph Hulslander

unread,
Mar 7, 2022, 4:47:04 PM3/7/22
to accelstepper
Now I cannot compile the code I compiled 30 minutes ago. 
The IDE is doing really stupid things:

C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:27:11:
error: redefinition of 'const int dirPin'

 const int dirPin = 4;

           ^~~~~~

C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:16:11:
note: 'const int dirPin' previously defined here


 const int dirPin = 6;

           ^~~~~~

C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\UnoAccelStepperForum_4 (2).ino:28:11:
error: redefinition of 'const int stepPin'

 const int stepPin = 5;

           ^~~~~~~

C:\Users\Ralph\Documents\Arduino\Stepper_Jim_Larson\VERSION-4\VERSION-4.ino:17:11:
note: 'const int stepPin' previously defined here


 const int stepPin = 7;

const int dirPin = 4; and const int dirPin = 5; do not exist in the code. The IDE is adding them.


I am not going to have any hair left.

Ralph

Ralph Hulslander

unread,
Mar 7, 2022, 5:13:32 PM3/7/22
to accelstepper
I got the sketch to compile, I copied the code to a new folder with a new name.

So irritating.

The motor does not run, just makes the pulses I posted earlier.

Ralph

Jim Larson

unread,
Mar 7, 2022, 6:34:15 PM3/7/22
to accelstepper
Try reducing your speed to 400 and acceleration to 300. If the motor runs, then gradually increase the speed and acceleration to find the maximum values.

    -jim

Ralph Hulslander

unread,
Mar 7, 2022, 7:51:00 PM3/7/22
to accels...@googlegroups.com
Exact same thing with 400/300
The motor just pulses.

Ralph

Jim Larson

unread,
Mar 7, 2022, 8:22:49 PM3/7/22
to accelstepper
Has the motor actually run with the controller hooked up? I can't quite determine if it has or not.
   -jim

Chris Toop

unread,
Mar 8, 2022, 3:55:05 AM3/8/22
to accelstepper
Hello Ralph, The code works here, although it is a bit jerky. I am running with aTB6600 driver and Teensy 4.0 (like them a lot), also I am compiling with the latest Arduino 1.8.19 and Teensyduino 1.56 (you don't need this because you are not using a teensy, but I thought I would mention it). I would recheck all your wiring, if you are using a breadboard then it may be a connection issue.

Chris..

Chris Toop

unread,
Mar 8, 2022, 5:58:11 AM3/8/22
to accelstepper
Found reason for jerkiness, no potentiometer connected so minimum all the time.

Ralph Hulslander

unread,
Mar 8, 2022, 10:17:29 AM3/8/22
to accels...@googlegroups.com
This sketch runs the motor.

It uses the same:

 myStepper.setMaxSpeed(400.);
 myStepper.setAcceleration(50.0);
  
As the sketch that only pulses the motor. (Version-4)
I was trying to move the full code from Version-4 to this sketch compiling every change when I got the 
compiling error for the Interrupt routine:

attachInterrupt(0, rising, RISING);  // Interrupt 0 is pin D2 

Ralph

Z_axis.ino

Ralph Hulslander

unread,
Mar 8, 2022, 2:49:47 PM3/8/22
to accelstepper
Well I finally corrected the location of the ISR and now it (Z_axis.ino) compiles!

The motor runs!
The jog right works!
The jog left does not work.
The STOP does not work.

I might not have all of the code for jog left and stop in the current sketch.

Ralph

Z_axis.ino

Ralph Hulslander

unread,
Mar 8, 2022, 3:03:25 PM3/8/22
to accelstepper
Once again I am seeing strange things.

This works:
if ( digitalRead(JogRt) == LOW )
      {
        Serial.println("jog right");
        myStepper.move (100 );    // right is assumed positive
        STOP = myStepper.currentPosition();                       // !! WORKS adds to STOP
        state = JOG_CMD;
      }
The motor jogs counter clockwise (to the left).

This does not work:
if ( digitalRead(JogRt) == LOW )
      {
        Serial.println("jog right");
        myStepper.move (-100 );    // right is assumed positive

        STOP = myStepper.currentPosition();                       // !! WORKS adds to STOP
        state = JOG_CMD;
      }
The motor does nothing. I "thought" I'd change the direction of the jog.
Clockwise for right counterclockwise for left.
It does not matter if it works.
Ralph

Ralph Hulslander

unread,
Mar 8, 2022, 4:03:28 PM3/8/22
to accelstepper
Well I found something interesting:
In the Run section

     if ( digitalRead(Run) == LOW)
      {
        if (findLimit) {
          myStepper.moveTo(endLimit);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          myStepper.setAcceleration(5000L);  // response is the same with or without this
          findLimit = false;
          myStart = myStepper.currentPosition();
           if (jogged) {
            state = JOG_RECOVER;
          }

          /*
          else {
            myStepper.moveTo(endLimit);
            enabFlag = 1; // enable the sensor interrupt
            findLimit = false;
            state = RJSTR;
          }

          */
        }
If I comment out the else the motor runs!
If I leave the else uncommented the motor does not run. The motor pulses!
This is in the Z_axis.ino which runs the motor and I am trying to port the rest of the code here.

I removed the else in Version-4.ino but the motor still does not run so it is a different problem.

Ralph

Ralph Hulslander

unread,
Mar 8, 2022, 5:23:17 PM3/8/22
to accelstepper
I am at a complete stand still.
Any further changes now kill the motor.

The original sketch worked perfectly but now no longer works.

Next I will compare the original with Version-4.
Ralph

Ralph Hulslander

unread,
Mar 9, 2022, 1:41:11 PM3/9/22
to accelstepper
Ok back to square one.

I am trying to run Jim's original sketch (UnoAccelStepperForum_4.ino).
The only changes that I know with Version-4.ino was:
Adding the POT code for variable speed.
Replacing the fixUp code with Jim's suggestion.

Both sketches are now doing the same thing, the motor just pulses, except Version -4 does run the right Jog.

You would think this should be a easy fix.

When I first downloaded Jim's sketch it would not run and I did "something".
Now I do not remember what that something was.

It's really a shame the sketch ran perfectly when it did run Jim really did a great job.

Ralph






Ralph Hulslander

unread,
Mar 9, 2022, 2:10:15 PM3/9/22
to accelstepper
I replaced the  digitalRead(Run) == LOW code with run code from a version 2 which works.
Now the motor works. so what is the difference in the run?

      if ( digitalRead(Run) == LOW)
      {
        if (jogged)
        {
          jogged = false;
          fixUp();
        }
        // myStepper.cleanUp();   // experimental function to fix glitch

        if (findLimit) {
          myStepper.moveTo(endLimit);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this
          findLimit = false;
        }
        else
        {
          STOP = myStepper.currentPosition();
          myStepper.moveTo(myStart);
          //myStepper.setAcceleration(50.0);  // response is the same with or without this

Ralph Hulslander

unread,
Mar 9, 2022, 2:46:55 PM3/9/22
to accelstepper
Next version.
I replaced the digitalRead(Run)==LOW code with code from a "Working" version:

     if ( digitalRead(Run) == LOW)
      {
        // if searching for the target limit, set it and then run back to START. Otherwise, just run and wait for STOP
        if (findLimit) {
          Serial.println("find stop");
          Serial.print("start: ");
          Serial.println(myStart);
          Serial.print("end: ");
          Serial.println(endLimit);
         
          myStart = myStepper.currentPosition();
         
          Serial.print("After set -- start: ");
          Serial.println(myStart);
          Serial.print("end: ");
          Serial.println(endLimit);

          if (jogged) {
            state = JOG_RECOVER;
          }
          else {
            myStepper.moveTo(endLimit);
            enabFlag = 1; // enable the sensor interrupt
            findLimit = false;
            state = RJSTR;
          }
        }
        else { // heading back to start
          Serial.println("back to start");
          Serial.print("start: ");
          Serial.println(myStart);
          Serial.print("end: ");
          Serial.println(endLimit);
          endLimit = myStepper.currentPosition();
          Serial.print("After set -- start: ");
          Serial.println(myStart);
          Serial.print("end: ");
          Serial.println(endLimit);

          if (jogged) {
            state = JOG_RECOVER;
          }
          else {
            myStepper.moveTo(myStart);

            enabFlag = 1; // enable the sensor interrupt
            findLimit = true;
            state = RJSTR;
          }
        }
      }

The motor runs. 
The Stop does not work.
The Jog is only pulsing. I have not changed that code.
None of the Serial.print() are printing.

So I guess I am closer.

I think I have posted the latest code, but save does not always work.

Ralph
UnoAccelStepperForum_4.ino

Ralph Hulslander

unread,
Mar 9, 2022, 3:12:23 PM3/9/22
to accelstepper
Unbelievable!!

I ran the sketch, the motor ran.
I changed the Jog parameters. The motor ran.

The motor stopped running!

I undid my changes back to where I had changed the (digitalRead(Run) = LOW)

I redid the  (digitalRead(Run) = LOW).

The motor is dead not even pulsing.

Ralph

Ralph Hulslander

unread,
Mar 9, 2022, 3:47:52 PM3/9/22
to accelstepper
Does this tell anyone what is happening with the motor?

-5
stopping
find stop
start: -5
end: 300000
After set -- start: -5
end: 300000
back to start
start: -5
end: 300000
After set -- start: -5
end: -5

stopping
find stop
start: -5
end: -5

After set -- start: -5
end: -5

back to start
start: -5
end: -5

After set -- start: -5
end: -5
stopping

The motor does not run.

Ralph

Jim Larson

unread,
Mar 9, 2022, 8:08:58 PM3/9/22
to accelstepper
Note that your start and end locations are identical. The motor is not moving because no movement is possible.

Ralph Hulslander

unread,
Mar 9, 2022, 8:20:48 PM3/9/22
to accels...@googlegroups.com
Jim, is that why your original code is not working?

I cannot see where to fix it.

Ralph

Chris Toop

unread,
Mar 10, 2022, 9:43:23 AM3/10/22
to accelstepper
Using UnoAccelStepperForum_4 (original I hope), I have made the following changes:

int Pot = A0; Added in the variable speed change option.

Added the following lines into RJSTR after printTime = 0; where you had it before.
int sensorReading = analogRead(A0); 
int motorSpeed = map(sensorReading, 0, 1023, 5, 4000);
myStepper.setMaxSpeed(motorSpeed); This works fine for me

attachInterrupt(digitalPinToInterrupt(2), rising, FALLING); Interrupt wasn't always working for me so I told it directly what pin number I was using.
pinMode(2, INPUT_PULLUP); added this because, I am not sure on this, I think it may be floating causing odd things to happen.
myStepper.setAcceleration(2000); This is in the STOP_NOW function and just makes the motor 'stop' not slow run down.

Found that if I do a 'Run' first it performs fine until endpoint (30000) then stops, operate 'Run' again and runs in reverse back to startpoint (0) full sweep. If either Jog is used the motor steps to 100 steps but somehow the endpoint is now changed so 'RUN' is now limited to where the Jog appears to have stopped (not checked that yet). Now if you operate 'STOP_NOW' whenever the motor is stepping it stops.

I switched to a UNO temporarily to confirm the above works and it does, unfortunately the teensy 4 is so quick the interrupt was firing too many times and causing me issues. 

Chris..
UnoAccelStepperForum_41.ino

Ralph Hulslander

unread,
Mar 10, 2022, 11:07:35 AM3/10/22
to accels...@googlegroups.com
Thanks Chris, but the motor does not run. Nor does it Jog.

Ralph

Ralph Hulslander

unread,
Mar 10, 2022, 11:16:06 AM3/10/22
to accelstepper
Serial.print shows the program running and doing exactly what it is supposed to do.

0
find stop
start: 0
end: 30000
After set -- start: 0
end: 30000
====111====438====7.40  0
====115====454====61.26  36
====114====450====112.67  122
====115====454====163.57  260
====182====715====213.85  448
====841====3289====264.15  687
====1023====4000====314.20  975
====1023====4000====364.07  1313
====1023====4000====413.98  1701
====1023====4000====463.80  2138
====1023====4000====513.66  2625
====1023====4000====563.46  3161
====1023====4000====613.29  3747
====921====3601====663.16  4382
====701====2742====712.95  5066
====551====2156====762.77  5800
====308====1207====812.48  6582
====89====352====352.00  7414
====0====5====5.00  7765
====0====5====5.00  7770
====0====5====5.00  7775
stopping
back to start
start: 0
end: 30000
After set -- start: 0
end: 7779
====0====5====-7.40  7779
====0====5====-5.00  7774
====0====5====-5.00  7769
====0====5====-5.00  7764
====251====985====-7.40  7759
====381====1492====-61.26  7723
====466====1824====-112.67  7637
====614====2402====-163.57  7499
====737====2883====-213.85  7311
====745====2914====-264.15  7072
====838====3277====-314.20  6784
====871====3406====-364.21  6446
====870====3402====-414.22  6058
====870====3402====-464.02  5621
====870====3402====-513.85  5134
====871====3406====-563.73  4598
====871====3406====-613.52  4012
====871====3406====-583.88  3409
====871====3406====-534.06  2852
====871====3406====-484.28  2345
====870====3402====-434.43  1887
====871====3406====-384.63  1479
====871====3406====-334.72  1120
====871====3406====-284.86  811
====870====3402====-235.04  552
====870====3402====-185.06  342
====870====3402====-135.09  182
====870====3402====-85.14  72
====871====3406====-35.36  12
done with run

You can see where the POT is increasing and slowing down the step.
And the STOP is working. 

Ralph

Ralph Hulslander

unread,
Mar 10, 2022, 11:31:23 AM3/10/22
to accelstepper
Once again DUH! The motor runs!

I changed the pins for step and dir to match Jim's (4 & 5).

The motor runs!!

Jim had made a change:

//fixUp();
      myStepper.setCurrentPosition(myStepper.currentPosition());
     
To get rid of the fixUp().

Ralph

Chris Toop

unread,
Mar 10, 2022, 12:01:02 PM3/10/22
to accelstepper
Yes I was using 6 and 7 for the motor, sorry about that..

Chris..

Ralph Hulslander

unread,
Mar 10, 2022, 12:41:29 PM3/10/22
to accelstepper
Oh there was no way for you to know, I should have checked, DUH.


Now I am "trying" to enable the ENA code to stop the motor with a real Limit Switch:

void loop () {
  if (digitalRead(Limit_1) == LOW) // First 'if', Limit Switch is operated and pin input is grounded or LOW
  {
    digitalWrite(ENA, LOW);
    Serial.print(digitalRead(Limit_1));
    Serial.print("++++");
    Serial.println(digitalRead(ENA));
    //*

    myStepper.stop();
      myStepper.runToPosition();  // brings to a stop!
    myStepper.moveTo(myStart);
        findLimit = true;
    //*/

This actually works, almost.
The motor stops and when the limit switch is relieved the motor returns to Start.

But the motor no longer runs when I engage the Run switch.
The motor is dead.

So how would I reset the motor? To enable it to Run?

Ralph

Ralph Hulslander

unread,
Mar 10, 2022, 1:02:50 PM3/10/22
to accelstepper
Boy some people.

I had not copied the state = RJSTR;

void loop () {
  if (digitalRead(Limit_1) == LOW) // First 'if', Limit Switch is operated and pin input is grounded or LOW
  {
    digitalWrite(ENA, LOW);
    Serial.print(digitalRead(Limit_1));
    Serial.print("++++");
    Serial.println(digitalRead(ENA));
    //*
    myStepper.stop();
      myStepper.runToPosition();  // brings to a stop!
    myStepper.moveTo(myStart);
        findLimit = true;
        state = RJSTR;
    //*/
   
  }
  if (digitalRead(Limit_1) == HIGH) // Second 'if', Limit Switch is operated and pin input is 'PULLED UP' or HIGH.

Now it runs doing exactly what I intended.

Boy this is such a great program it is doing exactly everything I was picturing when I first asked 
how to moveTo(STOP).

Thank you so much Jim and Chris!!

Ralph

Ralph Hulslander

unread,
Mar 11, 2022, 3:37:53 PM3/11/22
to accelstepper
Here we go again.
I have killed the motor.
I compared the sketch with a operating sketch and I see my changes but nothing that I can see that would kill the motor.
Motor Run and Jog no longer work.

Can anyone see my error?

Ralph

UnoAccelStepperForum_5.ino

Ralph Hulslander

unread,
Mar 11, 2022, 3:44:07 PM3/11/22
to accelstepper
58 years ago I barely passed Algebra I and flunked out of Algebra II.
Now I need to use some Algebra and I am lost:

8 TPI (* Threads Per Inch Leadscrew)

200 ppr (Pulses Per Revolution)

200 pulses = 1 revolution = 1/8" travel

For Jog:

myStepper.move (100 ); = 1/2 rev = 1/16" travel      so 0.0625          = 100 pulses
myStepper.move (50  );  = 1/4 rev = 1/32" travel            0.03125        = 50  pulses
                                                                1 mm  travel          0.03937008 =  X   pulses
How would I find X?

Ralph

Ralph Hulslander

unread,
Mar 11, 2022, 4:07:58 PM3/11/22
to accelstepper
Found my problem when I killed the motor.
Unbelievable what the problem was.
I had commented out:
        int sensorReading = analogRead(A0);
        // map it to a the maximum speed range
        int motorSpeed = map(sensorReading, 0, 1023, 5, 4000);
From the Jog routine and put the pin definitions on the top of the sketch with the other assignments.

To fix it I left the pin definitions but removed the assignments.
I put the assignments back in the Jog routine.

tadah

Here is the sketch with all of my changes, which do work.
Ralph
UnoAccelStepperForum_5.ino
Message has been deleted

Ralph Hulslander

unread,
Mar 12, 2022, 12:36:15 PM3/12/22
to accelstepper
Almost FINAL version!

Changes to Chris's UnoAccelStepperForum_41:

Added POT for motor speed Chris had done this and other changes to Jim's UnoAccelStepperForum_4.

setMaxSpeed (4000)
setAcceleration(3000.0);

Remove fixUp()
myStepper.setCurrentPosition(myStepper.currentPosition());

Switched speed and direction for JogLt and JogRt (-100 to 100 vice versa)
Motor now turns correct direction JogRt turns Clockwise JogLt turns Counter Clockwise

Added ENA Limit Switch and automated Return to START with switch deactivation

Changed Run Start direction long int endLimit = -30000;

Now the last change (I hope) is to automate the Run operation after the travel STOP has been set.
On the next push of the Run button I need to run to STOP and return to START.

Everything is working, this is so much fun to see the motor do what I had pictured and beyond.

Now I have to make a box to hold the Arduino and connect to my Driver and motor and see how this actually works. 
On Friday, March 11, 2022 at 4:14:22 PM UTC-5 Ralph Hulslander wrote:
Damm, the Pot is not working and killed the motor.
This is with the latest sketch I just posted.

Ralph

UnoAccelStepperForum_5.ino

Ralph Hulslander

unread,
Mar 16, 2022, 4:42:44 PM3/16/22
to accelstepper
Jim, when running your sketch the motor does not run.
-5
stopping
find stop
start: -5
end: 30000
After set -- start: -5
end: 30000

stopping
back to start
start: -5
end: 30000
After set -- start: -5
end: -4
stopping

The code ends instantly when the button is pressed.
It appears to instantly find the end limit.

Ralph
Message has been deleted

Ralph Hulslander

unread,
Mar 17, 2022, 11:46:42 AM3/17/22
to accelstepper
Boy you can sure tell I am not used to running programs and testing.

You would think that I know the first thing when a running program no longer works to test your wiring.

Well I finally checked all of the wiring and found a disconnected wire.

Now it works!! So much fun to see the motor do what it is supposed to.

Thanks so much for all of the help and Jim especially for the great sketch and Chris for the mods!

Ralph



On Wednesday, March 16, 2022 at 4:48:43 PM UTC-4 Ralph Hulslander wrote:
Chris, when running your code the motor runs constantly (and in my code also).
0
find stop
start: 0
end: 300000
After set -- start: 0
end: 300000
Pot  675  Motor Speed   2640 Current Speed  46.78 Current Position  0
Pot  675  Motor Speed   2640 Current Speed  2044.93 Current Position  1045
Pot  675  Motor Speed   2640 Current Speed  2640.00 Current Position  3545
Pot  675  Motor Speed   2640 Current Speed  2640.00 Current Position  6126
Pot  675  Motor Speed   2640 Current Speed  2640.00 Current Position  8706
Pot  675  Motor Speed   2640 Current Speed  2640.00 Current Position  11282
0
The only way to stop the motor is to Reset the Arduino.
I swear everything was working when I last uploaded what I thought was working code
but now this.

Ralph

Ralph Hulslander

unread,
Mar 17, 2022, 2:47:39 PM3/17/22
to accelstepper
Motor Speed?

I am running my Version 5 and Chris's 41 version.
There is a big difference in the two motor speeds.

Version 5

Start  0  Target  -30000  Current Position  -26641 ENA 1Pot  1023  Motor Speed   12000 Current Speed  -6529.11 Current Position  -2664
Start  0  Target  -30000  Current Position  -26651 ENA 1Pot  1023  Motor Speed   12000 Current Speed  -6530.33 Current Position  -2665
Start  0  Target  -30000  Current Position  -26661 ENA 1Pot  1023  Motor Speed   12000 Current Speed  -6531.56 Current Position  -2666
Start  0  Target  -30000  Current Position  -26671 ENA 1Pot  1023  Motor Speed   12000 Current Speed  -6532.78 Current Position  -2667
Start  0  Target  -30000  Current Position  -26681 ENA 1Pot  1023  Motor Speed   12000 Current Speed  -6534.01 Current Position  -2668
Start  0  Target  -30000  Current Position  -26691 ENA 1Pot  1023  Motor Speed   12000 Current Speed  -6535.23 Current Position  -2669
Start  0  Target  -30000  Current Position  -26701 ENA 1Pot  1023  Motor Speed   12000 Current Speed  -6536.45 Current Position  -2670
Start  0  Target  -30000  Current Position  -26711 ENA 1Pot  1023  Motor Speed   12000 Current Speed  -6537.68 Current Position  -2671


Version 41
Pot  1023  Motor Speed   12000 Current Speed  93.56 Current Position  -102
Pot  1023  Motor Speed   12000 Current Speed  7000.91 Current Position  2961
Pot  1023  Motor Speed   12000 Current Speed  10733.88 Current Position  7099
Pot  1023  Motor Speed   12000 Current Speed  12000.00 Current Position  11227
Pot  1023  Motor Speed   12000 Current Speed  12000.00 Current Position  15361
Pot  1023  Motor Speed   12000 Current Speed  12000.00 Current Position  19530
Pot  1023  Motor Speed   12000 Current Speed  12000.00 Current Position  23744
Pot  1023  Motor Speed   12000 Current Speed  12000.00 Current Position  27976
Pot  1023  Motor Speed   12000 Current Speed  12000.00 Current Position  32212

Both have the POT at the same setting (1023)

Why is the Current Speed dragging in Version 5?
The motor with Version 5 is hardly turning also, why?

Both sketches have the Same RUN parameters.

Ralph

Chris Toop

unread,
Mar 17, 2022, 3:16:50 PM3/17/22
to accelstepper
Just guessing here but you have a lot of serial statements within your code, more in version 5 than 4.1, I find that although they are useful when you are debugging they do cause delays in the code run through. Have you tried commenting them out and then running the code, ok I know you won't see what your software is doing but it is fundamentally working. I tend to only use serial debugging statements for the issue that I am having then as soon as that section of code is working take the serial debugging code out.

As I say, just a guess I could be completely wrong.

Chris..

Ralph Hulslander

unread,
Mar 17, 2022, 3:34:27 PM3/17/22
to accels...@googlegroups.com
Thanks Chris, that made a big difference.

Ralph

Ralph Hulslander

unread,
Mar 31, 2022, 3:06:54 PM3/31/22
to accelstepper
New problem:

At about 7 seconds you hear a change in the motor, a higher pitch, that is when I close the STOP switch.
The motor runs for another 9 seconds before stopping.

I loaded a UNO with the program and when to my shop to try it on my lathe.
It occasionally ran for a while then stop then refused to run then it would run for 10 seconds and then reverse
and run for ten seconds .

I brought the motor and driver to my home desk and the motor would not run.

Finally I switched the UNO for the Adafruit Metro which I had been using during development.
With the Metro most everything works, except for the STOP.

And I have to figure out how the Enable MF+ MF- works on CL86Y stepper driver.

Ralph Hulslander

unread,
Mar 31, 2022, 3:40:36 PM3/31/22
to accelstepper
Now the MF+ and MF- Enable is working! I had just shut everything down and restarted and it is working
as planned. The switch instantly stops the motor.
I do not knw why the STOP continues to run, but at least I can live with it.

Ralph

Ralph Hulslander

unread,
Feb 27, 2023, 5:16:44 PM2/27/23
to accelstepper
One year later!
I "had" the code running perfectly at my desk.
I hadn't gotten the Z Axis motor mounted so I could not test on my lathe.
Now I have the motor mounted and am ready to test.
Nothing happened when I tried to run the code in my shop on the lathe  .

I "thought" I would try at my desk again.
At my desk I am using a TB6600 clone.
It took me two weeks to get to running the code and being able to test


The motor does not make a full revolution but stops and the noise continues.

This is Jim Larson's code with modifications: I tried to remove all of the unused println but got errors.

// 02/09/2023  believe this is the version installed! I it came up when I opened Arduino.



// Include the AccelStepper Library
#include <AccelStepper.h>
#include <elapsedMillis.h>


// Motor Connections (constant current, step/direction bipolar motor driver)
const int dirPin = 4;

const int stepPin = 5;


AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);           // works for a4988 (Bipolar, constant current, step/direction driver)




// State definitions
#define READ_BUTTONS 01
#define RJSTR 02
#define JOG_CMD 03
#define STOP_NOW 04
#define JOG_RECOVER 05

// State variable
long STOP;
int state;
elapsedMillis printTime;

volatile int enabFlag;  // controls the end stop sensor interrupt


long int myStart = 0;
long int endLimit = -300000;
bool findLimit = true;
bool jogged = false;
//Button pin assignments
int JogLt = A2;

int JogRt = A1;
int Run = A3;
int Pot = A0;
const int ENA = 7;
int Limit_1 = A5;
int sensorReading;
int motorSpeed;

void setup () {
  Serial.begin(115200);
  attachInterrupt(digitalPinToInterrupt(2), rising, FALLING);  // Interrupt 0 is pin D2

  enabFlag = 1; // enable the sensor interrupt
  state = READ_BUTTONS;   // initial state is to read buttons

 
  myStepper.setMaxSpeed(4500.);
  myStepper.setAcceleration(2000.0);

  // Buttons
  pinMode(JogRt, INPUT_PULLUP);
  pinMode(JogLt, INPUT_PULLUP);
  pinMode(Run, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
   pinMode(Limit_1,INPUT_PULLUP); // Limit Switch needs to be 'pulled' LOW (connected to GND) to operate
   pinMode(ENA,OUTPUT);                  // ENA, enable pin is set to OUTPUT
 

  myStepper.moveTo (myStart );
  myStepper.runToPosition();
  //Serial.println(myStepper.currentPosition());

  digitalWrite(ENA, HIGH);

}

void loop () {
  if (digitalRead(Limit_1) == LOW) // First 'if', Limit Switch is operated and pin input is grounded or LOW
  {
    digitalWrite(ENA, LOW);

    //Serial.print("Pot  ");
    /*
        Serial.print(sensorReading);
        Serial.print("  Motor Speed   ");
        Serial.print(motorSpeed);
        Serial.print(" Current Speed  ");
        Serial.print(digitalRead(Limit_1));
        Serial.print("++++");
        Serial.print(digitalRead(ENA));
        Serial.print("++++");
        Serial.print("  Start  ");
        Serial.print(myStart);
        Serial.print("  Target  ");
        Serial.print(myStepper.targetPosition());
        Serial.print("  Current Position  ");
        Serial.print(myStepper.currentPosition());
        Serial.print("find stop");
        Serial.print("start: ");
        Serial.print(myStart);
        Serial.print("end: ");
        Serial.print(endLimit);

        Serial.print("After set -- start: ");
        Serial.print(myStart);
        Serial.print("end: ");
        Serial.println(endLimit);
   */

    myStepper.stop();
      myStepper.runToPosition();  // brings to a stop!
    myStepper.moveTo(myStart);
        findLimit = true;
        state = RJSTR;
   
   
  }
  if (digitalRead(Limit_1) == HIGH) // Second 'if', Limit Switch is operated and pin input is 'PULLED UP' or HIGH.
  {                                 // Basically not connected because you told the Limit-1 pin to be INPUT_PULLUP
    digitalWrite(ENA,HIGH);
    /*
        Serial.print("  Start  ");
        Serial.print(myStart);
        Serial.print("  Target  ");
        Serial.print(myStepper.targetPosition());
        Serial.print("  Current Position  ");
        Serial.print(myStepper.currentPosition());
*/
        /*
        Serial.print("find stop");
        Serial.print("start: ");
        Serial.print(myStart);
        Serial.print("end: ");
        Serial.print(endLimit);

        Serial.print("After set -- start: ");
        Serial.print(myStart);
        Serial.print("end: ");
        Serial.print(endLimit);
         Serial.print("----");
         */          
    //Serial.print(digitalRead(Limit_1));
    //Serial.print(" ENA ");
    //Serial.print(digitalRead(ENA));
    //Serial.print("  Pot  ");
    //Serial.print(sensorReading);

        //Serial.print("  Motor Speed   ");
        //Serial.print(motorSpeed);
        //Serial.print(" Current Speed  ");
        //Serial.println(myStepper.speed());
        //Serial.print(" Current Position  ");
        //Serial.println(myStepper.currentPosition());
       
        /*
        Serial.print("++++");
        Serial.print("  Start  ");
        Serial.print(myStart);
        Serial.print("  Target  ");
        Serial.print(myStepper.targetPosition());
        Serial.print("  Current Position  ");
        Serial.print(myStepper.currentPosition());
        Serial.print("find stop");
        Serial.print("start: ");
        Serial.print(myStart);
        Serial.print("end: ");
        Serial.print(endLimit);

        Serial.print("After set -- start: ");
        Serial.print(myStart);
        Serial.print("end: ");
        Serial.println(endLimit);
    */

  }
  switch (state) {
    case READ_BUTTONS:
      if ( digitalRead(JogRt) == LOW )
      {
        //Serial.println("jog right");
        //Serial.println(myStepper.currentPosition());

        myStepper.move (-100 );    // right is assumed positive
        //Serial.println(myStepper.targetPosition());
        myStepper.setSpeed(-100.); // this is the jog speed
        state = JOG_CMD;
      }
      if ( digitalRead(JogLt) == LOW )
      {
        //Serial.println("jog left");
        //Serial.println(myStepper.currentPosition());
        myStepper.move (100 );
        //Serial.println(myStepper.targetPosition());
        myStepper.setSpeed(100.);

        state = JOG_CMD;
      }
      if ( digitalRead(Run) == LOW)
      {
        // if searching for the target limit, set it and then run back to START. Otherwise, just run and wait for STOP
        if (findLimit) {
          //Serial.println("find stop");
          //Serial.print("start: ");
          //Serial.println(myStart);
          //Serial.print("end: ");
          //Serial.println(endLimit);
          myStart = myStepper.currentPosition();
          //Serial.print("After set -- start: ");
          //Serial.println(myStart);
          //Serial.print("end: ");
          //Serial.println(endLimit);

          if (jogged) {
            state = JOG_RECOVER;
          }
          else {
            myStepper.moveTo(endLimit);
            enabFlag = 1; // enable the sensor interrupt
            findLimit = false;
            state = RJSTR;
          }
        }
        else { // heading back to start
          //Serial.println("back to start");
          //Serial.print("start: ");
          //Serial.println(myStart);
          //Serial.print("end: ");
          //Serial.println(endLimit);
          endLimit = myStepper.currentPosition();
          //Serial.print("After set -- start: ");
          //Serial.println(myStart);
          //Serial.print("end: ");
          //Serial.println(endLimit);

          if (jogged) {
            state = JOG_RECOVER;
          }
          else {
            myStepper.moveTo(myStart);
            enabFlag = 1; // enable the sensor interrupt
            findLimit = true;
            state = RJSTR;
          }
        }
      }
      delay(200);  // tune for desired response time
      break;
    case JOG_RECOVER:
      Serial.println("recovering");
      myStepper.setCurrentPosition(myStepper.currentPosition());
      if (findLimit) {
        myStepper.moveTo(endLimit);
        findLimit = false;
      }
      else
      {
        myStepper.moveTo(myStart);
        findLimit = true;

      }
      enabFlag = 1; // enable the sensor interrupt
      jogged = false;
      state = RJSTR;

      break;
    case JOG_CMD:
      Serial.println("jogging");
      while (myStepper.distanceToGo() != 0) { // make the move before proceeding
        myStepper.runSpeed();
      }
      //Serial.println(myStepper.currentPosition());

      state = READ_BUTTONS;   // read buttons again.
      jogged = true;

      break;
    case RJSTR:
      //Serial.println("running");
      if (printTime >= 1000) {    // happens once per second
        printTime = 0;
        // Use Pot to set myStepper.setMaxSpeed(?????);
        // read the sensor value:
         sensorReading = analogRead(A0);
        // map it to a the maximum speed range
         motorSpeed = map(sensorReading, 0, 1023, 5, 12000);
        myStepper.setMaxSpeed(motorSpeed);

        Serial.print("  Start  ");
        Serial.print(myStart);
        Serial.print("  Target  ");
        Serial.print(myStepper.targetPosition());
        Serial.print("  Current Position  ");
        Serial.print(myStepper.currentPosition());
        Serial.print("find stop");
        Serial.print("start: ");
        Serial.print(myStart);
        Serial.print("end: ");
        Serial.print(endLimit);


       
       
        // set the motor speed:
        //Serial.print("====");
        //Serial.print(sensorReading);
        //Serial.print("====");
        //Serial.print(motorSpeed);
        //Serial.print("====");
        //*/


        //Serial.print(myStepper.speed());
        //Serial.print("  ");
        //Serial.println(myStepper.currentPosition());

      }
      if (!myStepper.run()) {
        state = READ_BUTTONS;
        //Serial.println("done with run");
      }
      break;
    case STOP_NOW:
      //Serial.println("stopping");
      myStepper.setAcceleration(4000);  // this makes motor stop much quicker!

      myStepper.stop();
      myStepper.runToPosition();  // brings to a stop!
      myStepper.setAcceleration(4000.0);  // slow motor acceleration back down
      state = READ_BUTTONS;
      break;
  }
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++ Interrupt service routine +++++++++++++++++++++++++++++
//  Come here if rising edge on D2.
//  If enable flag is true, enter state STOP_NOW
void rising() {
  if (enabFlag == 1) {
    state = STOP_NOW;
    enabFlag = 0;
  }
}

Appreciate the help.

Jim Larson

unread,
Feb 27, 2023, 7:18:49 PM2/27/23
to accelstepper
Hi Ralph!
Glad you're back on the project.
Since 4000 steps per second is a fairly high rate, I'd suggest that your first try a slower rate - maybe 200 or even 50. If that works, you can try increasing the speed in steps to find the maximum that works.Be sure to change the maxSpeed setting since that's the limit AccelStepper will try to accelerate to.
You might also give your wiring a careful check over.

      -jim
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages