Here is the code:
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int HomeSwitch = 2;
const int ledPin = 3;
const int CW = 1; //direction of rotation
const int CCW = 0;
int Pin1 = 12;
int Pin2 = 11;
int Pin3 = 10;
int Pin4 = 9;
int _step = 0;
//boolean dir = false;// false=clockwise, true=counter clockwise
int count=0;
int RotationSpeed = 80;
int HomeSpeed = 98;
int ledState = LOW;
/////////////////////////////
// enter rotation angle here
const float StepsPerRotation = 4076;
float DegreeForRotation = 90; // enter desired rotation degrees
float StepsforSelectedRotation;
/////////////////////////////
// enter time period here
float HoursToRotate = 0.0083;
float SecondsToRotate;
float MilisecondToRotate;
float DelayBetweenStep;
unsigned long currentMillis;
unsigned long previousMillis = 0;
const long interval = 100;
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void setup()
{
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
pinMode(Pin4, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(HomeSwitch, INPUT);
while (!Serial); // for Leonardo/Micro/Zero
Serial.begin(115200);
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(2014, 1, 21, 3, 0, 0));
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(1000);
Find_home();
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(1000);
/////////////////////////////////////////////
//calculations
/////////////////////////////////////////////
StepsforSelectedRotation = ((StepsPerRotation / 360) * DegreeForRotation);
Serial.println("//////////////////////// ");
Serial.println(StepsforSelectedRotation);
StepsforSelectedRotation = round(StepsforSelectedRotation);
Serial.println("//////////////////////// ");
Serial.print("Number of Steps :");
Serial.println(StepsforSelectedRotation);
SecondsToRotate = HoursToRotate * 60 *60 ;
MilisecondToRotate = SecondsToRotate * 1000;
DelayBetweenStep = (MilisecondToRotate / StepsforSelectedRotation);
Serial.println("//////////////////////// ");
Serial.print("Delay Between Each Step:");
Serial.println(DelayBetweenStep);
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void loop()
{
Serial.println("//////////////////////// ");
Serial.print("Delay Between Step: ");
Serial.println(DelayBetweenStep);
DateTime now = rtc.now();
GetTime();
unsigned long currentMillis = millis();
Step(CW,StepsforSelectedRotation,DelayBetweenStep);
delay(1000);
Find_home();
delay(1000);
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
void Step(int Direction,int Steps, int Speed_delay){
for (int i=0; i< Steps; i++){
if(Direction == CW){
_step++;
Serial.println("//////////////////////// ");
Serial.print("Step Clockwise: ");
Serial.print (i);
Serial.print (" Of ");
Serial.println (StepsforSelectedRotation);
}
if(Direction == CCW){
_step--;
Serial.println("//////////////////////// ");
Serial.println("Step Counter Clockwise: ");
Serial.print (i);
Serial.print (" Of ");
Serial.println (StepsforSelectedRotation);
}
if(_step>7){ // overflow reset
_step=0;
}
if(_step<0){
_step=7;
}
switch(_step){
case 0:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, HIGH);
break;
case 1:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, HIGH);
break;
case 2:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, LOW);
break;
case 3:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, LOW);
break;
case 4:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 5:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 6:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 7:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, HIGH);
break;
default:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
}
delay(Speed_delay);
Serial.print("Delay Between Step: ");
Serial.println(Speed_delay);
Serial.println ("");
}
}
/////////////////////////////////////////////////////////
void Find_home(void){
while(digitalRead(HomeSwitch)==1){ // test limit switch
Step(CCW,1,0); // move CCW to limit switch one step at a time without delay.
}
Serial.print("FOUND HOME");
return;
}
/////////////////////////////////////////////////////////
void GetTime(void){
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}