Hi, i’ve done a bit of research but think i’m attacking my project from the wrong angle. (yes its another arduino controlled chicken coop door closer).
Basic info; I’ve got an RTC module (DS1307) which I want to trigger a doorOpen and doorClose function at a certain time. Microswitch sensors are positioned at the extremes of the door to sense when the door has opened/closed.
For testing purposes i’ve omitted any reference to the RTC module for now and am just using the onboard Time.h function, when I get this working i’ll go back to the RTC module.
When the code runs, in the serial monitor the time ticks away, when it reaches 7.30pm it triggers the door close function.
What I cant seem to do is get this function to stop running when the doorClosed microswitch is pressed.
Is a while loop the right way to go about this?
Furthermore, once the door is closed i’m trying to change the doorState to 1 (closed) and stop all the functions with an if statement, else how would the arduino know what to do with all of its active outputs?
Below is my doorClose function, i’ve posted the whole code snippet below too for reference.
void doorClose(){
while (doorState == 0 && doorDown !=LOW)//if doorstate is open and door up sensor is triggered
{
Serial.println ("Door Closing"); //diagnostics
digitalWrite(LEDpin, HIGH); //turns LED on for diagnostics
digitalWrite(motorEnable, HIGH); //Enables the L293D
digitalWrite(motorUp, HIGH); //sets motor direction
digitalWrite(motorDown, LOW); //sets motor durection
}
if (doorDown == LOW); //if the door is down
{
doorState = 1; //set the state of the door to 1 (closed)
digitalWrite(LEDpin, LOW); //stop everything from happening
digitalWrite(motorEnable, LOW); //^
digitalWrite(motorUp, HIGH); //^
digitalWrite(motorDown, LOW); //^
}
}
#include "Time.h"
#include "TimeAlarms.h"
int motorUp = 3; //arduino pin connected to L293D pin2 (1A)
int motorDown = 4; //arduino pin conected to L293D pin7 (2A)
int motorEnable = 12; //arduino pin connected to L293D pin1 (1-2EN)
int doorUp = 1; //arduino pin connected to door up microswitch
int doorDown = 2; //arduino pin connected to door down microswitch
int LEDpin = 13; //LED pin for testing purposes
int doorState; // 0 for open, 1 for closed
//Enable needs to be hight to run
// if motorUp HIGH && motorDown LOW, clockwise
// if motorUP LOW && motorDown HIGH, anti clockwise
void setup(){
setTime(18,59,47,12,12,12); // set time to testing time (18,59,40)
Alarm.alarmRepeat(07,00,0,doorOpen); // open door 7:00am every day
Alarm.alarmRepeat(19,00,0,doorClose); // close door 7:30pm every day
Serial.begin(9600);
pinMode(doorUp, INPUT_PULLUP); //the input will be pulled LOW to trigger an action
pinMode(doorDown, INPUT_PULLUP);
pinMode (LEDpin, OUTPUT); //sets the LED pin to be output
doorState = 0; //sets the state of the door on power on to be open
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000);
}
void doorClose(){
while (doorState == 0 && doorDown !=LOW)//if doorstate is open and door up sensor is triggered
{
Serial.println ("Door Closing"); //diagnostics
digitalWrite(LEDpin, HIGH); //turns LED on for diagnostics
digitalWrite(motorEnable, HIGH); //Enables the L293D
digitalWrite(motorUp, HIGH); //sets motor direction
digitalWrite(motorDown, LOW); //sets motor durection
}
if (doorDown == LOW); //if the door is down
{
doorState = 1; //set the state of the door to 1 (closed)
digitalWrite(LEDpin, LOW); //stop everything from happening
digitalWrite(motorEnable, LOW); //^
digitalWrite(motorUp, HIGH); //^
digitalWrite(motorDown, LOW); //^
}
void doorOpen(){ //not yet coded as doorOpen doesn't work
//do stuff
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits){
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
}