Skip to content

Instantly share code, notes, and snippets.

@SchizoDuckie
Last active April 19, 2024 01:56
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save SchizoDuckie/a56f3449426279e48b1816e1be2590c1 to your computer and use it in GitHub Desktop.
Save SchizoDuckie/a56f3449426279e48b1816e1be2590c1 to your computer and use it in GitHub Desktop.
/**
* ---------------------------------------------------------
* | Esp-32/Arduino Stair Led Relay control |
* | By SchizoDuckie, ©2019 |
* | |
* | Disclaimer: |
* | I am in no way, shape or form responsible for |
* | - You setting your house on fire |
* | - Your firstborn suddenly getting autism |
* | - Your girlfriend running off with the neighbour |
* | - Your mom's failure to love you |
* | - Or any other side effect running this code may have |
* | |
* ---------------------------------------------------------
*/
/**
* These are the individual stair tred definitions.
* Meaning the relay that's connected to a specific step on the stairs is connected to this digital output pin.
* They are not used in code but you'll thank yourself for documenting them for future use.
*/
int STEP_1 = 0; // step 1 counting from the ground floor is connected to pin 0 on the ESP-32
int STEP_2 = 13; // and so on
int STEP_3 = 14;
int STEP_4 = 15;
int STEP_5 = 16;
int STEP_6 = 17;
int STEP_7 = 18;
int STEP_8 = 19;
int STEP_9 = 21;
int STEP_10 = 22;
int STEP_11 = 23;
int STEP_12 = 32;
int STEP_13 = 26;
int STEP_14 = 27;
int STEP_15 = 33;
int STEP_16 = 25;
/**
* PIR sensors are connected to digital input capable pins.
*/
int PIR_SENSOR_1 = 5;
int PIR_SENSOR_2 = 4;
/**
* This is the array of step pins that we can easily iterate over.
* Basically the values above but in a nice collection.
*/
int step_pins[16] = {0, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 32, 26, 27, 33, 25};
/**
* Predefined delay constants in milliseconds.
* TRED_DELAY is the delay before the next tred goes on or off
* WALKING_DELAY is the amount of time it takes you on average to walk up the stairs
* RESET_DELAY is the hardwired delay the PIR sensor needs before it starts looking for new movement
*/
int TRED_DELAY = 200;
int WALKING_DELAY = 8000;
int RESET_DELAY = 8000;
/**
* Initialize the code:
* - Open the serial connection (for logging to serial monitor)
* - Set the mode on all the step_pins to OUTPUT, so we can control the relay
* - Set the value to HIGH for all the relays, so they're turned OFF.
* - Wait for the PIR sensors to initialize
* - Set the pinmode on PIR sensor pins to INPUT so we can read it's values.
*/
void setup()
{
Serial.begin(19200);
for (byte i = 0; i < 16; i++) {
pinMode(step_pins[i], OUTPUT);
digitalWrite(step_pins[i], true);
}
delay(RESET_DELAY);
pinMode(PIR_SENSOR_1, INPUT);
pinMode(PIR_SENSOR_2, INPUT);
}
/**
* The loop executes the business end of the code on every clock step
* - Prints the raw sensor states for debugging
* - Checks if a PIR_SENSOR_1 was triggered
* - If so:
* - iterates over all the pins in step_pins and turns the relay connected to it ON, waiting TRED_DELAY after each switch
* - Waits for WALKING_DELAY before turning them off again
* - Iterates over all the pins in the same order and turns the relay OFF
* - Waits for RESET_DELAY to allow the PIR sensor to reset
* - Otherwise:
* - iterates over all the pins in step_pins in reverse order and turns the relay connected to it ON, waiting TRED_DELAY after each switch
* - Waits for WALKING_DELAY before turning them off again
* - Iterates over all the pins in the same reverse order and turns the relay OFF
* - Waits for RESET_DELAY to allow the PIR sensor to reset
* If nothing happened the check repeats.
*/
void loop()
{
printf("Sensor states: %d, %d\n", digitalRead(PIR_SENSOR_1), digitalRead(PIR_SENSOR_2));
if (digitalRead(PIR_SENSOR_1) == HIGH) { // the PIR sensor will put 5 volts on the signal port when it detects movement.
Serial.println("PIR_SENSOR_1 detected movement!"); // log it to the console
for (byte i = 0; i < 16; i++) { // iterate over all the relays
digitalWrite(step_pins[i], false); // and turn them on
delay(TRED_DELAY); // with TRED_DELAY pause in between.
}
delay(WALKING_DELAY); // allow time to pass moving down the stairs
for (byte j = 0; j < 16; j++) { // iterate over all the relays
digitalWrite(step_pins[j], true); // and turn them off
delay(TRED_DELAY); // with TRED_DELAY pause in between.
}
delay(RESET_DELAY); // PIR sensor won't give a new signal until RESET_DELAY ms passed.
} else if (digitalRead(PIR_SENSOR_2) == HIGH) {
Serial.println("PIR_SENSOR_2 detected movement!!");
prevState = true;
for (byte k = 0; k < 16; k++) { // iterate over all the relays in reverse order
digitalWrite(step_pins[15 - k], false);
delay(TRED_DELAY);
}
delay(WALKING_DELAY);
for (byte l = 0; l < 16; l++) {
digitalWrite(step_pins[15 - l], true);
delay(TRED_DELAY);
}
delay(RESET_DELAY);
}
}
@LukedeMunk
Copy link

LukedeMunk commented Jan 19, 2022

Nice project! Why are you using relays? Maybe you could make the "animation" smoother by using a WS2801 ledstrip and just a 5v adaptor? The advantage is that you can fade them and you can even make every rgb color. Maybe that is a nice improvement. I have written a library for the basic things, I could share it with you.

And since you are using an ESP32, if you really like programming, you can make a website to control/configure the leds.

@SchizoDuckie
Copy link
Author

SchizoDuckie commented Jan 19, 2022

Heheheh This was just V1.
The new version controls PCA9685's via PWM, but I rewrote the whole thing in javascript
https://imgur.com/gallery/LoaoaRp

@LukedeMunk
Copy link

Ah I see, I was thinking about PWM too, but I don't think that can be used with mechanical relays. It looks better now! Maybe for other projects, look at strips like the WS2801. Not that I am sponsored or something, but I think they are great for projects like this. And the only hardware they need is a power supply and, for example, an ESP.

@SchizoDuckie
Copy link
Author

Yeah I have kind of strong feelings against RGB everything. You'll only find warm and cold light in my house. But i do like the tech!

@Remus200
Copy link

Hy Schizo can you help me?
When I trai to upload the cod a have an error "preState "was not declare în this scope.
Thank you and good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment