I have been having some trouble with my NEMA 17 Stepper Motor. I have created code that controls the direction of my motor. You will see in the code box the description of the program.
The issue that I am having is that my motor will only step for 12 minutes which completes 1.25 rotations. I am unsure what the problem is but I think it may have to do with my code. I tried using Accel Library, but I could not fully grasp it how I did with Stepper Library.
/*
* Name:
* Date:
* Description: This program will control what is to happen to the motor when different buttons or switches are pressed or toggled.
*
* -When the Switch is toggled to [ON]-OFF-ON a stepper motor will accelerate clockwise continuously.
* -When the Switch is toggled to ON-OFF-[ON] the motor will switch to a counter-clockwise rotation.
* -When the Switch is toggled to ON-[OFF]-ON, allow or check the push buttons to function.
* -When Button 1 is pressed, the motor steps clockwise.
* -When Button 2 is pressed, the motor steps counter-clockwise.
*/
#include <Stepper.h>
const int cwPin = 7;
const int ccPin = 8;
const int ScwPin = 13;
const int SccPin = 12;
const int stepPin = 3;
const int dirPin = 2;
const int StepperMaxRPM = 300;
int steps = 0; // Steps integer that increments
float lsteps = steps; // Long form of steps for calculations
float initspeed = 0.26381147; // Initial speed, unmodified, in steps/second
float accelerat = 0.00009666; // Acceleration, unmodified, in steps/second^2
float minitsped = 0.99926844; // Modification of speed, slope value
float binitsped = 0.00007738; // Modification of speed, constant value
float maccelert = 1.00048765; // Modification of acceleration, slope value
float baccelert = 0.00000002; // Modification of acceleration, constant value
float realspeed = minitsped*initspeed+binitsped; // Modified speed for use in constant speed calc
float realaccel = maccelert*accelerat+baccelert; // Modified acceleration for use in constant speed calc
// Speed as a function of time :: a*t + v0
// Time as a function of steps :: (v0/a)*((2*a*steps/v0/v0+1)^1/2-1)
// Modified speed as a function of time :: a'*t + v0' = (m_a*a+b_a)*t+(m_v*v0+b_v)
// Modified speed as a function of step :: (m_a*a+b_a)*(v0/a)*((2*a*steps/v0/v0+1)^1/2-1)+(m_v*v0+b_v)
// Modified time as a function of steps :: 1/((m_a*a+b_a)*(v0/a)*((2*a*steps/v0/v0+1)^1/2-1)+(m_v*v0+b_v))
float spdfxnstp = (float)(realaccel*(initspeed/accelerat)*(sqrt(2.0*accelerat*lsteps/pow(initspeed,2.0)+1.0)-1.0)+realspeed);
long timfxnstp = round(1/(spdfxnstp/1000));
Stepper stepper(1600, stepPin, dirPin);
void setup()
{
pinMode(ScwPin, INPUT);
pinMode(SccPin, INPUT);
pinMode(cwPin, INPUT);
pinMode(ccPin, INPUT);
}
void loop()
{
// Step forward until toggled OFF
while(digitalRead(ccPin) == HIGH)
{
steps++;
lsteps = steps;
spdfxnstp = (float)(realaccel*(initspeed/accelerat)*(sqrt(2.0*accelerat*lsteps/pow(initspeed,2.0)+1.0)-1.0)+realspeed);
timfxnstp = round(1/(spdfxnstp/1000));
delay(timfxnstp/32);
stepper.step(1);
}
// Step reverse until toggled OFF
while(digitalRead(cwPin) == HIGH)
{
steps++;
lsteps = steps;
spdfxnstp = (float)(realaccel*(initspeed/accelerat)*(sqrt(2.0*accelerat*lsteps/pow(initspeed,2.0)+1.0)-1.0)+realspeed);
timfxnstp = round(1/(spdfxnstp/1000));
delay(timfxnstp/32);
stepper.step(-1);
}
// Step forward until button1 is let go
while(digitalRead(SccPin) == HIGH)
{
stepper.setSpeed(StepperMaxRPM);
stepper.step(1);
}
// Step reverse unitil button2 is let go
while(digitalRead(ScwPin) == HIGH)
{
stepper.setSpeed(StepperMaxRPM);
stepper.step(-1);
}
}
Add serial debug prints of 'spdfxnstp' and 'timfxnstp'.
If you want people to troubleshoot your code in future, please use meaningful, readable variable names.
What would I be looking for exactly? Those lines of code only mess with the delay between steps.
mrstylishnerd:
What would I be looking for exactly? Those lines of code only mess with the delay between steps.
Break up the long line of code into several separate lines each of which does one part of the calculation. Then you will be able to print the intermediate values to satisfy yourself that the calculation is doing what you think it is.
There is no advantage in having all the code on one line.
// Step forward until toggled OFF
while(digitalRead(ccPin) == HIGH)
{
start = micros(); // Start mesuring time in microseconds
steps++;
lsteps = steps;
spdfxnstp = (float)(realaccel*(initspeed/accelerat)*(sqrt(2.0*accelerat*lsteps/pow(initspeed,2.0)+1.0)-1.0)+realspeed);
timfxnstp = round(1/(spdfxnstp/1000));
delay(timfxnstp/32);
stepper.step(1);
finish = micros(); // Finish mesuring time in microseconds
elapse = finish-start;
total = total+elapse;
Serial.print(lsteps,0);
Serial.print("\t");
Serial.print("\t");
Serial.print(spdfxnstp,8);
Serial.print("\t");
Serial.print(elapse);
Serial.print("\t");
Serial.print("\t");
Serial.println(total);
}
I is similar to the other switch, I just did not want to keep posting the same code over and over again...
Also, I assume that while my motor moves, it should not be humming a tune... As the motor accelerates slowly there is a low sounding tune I can here. Would this be an issue with setting the current limiter on the Driver chip?
mrstylishnerd:
What would I be looking for exactly? Those lines of code only mess with the delay between steps.
Well, just for an example, what if the delay between steps became extremely long? Would that not stop the motor, which is your complaint? By the way, why ask questions before you even begin? Just run the debug, it can't hurt you.
I ran the Debug in the Serial Monitor and nothing looked out of the ordinary. The speed was slowly increasing as it needed to be. The delay between each step became shorter, which is what needed to happen. But progression stop at 12 minutes and 10 seconds.
mrstylishnerd:
I ran the Debug in the Serial Monitor and nothing looked out of the ordinary. The speed was slowly increasing as it needed to be. The delay between each step became shorter, which is what needed to happen. But progression stop at 12 minutes and 10 seconds.
Please post the complete program that behaves like that.
If you can capture a sample of the output just before and including the moment the progression stops please also post that.
Perhaps one or more of your variables is overflowing because the value it is expected to contain gets too big. For example the max for an int is 32767 and if you increment that by one it becomes -32768 which could cause a problem. If that is the problem change the int to a long.
Hi,
You can use 10K rather than 1K resistors as pull down resistors.
What are you using for your 9V power source?
A 9V smoke detector battery will not last very long if that is what you are using.
Sorry, I didn’t notice that my list at the bottom left wasn’t updated... I am currently during a 12 V adaptor that plugs into the wall. I learn that the battery would not support my motor, so I was encouraged to switch to a 12 V adapter. Also why would I need to go from 1k Ohms to 10 k Ohms. I do have a DMM. It’s been my main tool for troubleshooting everything. I also have an Oscilloscope, but I do not understand that tool quite as much.
I have been wondering if it was an issue with Memory... I pondered over that for the weekend while AFK. Then noticing Robin2’s latest post makes me think I need to look into the memory issues.