I am very new to Arduinos and coding, so please bare with me. I have about a 700 post thread about a sawmill head lift that some great members on here have been very patient and helpful in guiding me through. That learning process has led me to this current project.
Currently, I have a linear actuator connected to a BTS7960 motor driver. I have coded an Arduino to open the door at a set time in the morning and close it at a set time at night. The door has limit switches on each end to turn the motor off. I am using the DS3231 RTC to keep the time. It is working as I expect it to, but I would like to improve it and I am not sure how or if it is possible. I have pasted the code at the bottom:
I am an ALEXA fan. I have many smart devices that I can control with ALEXA. What I would like to do is basically have the same setup as I do now. The door opens in the morning and closes at night, but I would also like to be able to control it with a smart relay/ ALEXA. So, that I can Open/Close it any time I want via ALEXA.
What I am thinking is:
I have an old 40A DC motor speed controller with a forward and reverse switch lying around (PWM does not work correctly, but I should only need 100% power for this project).
All this controller needs in order to come on is for one leg of the Forward/Reverse switch to go to ground.
I am fairly confident I can make it work using the arduino and a 2 ch relay to switch f/r and still have the limit switches turn the motor off.
However, if I use a smart Relay, how would I be able to have the limit switches tell the arduino/ALEXA the door is opened/closed?
This may be really simple, but I haven't been able to find something like this while searching the forum.
This is the current code I am using:
#include <RTClib.h> // for the RTC
#include <Wire.h>
// Instance of the class for RTC
RTC_DS3231 rtc;
char t[32];
int RelayUp = 4;
int RelayDown = 5;
int switchA = 6;
int switchB = 7;
const int OnHourUp = 6; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMinUp = 38;
const int OnHourDown = 21; //SET TIME TO OFF RELAY
const int OnMinDown = 8;
const int upHour;
const int upMin;
const int downHour;
const int downMin;
int CurrentHour;
int CurrentMinute;
void setup() {
Serial.begin(9600);
Wire.begin();
rtc.begin();
if(rtc.lostPower()) { // uncomment this line on the second time upload
rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
}
//If time is off comment out the if statement above and adjust the date in the line below
//rtc.adjust(DateTime(2021,6,9,16,6,0));
//The time set above is June 8, 2021 6 16 AM uncomment it
//Once the time has been set correctly comment out the line rtc.adjust(DateTime(2021,6,8,6,20,0)); line and then
//uncomment the if statement above that you previously commented out to adjust the time
pinMode(switchA, INPUT_PULLUP);
pinMode(switchB, INPUT_PULLUP);
pinMode(RelayUp, OUTPUT);
pinMode(RelayDown, OUTPUT);
}
void loop() {
DateTime now = rtc.now();
// Save check in time;
sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year());
CurrentHour = now.hour();
CurrentMinute = now.minute();
Serial.print(F("Date/Time: "));
Serial.print(t);
Serial.print("\tHour: ");
Serial.print(CurrentHour);
Serial.print("\tMinute: ");
Serial.println(CurrentMinute);
if(CurrentHour == OnHourUp && CurrentMinute == OnMinUp){
Serial.println("Door Opening");
doorOpening();
}
if(CurrentHour == OnHourDown && CurrentMinute == OnMinDown){
Serial.println("Door Closing");
doorClosing();
}
if(digitalRead(switchA)==LOW){
Serial.println("switchA was read");
}
if(digitalRead(switchB)==LOW){
Serial.println("switchB was read");
}
if(CurrentHour == upHour && CurrentMinute == upMin){
Serial.println("Door Opening");
doorOpening();
}
if(CurrentHour == downHour && CurrentMinute == downMin){
Serial.println("Door Closing");
doorClosing();
}
}
void doorOpening()
{
while(digitalRead(switchA)== HIGH){
digitalWrite(RelayUp,HIGH);
digitalWrite(RelayDown,LOW);
}
if(digitalRead(switchA)== LOW){
digitalWrite(RelayUp,LOW);
digitalWrite(RelayDown,LOW);
Serial.println("Door Opened");
delay(60000);
}
}
void doorClosing()
{
while(digitalRead(switchB)== HIGH){
digitalWrite(RelayDown,HIGH);
digitalWrite(RelayUp,LOW);
}
if(digitalRead(switchB)== LOW){
digitalWrite(RelayDown,LOW);
digitalWrite(RelayUp,LOW);
Serial.println("Door Closed");
delay(60000);
}
}
Thanks!

