# Making a stepper motor turn back and forth at specific times.

So I want to be able to turn the motor in one direction for one revolution at a specific time( for example the motor turns one revolution clockwise at 9:38 am or any other random time) and then be able to turn the motor back in the reverse direction for one revolution at another time. Using an rtc, I figured out in my code how the make the motor turn back and forth at specific times. However, the motor just keeps rotating until that time has finished. For example, if i set the motor to turn clockwise or counterclockwise at 9:38, it will turn in that direction at precisely that time. The problem is the motor will continue to rotate until that minute has finished, meaning that the motor will keep rotating in that direction until 9:38 is over and 9:39 begins. I only want the motor to rotate for one revolution at those specific times.

I tried to resolve this by creating a variable that counts the step of the motor called stepCount. Each time the the code cycles through the loop, stepCount is suppose to increase by 1. Then when stepCount= 200( the # of steps in one revolution for the motor), the motor stops turning in that direction. However, when i tried running the code with these modifications, that did nothing to resolve the problem. When i looked at the serial monitor to see whether stepCount was incrementing, it was not as stepCount was staying constant at zero.

As a result, how do i make stepCount increment properly? Or if the stepCount variable is not the way to solve this problem is there any other way?

``````#include <Stepper.h>
int enA  = 3;  // Enable pin 1 on Motor Control Shield
int enB  = 11;  // Enable pin 2 on Motor Control Shield
int dirA = 12;  // Direction pin dirA on Motor Control Shield
int dirB = 13;  // Direction pin dirB on Motor Control Shield

const int stepsPerRevolution = 200;  // Change this to fit the number of steps per revolution

#include <Wire.h>
#include "RTClib.h"

const int OpenTime [] = {13, 39}; //Opening Time in Hours, Minutes
const int CloseTime []= {13,40};  //Closing Time in Hours, Minutes
int stepCount=0;

RTC_DS1307 rtc;
// Initialize the stepper library on pins 12 and 13:
Stepper myStepper(stepsPerRevolution, dirA, dirB);

void setup() {
while (!Serial);

Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}

if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2017, 4, 2, 13, 57, 0));

}

// set the speed at rpm:
myStepper.setSpeed(60);

// Enable power to the motor
pinMode(enA, OUTPUT);
digitalWrite (enA, HIGH);

pinMode(enB, OUTPUT);
digitalWrite (enB, HIGH);
}

void loop() {
DateTime now = rtc.now ();
DateTime current (now + TimeSpan(0,0,7,35));

if(current.hour() == OpenTime && current.minute() == OpenTime && stepCount < 200)
{
// Step one revolutions into one direction:

myStepper.step(-1);
Serial.print("steps:");
Serial.println(stepCount);
stepCount++;
delay(10);

}

else if (current.hour() == CloseTime && current.minute() == CloseTime && stepCount < 200)
{ // Step one revolutions in the other direction:

myStepper.step(1);
Serial.print("steps:");
Serial.println(stepCount);
stepCount++;
delay(10);

}

else ((current.hour() != OpenTime || current.minute () != OpenTime) && stepCount == 200)
;  {
stepCount=0;

}

}
``````

Add a boolean flag, beenThereDoneThat. Set it to false whenever the time is NOT the time to rotate.

When it is time to rotate, check the flag. If it is false, rotation has not occurred, so rotate and set the flag to true.

Next time through loop(), the time will still be the same, but the flag will be true, so rotation won't happen again.

So i followed your advice and added the boolean variable rotate. However, the same problem keeps happening.

Here is the revised code with the boolean.

``````#include <Stepper.h>
int enA  = 3;  // Enable pin 1 on Motor Control Shield
int enB  = 11;  // Enable pin 2 on Motor Control Shield
int dirA = 12;  // Direction pin dirA on Motor Control Shield
int dirB = 13;  // Direction pin dirB on Motor Control Shield

const int stepsPerRevolution = 200;  // Change this to fit the number of steps per revolution

#include <Wire.h>
#include "RTClib.h"

const int OpenTime [] = {15, 35}; //Opening Time in Hours, Minutes
const int CloseTime []= {15,36};  //Closing Time in Hours, Minutes

boolean rotate = false;

RTC_DS1307 rtc;
// Initialize the stepper library on pins 12 and 13:
Stepper myStepper(stepsPerRevolution, dirA, dirB);

void setup() {
while (!Serial);

Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}

if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2017, 4, 2, 13, 57, 0));

}

// set the speed at rpm:
myStepper.setSpeed(60);

// Enable power to the motor
pinMode(enA, OUTPUT);
digitalWrite (enA, HIGH);

pinMode(enB, OUTPUT);
digitalWrite (enB, HIGH);
}

void loop() {
DateTime now = rtc.now ();
DateTime current (now + TimeSpan(0,0,7,35));

if(current.hour() == OpenTime && current.minute() == OpenTime && rotate == false )
{
// Step one revolutions into one direction:

myStepper.step(-stepsPerRevolution);
rotate=true;

}

else if (current.hour() == CloseTime && current.minute() == CloseTime && rotate == false)
{ // Step one revolutions in the other direction:

myStepper.step(stepsPerRevolution);
rotate=true ;

}

else ( current.hour() != OpenTime || current.minute() != OpenTime || current.hour() != CloseTime || current.minute() != CloseTime)
;  {
rotate=false;
}
}
``````

Shouldn't this

``````else ( current.hour()
``````

be

``````else if ( current.hour()
``````

If this was my project I would convert the time into minutes after midnight so I only need to check one number.

...R

Your right it should be else if. but the same problem still occurs. Converting the time to midnight shoulds like a better idea

Your right it should be else if. but the same problem still occurs.

If you know that the problem persists after making a code change, but you don't post the changed code, all we can do is assume that you screwed up, and move on.

Is that the assumption you want us to make? Is that the action you want us to take?