trying to get stepper to step very slowly

Hi all,

I'm new to arduino/electronics/coding so forgive me if the answer is very obvious.

I'm trying to rotate a bipolar Nema 17 stepper with 200 steps (42shd0404-22) very slowly, about 1 step every 15 mins. This is to slowly stretch silicone that I'll be growing cells on in the lab. I'm using an arduino nano and an a4988 stepper driver. The transition between steps doesn't need to be especially smooth).

I can get the motor to rotate in both directions as instructed, but encounter problems when there is much more than about 30 seconds delay between steps. With longer than 30ish seconds the motor doesn't step and nothing at all seems to happen. I wired the boards and motor in the manner described by Howtomechatronix at How To Control a Stepper Motor with A4988 Driver and Arduino - YouTube so I think everything is wired correctly.

Here is the code that I'm using: (set to rotate in both directions by 100 steps with 30 seconds between steps):

// testing a stepper motor with a Pololu A4988 driver board or equivalent
// this version uses delay() to manage timing

byte directionPin = 4;
byte stepPin = 3;
int numberOfSteps = 100;
int pulseWidthMicros = 20; // microseconds
int millisbetweenSteps = 30000; // milliseconds

void setup() {

Serial.begin(9600);
}

void loop() {

delay(2000);

pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);

digitalWrite(directionPin, HIGH);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);

delay(millisbetweenSteps);

}

delay(3000);

digitalWrite(directionPin, LOW);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);

delay(millisbetweenSteps);

}
}

the following definition is using an int which is 16 bits with a maximum value of 32767

int millisbetweenSteps = 30000; // milliseconds

the delay() function takes a unsigned long int parameter
https://www.arduino.cc/en/Reference/Delay
which has a maximum 4,294,967,295

Thanks Horace! As I said I'm a complete novice, so I'll have to go and read up a bit around what you've said. In a nutshell, I think you're saying that the use of int prevents me from going longer than 30 seconds, so I'll need to come up with a different code??

for a start change the type of millisbetweenSteps so it can store larger delay values, e.g. for a 15 minute delay

unsigned long int millisbetweenSteps = 15*60*1000; // milliseconds

one of the first things to do when starting to work on a new computer or compiler is to check the number of bytes allocated to ints, long ints, floats, etc - it can make porting code across systems or transferring binary data between system very difficent

unsigned long int millisbetweenSteps = 15*60*1000; // milliseconds

The default for the constants is integer, so the right side will overflow even thought the left is typed unsigned long. Integer Constants - Arduino Reference

You need to use the UL specifier.

unsigned long int millisbetweenSteps = 15*60*1000UL; // milliseconds

Horace and Cattledog. Thank you both very much for your help. Problem solved!!

My first experience on the forum has been a very good one! :slight_smile: :slight_smile: :slight_smile: