I am helping build an escape room for a buddy, and one thing he would like is a chandelier that raises and lowers (it will be hiding a key in it). The code I have is pretty simple, however I am having trouble with it.
#include <SoftwareSerial.h>
const int RelayDownPin = 4; // Relay 1 (Chandelier Down) In
const int RelayUpPin = 5; // Relay 2 (Chandelier Up) In
const int ButtonDownPin = 6; // Start Chandelier Down NO button
const int ButtonUpPin = 7; // Start Chandelier Up NO button
const int ledPin = 13; // onboard LED Pin that shows if the Chandelier status, Down is ON, Up is OFF
int ChandelierStatus = 0; // 0 = Up; 1 = Down
int ChandelierUp = 0;
int ChandelierDown = 0;
void setup() {
pinMode(RelayDownPin, OUTPUT);
pinMode(RelayUpPin, OUTPUT);
pinMode(ButtonDownPin, INPUT);
pinMode(ButtonUpPin, INPUT);
pinMode(ledPin, OUTPUT);
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
// read the status of Chandelier Down button
ChandelierDown = digitalRead(ButtonDownPin);
// read the status of Chandelier Up button
ChandelierUp = digitalRead(ButtonUpPin);
// check if Down button is pressed and if Chandelier is already down
if (ChandelierDown == HIGH and ChandelierStatus == 0) {
// flip variables, showing that the Chandelier is now down
ChandelierStatus = 1;
// Activate down relay for 5 seconds
digitalWrite(ledPin, HIGH);
digitalWrite(RelayDownPin, LOW);
Serial.println(F("Chandalier down start."));
delay(5000);
// Stop Chandelier down motion
digitalWrite(RelayDownPin, HIGH);
Serial.println(F("Chandalier down done."));
}
// check if Up button is pressed and if Chadelier is already up (limiter in cable will protect the ceiling)
if (ChandelierUp == HIGH and ChandelierStatus == 1) {
// flip variables, showing that the Chandelier is now up
ChandelierStatus = 0;
// Activate up relay for 5 seconds
digitalWrite(ledPin, LOW);
digitalWrite(RelayUpPin, LOW);
Serial.println(F("Chandalier up start."));
delay(5000);
// Stop Chandelier up motion
digitalWrite(RelayUpPin, HIGH);
Serial.println(F("Chandalier up done."));
}
if (ChandelierDown == HIGH and ChandelierDown == 1) {
// Chandelier is already down, do NOT activate the relay, but flash the LED as an error message.
Serial.println(F("Chandlier already down!!"));
for (int i = 0; i > 10; i++) {
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
delay(200);
}
// reset ledPIN to on to signify that the Chandelier is down
digitalWrite(ledPin, HIGH);
}
if (ChandelierUp == HIGH and ChandelierDown == 0) {
// Chandelier is already up, do NOT activate the relay, but flash the LED as an error message.
Serial.println(F("Chandalier already up!!"));
for (int i = 0; i>10; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
// reset ledPIN to off to signify that the Chandelier is up
digitalWrite(ledPin, LOW);
}
}
I put in the serial print commands ONLY to track while troubleshooting. The goal is to push a button and it activates the relay that triggers a hoist that drops the chandelier. The same button will not allow the hoist to drop further. The opposite button sends the chandelier back up but will not let it go up further.
Watching the serial output currently, it is stuck in a loop…