// https://wokwi.com/projects/400042875593055233
// now https://wokwi.com/projects/400052399351512065 with doorDoor
// 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);
Serial.println("\nif tangle world!\n");
pinMode(buttonPin, INPUT_PULLUP);
pinMode(upsensPin, INPUT_PULLUP); //... because switches
pinMode(downsensPin, INPUT_PULLUP); //... because switches
buttonState = digitalRead(buttonPin); // read so we don't get spurious activity
pinMode(powerrelayPin, OUTPUT);
pinMode(uprelayPin, OUTPUT);
pinMode(downrelayPin, OUTPUT);
allStop();
setupSimulation();
}
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);
}
unsigned long now; // millis for the entire not-blocked loop
void loop() {
//...
now = millis();
advanceSimulation();
report();
buttonState = digitalRead(buttonPin);
if (buttonState != previousButtonState) {
previousButtonState = buttonState;
if (buttonState == LOW) {
if (digitalRead(upsensPin) == LOW) {
lower();
// while (digitalRead(upsensPin) == LOW); // wait until door gets off switch may no longer be necessary
}
else if (digitalRead(downsensPin) == LOW) {
raise();
// while (digitalRead(downsensPin) == LOW); // wait until door gets off switch may no longer be necessary
}
else if (doorDirection == STOPPED && digitalRead(downsensPin) == HIGH && digitalRead(upsensPin) == HIGH) {
lower();
}
else if (doorDirection == RAISE && digitalRead(downsensPin) == HIGH && digitalRead(upsensPin) == HIGH) {
stop();
}
else if (doorDirection == LOWER && digitalRead(downsensPin) == HIGH && digitalRead(upsensPin) == HIGH) {
raise();
}
}
}
if (digitalRead(downsensPin) == LOW && doorDirection == LOWER) {
stop();
}
if (digitalRead(upsensPin) == LOW && doorDirection == RAISE) {
stop();
}
delay(20); // poor man's debounce - so sue me.
}
// inquiring minds want to know
void report() {
return; // we don't need no stinkin' report
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);
}
// below this line are functions to produce a simulation of the physical process
// it places the sensor output on the processOutput pin, which
// can be fed directly to the processInput pin for measurement and monitoring
// initiate the process with the goose button
// control the speed for testing with the slide fader at processSpeedControl
# include <Adafruit_NeoPixel.h>
# define PIN 7 // the pin
# define NPIXELS 16 // number of LEDs on strip
# define UPA A0 // make it go up manually
# define DOWNA A3 // make it go down manually
# define upLED A2 // on (LOW) when door is at extreme
# define downLED A1 // on (LOW) when door is at other extreme
# define PRESST LOW
Adafruit_NeoPixel doorStrip(NPIXELS, PIN, NEO_GRB + NEO_KHZ800);
static int doorPostition; // pixel number for door traveler
void setupSimulation()
{
doorStrip.begin();
doorStrip.fill(0x606060, 0, NPIXELS);
doorStrip.setPixelColor(1, 0xff00ff);
doorStrip.show();
delay(500);
doorStrip.fill(0x606060, 0, NPIXELS);
doorStrip.show();
delay(500);
pinMode(UPA, INPUT_PULLUP);
pinMode(DOWNA, INPUT_PULLUP);
pinMode(upLED, OUTPUT);
pinMode(downLED, OUTPUT);
doorPostition = 0;
doorStrip.setPixelColor(doorPostition, 0xff0000);
doorStrip.show();
}
// if the up motor is powered, move the door up
// if the down motor is powered, move the door down
# define SPEED 200 // milliseconds per step
# define ON LOW
# define OFF HIGH
void advanceSimulation()
{
static unsigned long timer;
if (timer && (now - timer < SPEED)) return;
timer = now;
// later the real outputs will hook into the buttons for automatic control
bool goUp = digitalRead(UPA) == PRESST;
bool goDown = digitalRead(DOWNA) == PRESST;
// plausible request?
if (goUp && goDown) {
Serial.println("up and down at once impossible.");
goUp= false;
goDown = false;
}
// do it one step closer
if (goUp) {
doorStrip.setPixelColor(doorPostition, 0x606060);
doorPostition++;
if (doorPostition >= NPIXELS) {
doorPostition = NPIXELS - 1; // door fully opened
}
doorStrip.setPixelColor(doorPostition, 0xff0000);
}
if (goDown) {
doorStrip.setPixelColor(doorPostition, 0x606060);
doorPostition--;
if (doorPostition < 0) {
doorPostition = 0; // door fully closed
}
doorStrip.setPixelColor(doorPostition, 0xff0000);
}
// update limit switches output
if (doorPostition == 0) {
digitalWrite(downLED, ON);
digitalWrite(upLED, OFF);
}
else if (doorPostition == NPIXELS - 1) {
digitalWrite(upLED, ON);
digitalWrite(downLED, OFF);
}
else {
digitalWrite(upLED, OFF);
digitalWrite(downLED, OFF);
}
doorStrip.show();
}
BUTTON
POWER---UP---DOWN
LIMIT SWITCHES ->
MOTOR CONTROL ->