Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* ---------------------------------------------------------
* | 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.