Created
December 10, 2019 15:29
-
-
Save scealux/4456dedaaabe17f41e508fc29a29237b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DS3231.h> //Realtime Clock (RTC) Library from: http://www.rinkydinkelectronics.com/library.php?id=73 | |
#include <Stepper.h> //Arduino Stepper Library | |
//----------------------------------------- Limit Switch ----------------------------------------- | |
int switchPin = 6; | |
//----------------------------------------- Stepper Motor ----------------------------------------- | |
const int stepsPerRev = 200; //360 degrees / 1.8 degrees per step | |
const int sleepPin1 = 2; //Pins for enabling or disabling the h-bridge | |
const int sleepPin2 = 3; | |
long atStep = 0; //What step the tape measure thinks it's on | |
int homingSteps = 20; //Steps needed to test homing | |
int stepsPer24In = 1000; //How many steps it takes the stepper to extend the tape to | |
Stepper stepBoi(stepsPerRev,8,9,10,11); //Initialize stepper | |
//----------------------------------------- Realtime Clock ----------------------------------------- | |
DS3231 rtc(SDA, SCL);// Initialize the DS3231 | |
Time t; // Init a Time-data structure | |
long h; // *Have to use longs here because the numbers get so large.* | |
long m; | |
int s; | |
long tot; //Total time in seconds | |
long hrMult = 3600 ; //Multiplier for hours to seconds | |
int minMult = 60; //Multiplier for minutes to seconds | |
long secondHours; //Hours in seconds | |
long secondMinutes; //Minutes in seconds | |
bool am = true; | |
//---------------------------------------------------------------- SETUP ---------------------------------------------------------------- | |
void setup() { | |
pinMode(switchPin, INPUT_PULLUP); | |
pinMode(sleepPin1,OUTPUT); | |
pinMode(sleepPin2,OUTPUT); | |
rtc.begin(); //Start the rtc chip | |
Serial.begin(9600); //Start serial communication | |
homeTape(); //Home the tape for startup | |
} | |
//---------------------------------------------------------------- LOOP ---------------------------------------------------------------- | |
void loop() { | |
long currentSeconds = getRTCTime(); //Get current time in seconds | |
if (currentSeconds > 43200) {//Noon Condition | |
if (am == true){ | |
am = false; | |
homeTape(); | |
}; | |
Serial.println("It is after noon"); | |
currentSeconds = currentSeconds - 43200; | |
}else{ | |
Serial.println("It is before noon"); | |
} | |
if (currentSeconds >= 86220) {//Midnight Condition | |
if (am == false){ | |
am = true; | |
homeTape(); | |
}; | |
}; | |
long shouldBeAt = map(currentSeconds, 0, 43200, 0, 275); //REPLACE 1000 with number of steps for 24in and 2000 with number of seconds in 24 hrs. | |
shouldBeAt += 15; //Because she fickle | |
Serial.println("At Step = " + String(atStep) + " Should be at = " + String(shouldBeAt)); | |
long directions = shouldBeAt - atStep; //Figures out the difference in where the tape measure should be vs where it is | |
Serial.println("Trying to step " + String(directions) + " steps."); | |
if (directions >= 2) { //If the difference is more than a step, move the stepper to the right position | |
enableStepper(); | |
for (int i = 0; i < directions; i++){ | |
stepBoi.step(1); | |
atStep++; | |
delay(200); | |
}; | |
disableStepper(); | |
} | |
if (currentSeconds > 3600 && digitalRead(switchPin) == HIGH){ //User can home tape if it's past 1am or pm | |
homeTape(); | |
}; | |
} | |
//---------------------------------------------------------------- FUNCTIONS ---------------------------------------------------------------- | |
void homeTape(){ //Homes the tape measure for accuracy | |
enableStepper(); | |
Serial.println("Homing"); | |
while (digitalRead(switchPin) == LOW){ //Pulls the tape measure in until the limit switch is pressed | |
stepBoi.step(-1); | |
Serial.print("."); | |
delay(200); | |
}; | |
Serial.println(""); | |
Serial.println("Hit once."); | |
for (int i = 0; i<homingSteps; i++){ //Extends out a specified number of steps | |
stepBoi.step(1); | |
delay(200); | |
}; | |
delay(500); | |
int numSteps = 0; | |
Serial.println(""); | |
Serial.println("One more time"); | |
while (digitalRead(switchPin) == LOW){ //Retracts the tape measure again, while counting steps | |
stepBoi.step(-1); | |
numSteps ++; | |
Serial.print("."); | |
delay(200); | |
}; | |
atStep = 0; | |
Serial.println(""); | |
Serial.println("Homed with " + String(25-numSteps) + " step difference"); //Good homing would be a 0 step difference. Otherwise errors accumulate and reduce overall accuracy. | |
disableStepper(); | |
}; | |
long getRTCTime(){ //Reads data from the RTC chip and returns current time in seconds (out of 24 hours) | |
Serial.println("Getting RTC Time..."); | |
t = rtc.getTime(); //Retrieve current time data from RTC Chip | |
h = t.hour; | |
m = t.min ; | |
s = t.sec; | |
Serial.println(String(t.hour) + " " + String(t.min) + " " + String(t.sec)); | |
secondHours = h * hrMult; //Convert hours to seconds | |
secondMinutes = m * minMult; //Convert minutes to seconds | |
tot = secondHours + secondMinutes + s; //Add converted times for total seconds | |
Serial.println("Total from RTC = " + String(tot)); | |
return tot; //return current time in seconds (out of 24 hr day); | |
}; | |
void enableStepper(){ //Enables the h-bridge | |
digitalWrite(sleepPin1,HIGH); | |
digitalWrite(sleepPin2,HIGH); | |
}; | |
void disableStepper(){ //Disables the h-bridge | |
digitalWrite(sleepPin1,LOW); | |
digitalWrite(sleepPin2,LOW); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment