I am trying to use a step motor which would be controlled by code i.e. manual switch on, rotate a certain number of times CW then pause, then rotate a similar number of times CCW until switched off.
I am starting from scratch and have found code that controls a motor by potentionmeter etc but it is the automation that is leaving me at a loss.
Is your 28BYJ-48 5 volt or 12 volt? Which Arduino are you using? How fast do you want the motor to turn? How many turns? If turned off in the process of turning, should it complete the turn or stop immediately?
Well, that's 30 RPM, too fast for the 28BYJ-48 with it's 64 : 1 gear ratio, but I was able to get 24 RPM using the AccelStepper library. So install that library from the IDE library manager and give this a shot.
/*
* Connect UNO pin 11 to ULN2003 pin 1,
* 10 2,
* 9 3,
* 8 4
*/
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(4, 8, 10, 9, 11);
void setup()
{
// Change these to suit your stepper if you want
stepper.setMaxSpeed(820.0);
stepper.setAcceleration(1000.0);
}
void loop()
{
static int stepsPerRev = 2048,
turns = 4; // set number of turns here
// If at the end of travel go to the other end
if (stepper.distanceToGo() == 0)
stepper.moveTo(stepsPerRev * turns);
if (stepper.distanceToGo() == 0)
stepper.moveTo(0);
stepper.run();
}