I was using this code to run a stepper motor driven by an easydriver to turn a drum 5 rotations and then change directions for another 5 rotations and so on but now I have geared the drum for a slower rotation and need to increase the steps to accomplish the same number of turns on the drum.
This code worked fine before because it was run on an UNO Board but now the step number has increased beyond the INT limit on the 16Bit board. I just purchased a 32 Bit DUE sent the code to it and now the motor doesn't change directions after it hit the step count it just pauses and then continues in the same direction.
I changed the step count to 1600 just to see if it would work properly and it hasn't changed.
What would be the easiest way to make this work properly and why did it change?
void loop() {
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
//1600 Steps for one rotation on the motor
if (Distance == 1600)
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for half a second
delay(2000);
}
}
Please use code tags. If you don't, the forum software converts some code into smileys.
Read the "How to use this forum" post at the top of every forum.
You can use 32-bit long int on the 8-bit Arduinos. You can even use a 64-bit long long. That should be sufficient to count the number of atoms in the universe or something.
If you care about the bit width of variables and want them to be a constant size regardless of CPU architecture, don't use the native types.
Use the stdint types.
i.e. int16_t uint16_t int32_t uint32_t int64_t uint64_t
etc....
If you don't, the compiler will make the choice for you and it may not match what you want/expect.
Try the code below, it should work with a static variable inside loop() :
void setup() {
pinMode(8, OUTPUT); // Dir pin
pinMode(9, OUTPUT); // step pin
//digitalWrite(8, HIGH);
//digitalWrite(9, LOW);
}
void loop() {
static uint32_t Distance; // Record the number of steps we've taken
digitalWrite(9, HIGH);
delayMicroseconds(1000);
digitalWrite(9, LOW);
delayMicroseconds(1000);
Distance++; // record this step
// Check to see if we are at the end of our move
//1600 Steps for one rotation on the motor
if (Distance == 1600)
{
// We are! Reverse direction (invert DIR signal)
digitalWrite(8, !digitalRead(8));
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for 2 seconds ***************
delay(2000);
}
}
I tried putting the long before the two Distances in the code and the L at the end of the number and i'm getting an error -
error: expected primary-expression before 'long'
error: expected ')' before 'long'
I like the idea of this because I can still use the UNO board on this project.
My wiring matches except I'm not using the MS1 wire to port 4, but this code worked fine on the UNO?
It seems like pin 8 starts low then gets changed to high and then stays high and never switches back and forth. If I un plug pin 8 while it is running the motor changes direction and when plugged back in changes again.
I did also notice that when the code first runs it does one rotation (by spefied step count) changes direction and then never changes back.
Did you notice this in Easydriver description from Sparkfun ?:
"The EasyDriver is a simple to use stepper motor driver, compatible with anything that can output a digital 0 to 5V pulse (or 0 to 3.3V pulse if you solder SJ2 closed on the EasyDriver)".
Arduino DUE outputs 0 to 3.3 V !!
(The DUE is 3.3V compliant only, and by chance, there are only inputs from Arduino to Easydrive, otherwise, you would destroy your board !!)
I did not see that I will try it when I get home with a jumper wire first as I am not sure if it means the operating voltage of the easydriver and if this will affect the output voltage of the easydriver, as I'm using the 5v output to power the DUE.
If you are connecting 5V signals from an external device to the Due I/Os, you have almost certainly damaged your Due. If you MUST connect a 5V device to a Due, at least put series resistors (5-10K) on those signal lines to protect the Due.
I am using the vin input on DUE to connect the 5v from the easydriver and how I understood it was that the due could receive a recommended 6v-16v through the vin pin. I just didn't want to connect a 12v battery to both the DUE and the easydriver. id rather fry the easy driver if something was connected wrong.
I do know that 5v is less then the 6v recommended but it seemed to work fine on the UNO