//
// https://forum.arduino.cc/t/one-if-statement-overwriting-another-if-statement/1269008
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
// not used yet.
# define ENERGIZED = HIGH; // one stop shop for changining relay sense
# define OFF = LOW; // one stop shop for changining relay sense
int doorDirection = 0; // 0=stopped, 1=lower, 2=raise
enum {STOPPED, LOWER, RAISE};
char *tags[] = {"stopped", "lowering", "raising"};
int buttonState;
int previousButtonState;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(upsensPin, INPUT_PULLUP); //... because switches
pinMode(downsensPin, INPUT_PULLUP); //... because switches
pinMode(powerrelayPin, OUTPUT);
pinMode(uprelayPin, OUTPUT);
pinMode(downrelayPin, OUTPUT);
allStop();
}
void raise() {
doorGoUp();
Serial.println(" please raise.");
doorDirection = RAISE;
}
void lower() {
doorGoDown();
Serial.println(" please lower.");
doorDirection = LOWER;
}
void stop() {
allStop();
Serial.println(" please all stop.");
doorDirection = STOPPED;
}
void doorGoUp()
{
Serial.println("turn on and go UP");
digitalWrite(downrelayPin, HIGH);
digitalWrite(uprelayPin, LOW);
digitalWrite(powerrelayPin, LOW);
}
void doorGoDown()
{
Serial.println("turn on and go DOWN");
digitalWrite(uprelayPin, HIGH);
digitalWrite(downrelayPin, LOW);
digitalWrite(powerrelayPin, LOW);
}
void allStop()
{
Serial.println("turn off");
digitalWrite(powerrelayPin, HIGH);
digitalWrite(uprelayPin, HIGH);
digitalWrite(downrelayPin, HIGH);
}
void loop() {
static int lastPrintedDD = 999;
static int lastPrintedBS = 999;
if (lastPrintedDD != doorDirection) {
Serial.print("door Direction now : ");
Serial.println(tags[doorDirection]);
lastPrintedDD = doorDirection;
}
if (lastPrintedBS != buttonState) {
Serial.print("button State now : \t");
Serial.println(buttonState);
lastPrintedBS = 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();
while (digitalRead(upsensPin) == LOW); // wait until door gets off switch
}
//-----------------------------------------------------------------------------
// low limit sensor is active and button is pushed - begin raising------------2
//-----------------------------------------------------------------------------
// should add both sensor to if statement
if (digitalRead(downsensPin) == LOW) {
raise();
while (digitalRead(downsensPin) == LOW); // wait until door gets off switch
}
//------------------------------------------------------------------------------
// Door was stopped midway and button was pushed - lower-----------------------3 currently changes to raise should fully lower
//------------------------------------------------------------------------------
if (doorDirection == STOPPED && digitalRead(downsensPin) == HIGH && digitalRead(upsensPin) == HIGH) {
lower();
}
//------------------------------------------------------------------------------
// Door was raising and button was pushed - stop-------------------------------4
//------------------------------------------------------------------------------
else if (doorDirection == RAISE && 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
//-------------------------------------------------------------------------------
else if (doorDirection == LOWER && 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 == LOWER) {
stop();
}
//-----------------------------------------------------------------------------
// high limit reached while door is raising -- door is fully open------------7
//-----------------------------------------------------------------------------
if (digitalRead(upsensPin) == LOW && doorDirection == RAISE) {
stop();
}
//-----------------------------------------------------------------------------
// makes previous button state what current button state is-------------------8
//-----------------------------------------------------------------------------
previousButtonState = buttonState;
}
/* lost code area
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();
}
}
}
*/
/*
// thread link
// simulation link
void setupX()
{
Serial.begin(115200);
Serial.println("Jello Whirled!\n");
}
unsigned long now;
void loopX() {
now = millis();
}
// https://forum.arduino.cc/t/one-if-statement-overwriting-another-if-statement/1269008
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
// not used yet.
# define ENERGIZED = HIGH; // one stop shop for changining relay sense
# define OFF = LOW; // one stop shop for changining relay sense
int doorDirection = 0; // 0=stopped, 1=lower, 2=raise
enum {STOPPED, LOWER, RAISE};
char *tags[] = {"stopped", "lowering", "raising"};
int buttonState;
int previousButtonState;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(upsensPin, INPUT_PULLUP); //... because switches
pinMode(downsensPin, INPUT_PULLUP); //... because switches
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);
Serial.println(" please raise.");
doorDirection = RAISE;
}
void lower() {
digitalWrite(uprelayPin, HIGH);
digitalWrite(downrelayPin, LOW);
digitalWrite(powerrelayPin, LOW);
Serial.println(" please lower.");
doorDirection = LOWER;
}
void stop() {
digitalWrite(powerrelayPin, HIGH);
digitalWrite(uprelayPin, HIGH);
digitalWrite(downrelayPin, HIGH);
Serial.println(" please all stop.");
doorDirection = STOPPED;
}
void loop() {
static int lastPrintedDD = 999;
static int lastPrintedBS = 999;
if (lastPrintedDD != doorDirection) {
Serial.print("door Direction now : ");
Serial.println(tags[doorDirection]);
lastPrintedDD = doorDirection;
}
if (lastPrintedBS != buttonState) {
Serial.print("button State now : \t");
Serial.println(buttonState);
lastPrintedBS = 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();
while (digitalRead(upsensPin) == LOW); // wait until door gets off switch
}
//-----------------------------------------------------------------------------
// low limit sensor is active and button is pushed - begin raising------------2
//-----------------------------------------------------------------------------
// should add both sensor to if statement
if (digitalRead(downsensPin) == LOW) {
raise();
while (digitalRead(downsensPin) == LOW); // wait until door gets off switch
}
//------------------------------------------------------------------------------
// Door was stopped midway and button was pushed - lower-----------------------3 currently changes to raise should fully lower
//------------------------------------------------------------------------------
if (doorDirection == STOPPED && digitalRead(downsensPin) == HIGH && digitalRead(upsensPin) == HIGH) {
lower();
}
//------------------------------------------------------------------------------
// Door was raising and button was pushed - stop-------------------------------4
//------------------------------------------------------------------------------
else if (doorDirection == RAISE && 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
//-------------------------------------------------------------------------------
else if (doorDirection == LOWER && 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 == LOWER) {
stop();
}
//-----------------------------------------------------------------------------
// high limit reached while door is raising -- door is fully open------------7
//-----------------------------------------------------------------------------
if (digitalRead(upsensPin) == LOW && doorDirection == RAISE) {
stop();
}
//-----------------------------------------------------------------------------
// makes previous button state what current button state is-------------------8
//-----------------------------------------------------------------------------
previousButtonState = buttonState;
}
*/
//jello.
/* lost code area
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();
}
}
}
*/UPPER
(low if)
LOWER
(low if)
BUTTON
POWER---UP---DOWN