I am working on writing a code to control motor that will raise and lower a door on serving window between our church kitchen and multipurpose room. I am using a single momentary push button and two hall effect sensors for inputs. One hall effect sensor tell it when fully raised and one tell it when fully lowered. Outputs control three relay, raise, and lower, and power relay. Power relay need to be energized for raise and lower. I want to work just like a garage door opener works.
- If button is pushed while door is fully closed, raise door and continue to fully open.
- If button is pushed while door is fully open, lower door and continue to fully closed.
- If button is pushed while door is raising, stop door (door will be in middle of travel.
- If button is pushed while door is stopped midway, lower door.
- If button is pushed while door is lowering, reverse direction and raise door.
- Door will stop when fully raised is sensed.
- Door will stop when fully lowered is sensed.
The way I have my current code written it all works except for when door is stopped midway and the button is pushed, it should lower door. It does initially but once "doorDirection" is written, it changes to raise. What I door understand is all my if statement for controlling door are nested inside a if statement for "Buttonstate" so if button has not change it should change to raise. Delays were added in void functions as troubleshooting device. Serial print was also an attempt at debugging. Can somebody tell what I am missing here so I can fix it?
Thanks in advance for the help.
Code is below
const int upsensPin = 4; // UP SENSOR tells door is open
const int downsensPin = 5; // DOWN SENSOR tells door is down
const int buttonPin = 6; // CONTROL SWITCH
const int powerrelayPin = 9; // green power relay energize
const int uprelayPin = 10; // blue up relay energize
const int downrelayPin = 11; // orange down relay energize
int doorDirection = 0; // 0=stopped, 1=lower, 2=raise
int buttonState;
int previousButtonState;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(upsensPin, INPUT);
pinMode(downsensPin, INPUT);
pinMode(powerrelayPin, OUTPUT);
pinMode(uprelayPin, OUTPUT);
pinMode(downrelayPin, OUTPUT);
digitalWrite(uprelayPin, HIGH);
digitalWrite(powerrelayPin, HIGH);
digitalWrite(downrelayPin, HIGH);
Serial.begin(9600);
}
void raise() {
digitalWrite(downrelayPin, HIGH);
digitalWrite(uprelayPin, LOW);
digitalWrite(powerrelayPin, LOW);
delay(500);
doorDirection = 2;
}
void lower() {
digitalWrite(uprelayPin, HIGH);
digitalWrite(downrelayPin, LOW);
digitalWrite(powerrelayPin, LOW);
delay(500);
doorDirection = 1;
}
void stop() {
digitalWrite(powerrelayPin, HIGH);
digitalWrite(uprelayPin, HIGH);
digitalWrite(downrelayPin, HIGH);
delay(500);
doorDirection = 0;
}
void loop() {
Serial.print("Door direction : \t");
Serial.println(doorDirection);
Serial.print("Button State : \t");
Serial.println(buttonState);
//Serial.print("Up sensor: \t");
//Serial.println(upsens);
//Serial.print("Down sensor: \t");
//Serial.println(downsens);
//-----------------------------------------------------------------------------
// declares what buttonState is------------------------------------------------0.1
//-----------------------------------------------------------------------------
buttonState = digitalRead(buttonPin);
//-----------------------------------------------------------------------------
// if button state is different then previous button state and state is LOW---0.2
//-----------------------------------------------------------------------------
if (buttonState != previousButtonState) {
if (buttonState == LOW) {
//------------------------------------------------------------------------------
// high limit is active and button is pushed - begin lowering------------------1
//------------------------------------------------------------------------------
if (digitalRead(upsensPin) == LOW) {
lower();
}
//-----------------------------------------------------------------------------
// low limit sensor is active and button is pushed - begin raising------------2
//-----------------------------------------------------------------------------
// should add both sensor to if statement
if (digitalRead(downsensPin) == LOW) {
raise();
}
//------------------------------------------------------------------------------
// Door was stopped midway and button was pushed - lower-----------------------3 currently changes to raise should fully lower
//------------------------------------------------------------------------------
if (doorDirection == 0 && digitalRead(downsensPin) == HIGH && digitalRead(upsensPin) == HIGH) {
lower();
}
//------------------------------------------------------------------------------
// Door was raising and button was pushed - stop-------------------------------4
//------------------------------------------------------------------------------
if (doorDirection == 2 && digitalRead(downsensPin) == HIGH && digitalRead(upsensPin) == HIGH) {
stop();
}
//-------------------------------------------------------------------------------
// Door was lowering and button was pushed - raise------------------------------5 if statement cause issue with #5 if this is removed, 5 works
//-------------------------------------------------------------------------------
if (doorDirection == 1 && digitalRead(downsensPin) == HIGH && digitalRead(upsensPin) == HIGH) {
raise();
}
}
}
//-----------------------------------------------------------------------------
// low limit is reached while door was lowering - door is now fully closed---6
//-----------------------------------------------------------------------------
if (digitalRead(downsensPin) == LOW && doorDirection == 1) {
stop();
}
//-----------------------------------------------------------------------------
// high limit reached while door is raising -- door is fully open------------7
//-----------------------------------------------------------------------------
if (digitalRead(upsensPin) == LOW && doorDirection == 2) {
stop();
}
//-----------------------------------------------------------------------------
// makes previous button state what current button state is-------------------8
//-----------------------------------------------------------------------------
previousButtonState = buttonState;
}