Hello, I am trying to have my stepper motor engaged(on) at all times. The stepper motor will have to turn a certin amount at certain prgrammed times. Is it possibel to have this run in a sketch. I am starting with the below setup. Not very experienced in C++.
#include "Dstepper.h"
/* Arduino Sketch for the "Improved Wandering Hour Clock" with a stepper
motor.
https://www.printables.com/model/327198-improved-wandering-hour-clock
This uses an Arduino Nano, although I think any Arduino will work.
Connections are as follows:
D2 Down switch (other end of switch goes to Ground)
D3 Up switch (other end of switch goes to Ground)
D7 Motor driver IN1
D8 Motor driver IN2
D9 Motor driver IN3
D10 Motor driver IN4
VIn Motor driver power +
Gnd Motor driver power -
There are numerous cheap 28BYJ-48 stepper motors with ULN2003
driver boards available on Amazon or eBay. (5 motors & drivers
for around $15.)
*/
/*
4096 Steps/revolution
1092 steps/second (motor is guaranteed 500 steps/second)
According to a datasheet, "Frequency" is 100Hz, which results in 1 rev in 40.96 sec.
Max pull-in frequency = 500 Hz (8.1 sec/rev, 7.32 rpm),
Max pull-out frequency = 900 hz (4.55 sec/rev, 13.2 rpm).
*/
/* It seems that the max RPM these motors can do is ~ 13 RPM, = ~ 1100
mSec per step. It also depends on the amount of current your power
supply can provide.
*/
const int stepsPRev = 4096; /* steps / rev for stepper motor /
const int maxSpeed = 8; / max speed stepper RPM, conservative /
const int upSw = 3; / up switch /
const int downSw = 2; / down switch /
const int led = 13; / built-in led */
const int MSEC_HOUR = (3600L * 1000L);
Dstepper m(7,8,9,10); /* One Motor /
int stepDelay; / minimum delay / step in uSec /
unsigned long ttime = 0;
unsigned long ctime;
unsigned long tStart;
long cStep = 0; / current motor step count */
void dprintf(const char fmt, ...); / for debugging */
int mSecTime(int n, int speed); // compute the time it takes to move n steps at rpm speed.
void setup() {
pinMode(upSw, INPUT_PULLUP);
pinMode(downSw, INPUT_PULLUP);
pinMode(led, OUTPUT);
stepDelay = ((60L * 1000L * 1000L) / stepsPRev) / maxSpeed; /* delay for max speed */
#if 0
Serial.begin(9600);
while (!Serial) ;
Serial.print(stepDelay);
Serial.print(" uSec/step. OK\n");
#endif
m.setPosition(0); /* initialize stepper /
m.moveToUsec(-stepsPRev/4, stepDelay); / just for the heck of it, 1/4 turn ccw /
while (m.check()) delay(1); / wait /
m.moveToUsec(0, stepDelay); / and back /
while (m.check()) delay(1); / wait */
cStep = 0;
tStart = ctime = ttime = millis();
}
/* We go 1 rev = 4096 steps / hour. Let's move every 10 seconds. */
char inMove = 0;
void loop() {
if (m.check()) return; /* motor active? if so, do nothing */
if (digitalRead(upSw) == 0 || digitalRead(downSw) == 0) { /* button pressed? fast move /
int delta = stepsPRev / 60; / one minute's worth /
if (digitalRead(downSw) == 0) delta = - delta; / backwards? /
cStep += delta;
m.moveToUsec(cStep, stepDelay);
inMove = 1; / show we are setting /
return;
}
if (inMove) { / ending a move? /
inMove = 0; / reset all after move */
m.setPosition(0);
cStep = 0;
tStart = millis();
}
/* Every 10 seconds, do a little move... /
ctime = millis();
if (ctime > (ttime + 10000)) { / time for update? /
ttime = ctime;
long tPos = (ctime - tStart) /1000; / seconds since starting /
cStep = (stepsPRev * tPos) / 3600; / desired motor position /
m.moveTo(cStep, 1000); / move slowly /
return;
}
if (ctime > (tStart + MSEC_HOUR)) { / hour wrap? Avoid overflow. */
tStart += MSEC_HOUR;
m.setPosition(0);
cStep = 0;
}
}
char buffer[80];
/* Sets and draws a label using formats and parameters from sprintf(). */
void dprintf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsprintf(buffer, fmt, args);
va_end(args);
Serial.print(buffer);
}
// compute the time it takes to move n steps at rpm speed.
int mSecTime(int n, int speed)
{
if (speed <= 0) return 0;
if (speed > maxSpeed) {
dprintf("Speed %d is above max speed.\n", speed);
while (1);
}
if (n < 0) n = -n;
/* to mSec / steps-rev/speed */
return (long) n * 60L * 1000L / stepsPRev / speed;
}