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
// for your motor
#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
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// 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[0] && current.minute() == OpenTime[1] && 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[0] && current.minute() == CloseTime[1] && 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[0] || current.minute () != OpenTime[1]) && stepCount == 200)
; {
stepCount=0;
}
}