Hi all,
As part of a project I want to drive a motor at specific times every day of the year at sunrise and sunset. However, I don't know how to get the time by itself, ignoring the date, from unix time. Is there some sort of command that allows me to do this?
So far I can only get the motor to run at 2 specific times.
Any help would be greatly appreciated.
This is the code I have so far:
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_MotorShield.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x57);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
RTC_DS3231 rtc;
uint32_t sunrise = 1593575280;
uint32_t sunset = 1593634860;
int counter = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis;
void setup ()
{
Serial.begin(9600);
Serial.println("Starting RTC");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
// while (1);
}
Serial.println("RTC started");
Serial.println("Starting AMS");
AFMS.begin();
Serial.println(" AMS started, setup routine finished");
myMotor->setSpeed(10);
}
void doorUp () {
//delay(2000);
myMotor->step(50, FORWARD, SINGLE);
//delay(5000);
myMotor->step(200, FORWARD, SINGLE);
}
void doorDown () {
myMotor->step(200, BACKWARD, SINGLE);
//delay(5000);
//delay(2000)
myMotor->step(50, BACKWARD, SINGLE);
}
void loop ()
{
delay(1000);
currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
previousMillis = currentMillis;
Serial.print("Loop has run ");
Serial.print(counter);
Serial.println(" times");
counter = counter + 1;
DateTime now = rtc.now();
Serial.println(now.unixtime());
if (now.unixtime() > sunrise and sunrise < (sunrise + 1)) {
doorUp ();
}
if (now.unixtime() > sunset and sunset < (sunset + 1)) {
doorDown ();
}
}
}