Dear,
I'm a beginner in the Arduino world, and can't solve a particular issue on my own.
I have a 12V geared motor that operates a slide door. On the door i have 2 microswithes that stops the motor when at the end of the travel and activates a door LED light to indicate the state.
I've used a GREEN LED to indicate that the door is CLOSED and a RED LED to indicate it is OPEN.
I have integrated another LED, which should come on when the door is neither full open or full closed. So, this AMBER led is indicating a TRANSIT situation.
Now, i've been testing around with several IF, Else and IF statements, but i never get it to work like i wish. Whenever the door is in transit, both the GREEN and RED led's should be OFF and the AMBER on. Once at eiher side end-of travel, the AMBER Led should go out, and either the FULL OPEN RED led or the FULL CLOSED GREEN Led should come on.....
My actual code looks like this:
#include <math.h>
#include <Dusk2Dawn.h>
#include <EEPROM.h>
#include <Wire.h>
#include <RTClib.h>
//Define what pins are used
const int doorUpSensorPin = 4; // reedswitch door open
const int doorDownSensorPin = 5; // reedswitch door down
const int doorDownMotorPin = 10; // wind motor down
const int DoorUpMotorPin = 9; // wind motor up
const int builtInLEDRED = 12; // LED indicating door closed (microswitch on pin 5)
const int builtInLEDGREEN = 8; // LED indicating door OPEN ( microswitch on pin 4)
const int transitledAmber = 7; // LED indicating door in TRANSIT ( neither of the microswitches active)
const byte CloseDoorManual = 3; // Close door by hand
int buttonstateclose = 0;
int currentMins;
int sunrise;
int sunset;
DateTime now;
//Are We Debugging?
const bool debug = true;
//Do we need to set/adjust RTC?
const bool setRTC = true;
//Define RTC
RTC_DS1307 rtc;
//define the location / timezone for dusk2dawn (this is yourcity, countrycode 2 digit)
// change yourcity an fill in coordinates and timezone
Dusk2Dawn Kapellen(51.31483014538207, 4.458376022992936,+1);
// the setup function runs once when you press reset or power the board
void setup() {
//Set the mode the pins will operate in.
pinMode(DoorUpMotorPin, OUTPUT);
pinMode(doorDownMotorPin, OUTPUT);
pinMode(CloseDoorManual, INPUT);
//Set Serial for Debugging
if (debug) {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.begin();
// //Lets just set the date/time when setRTC = true.
if (setRTC)
{
//write PC date and time while uploading and when setRTC = true
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//uncomment line 71 to write date and time manually into RTC when line 38 setRTC = true. For testing sunset/sunrise
//rtc.adjust(DateTime(2020, 7, 23, 7, 9, 20));
}
}
//switches setup
pinMode(doorUpSensorPin, INPUT);
pinMode(doorDownSensorPin, INPUT);
pinMode(builtInLEDRED, OUTPUT);
pinMode(builtInLEDGREEN, OUTPUT);
pinMode(transitledAmber,OUTPUT);
Serial.println("FinishedSetup");
if (digitalRead(doorUpSensorPin) == HIGH)
{
Serial.println("Door is open");
}
if (digitalRead(doorDownSensorPin) == HIGH)
{
Serial.println("Door is closed");
}
delay(3000);
}
// the loop function runs over and over again until power down or reset
void loop() {
delay(2000);
if (debug){
Serial.println();
Serial.println("SERVO LOOP");
}
//Get the Date/Time from the RTC
now = rtc.now();
//Get Sunrise/Sunset for the current year/month/day as INT's that equate to minutes from midnight that he sunrise/sunset will occur (THE TRUE is passing in Daylight Savings Time!)
sunrise = Kapellen.sunrise(now.year(), now.month(), now.day(), true);
sunset = Kapellen.sunset(now.year(), now.month(), now.day(), true);
if (debug) {
Serial.println();
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(" - ");
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
}
buttonstateclose = digitalRead(CloseDoorManual);
if(buttonstateclose == HIGH){
ManualClose();
}
//Lets get add the "now" Minutes and "now" hours*60 to see how many minutes from midnight we are
currentMins = ((now.hour()) * 60) + (now.minute());
Serial.println(currentMins);
//lets start comparisons, if the door should be up....
//delay of 30 minutes after sunset time to make sure all chickens are inside before closing the door.
if ((sunrise - 45 < currentMins && currentMins < sunset + 20) && ( buttonstateclose != HIGH))
{
Serial.println("Door state -- must be up");
while ( digitalRead(doorUpSensorPin) == LOW )
raiseDoor();
}
else
{
Serial.println("Door state -- must be down");
while ( digitalRead(doorDownSensorPin) == LOW )
lowerDoor();
}
int DoorUp = digitalRead(doorUpSensorPin);
int DoorDown = digitalRead(doorDownSensorPin);
/* This is just temporary switch debug code*/
if (DoorUp == HIGH)
{
Serial.println("Door Full OPEN");
digitalWrite(builtInLEDRED, LOW);
digitalWrite(builtInLEDGREEN, HIGH);
digitalWrite(transitledAmber, LOW);
stopDoor();
}
if (DoorDown == HIGH)
{
Serial.println("Door Full CLOSED");
digitalWrite(builtInLEDRED, HIGH);
digitalWrite(builtInLEDGREEN, LOW);
digitalWrite(transitledAmber, LOW);
stopDoor();
}
if (DoorDown == LOW && Doorup == LOW)
{
Serial.println("Door TRANSIT");
digitalWrite(builtInLEDRED, LOW);
digitalWrite(builtInLEDGREEN, LOW);
digitalWrite(transitledAmber, HIGH);
stopDoor();
}
}
//Wind the Door Up
void raiseDoor() {
digitalWrite(DoorUpMotorPin, HIGH);
digitalWrite(doorDownMotorPin, LOW);
if (debug) {
Serial.println("Door moving UP");
stopDoor();
}
}
//Wind The Door Down
void lowerDoor() {
digitalWrite(doorDownMotorPin, HIGH);
digitalWrite(DoorUpMotorPin, LOW);
if (debug) {
Serial.println("Door moving DOWN");
stopDoor();
}
}
//Stop the Door
void stopDoor() {
digitalWrite(DoorUpMotorPin, LOW);
digitalWrite(doorDownMotorPin, LOW);
if (debug) {
Serial.println("Door Stop");
}
}
//Close door manual
void ManualClose() {
digitalWrite(doorDownMotorPin, HIGH);
digitalWrite(DoorUpMotorPin, LOW);
if (debug) {
Serial.println("Door Closing Manual");
while ( digitalRead(doorDownSensorPin) == LOW)
digitalWrite(transitledAmber, HIGH);
stopDoor();
}
}
What do i need to do, to get it work???
ps, there's also a manual override switch, that allows me to CLOSE the door manually....
Thanks in advance for the tips and help.
Krgds
Kim