Chandelier for escape room

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…

How does the problem loop manifest itself ?

    for (int i = 0; i > 10; i++)

How many times will this for loop iterate ?

More generally, how are the inputs wired ? Do you have any pulldown resistors on them ?


Here is my diagram of the connections. The loop is also currently for a troubleshooting (flashing the onboard LED to indicate status.)

Sorry, but I still do not understand how your problem manifests itself

Some observations

The for loops are wrong

When doing this

  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!!"));

ChandelierDown will already have a value of HIGH if the button was pressed earlier in loop() so you will get the message at the end of lowering the chandelier. Consider changing to detecting the buttons becoming pressed rather than being pressed

Renaming some variables will make the code easier to read. For instance I would have a single boolean variable named chandelierUp with a value of true or false as it can only be in 2 states and also making it clear that it is the state of the button that you are checking. This would mean that you could write

  if (chandelierUp == true and upButtonState == HIGH)  //already up but trying to move up

None of this may affect the logic of the sketch but will make it easier to read and understand

Hello
I guess a small FSM, using the SWITCH/CASE statement, a timer function and an I/O-handler for the LED and buttons should help.

So the original relay board I was thinking of using will not work with my application. I need a DPDT relay that can handle 120V at 5 amps. Any suggestions on how to wire this up? I am not certain how to do this. Can an arduino provide enough power through its digital output pins to trigger a standard relay?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.