Skip to content

Instantly share code, notes, and snippets.

@michalgce
Last active October 5, 2017 19:59
Show Gist options
  • Save michalgce/329526bb18c663b3ecd93af229c05f89 to your computer and use it in GitHub Desktop.
Save michalgce/329526bb18c663b3ecd93af229c05f89 to your computer and use it in GitHub Desktop.
[ARDUINO] My stairs driver
#include "FastLED.h"
#define PIN 6
#define LEDS_PER_STAIR 15
#define STAIRS_NUMBER 16
#define NUM_LEDS LEDS_PER_STAIR * STAIRS_NUMBER
#define LIGHT_SENSOR_ANALOG_PIN 4
#define LIGHT_LEVLE_LIMIT 50
CRGB leds[NUM_LEDS];
int upperMotionSensor = 4;
int bottomMotionSensor = 5;
int calibrationTime = 16;
boolean bottomActivated = false;
boolean upperActivated = false;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
void lightUp(boolean moveUp, boolean dim) {
for (int stairNumber = 0; stairNumber < STAIRS_NUMBER; stairNumber++) {
for (int brightnessValue = 255; brightnessValue >= 0; brightnessValue = brightnessValue - 15) {
int dimBrightnessValue;
if (dim) {
dimBrightnessValue = 255 - brightnessValue;
} else {
dimBrightnessValue = brightnessValue;
}
if (moveUp) {
for (int ledNumberInCurrentStair = (stairNumber * LEDS_PER_STAIR);
ledNumberInCurrentStair < (stairNumber * LEDS_PER_STAIR) + 15;
ledNumberInCurrentStair++) {
leds[ledNumberInCurrentStair].red = 50;
leds[ledNumberInCurrentStair].green = 100;
leds[ledNumberInCurrentStair].blue = 150;
leds[ledNumberInCurrentStair].fadeToBlackBy(dimBrightnessValue);
}
} else {
int stairEndNumber = (STAIRS_NUMBER - stairNumber);
int lastLedPosition = (stairEndNumber * LEDS_PER_STAIR) - 1;
for (int ledNumberInCurrentStair = lastLedPosition;
ledNumberInCurrentStair > lastLedPosition - 15;
ledNumberInCurrentStair--) {
leds[ledNumberInCurrentStair].red = 50;
leds[ledNumberInCurrentStair].green = 100;
leds[ledNumberInCurrentStair].blue = 150;
leds[ledNumberInCurrentStair].fadeToBlackBy(dimBrightnessValue);
}
}
delay(10);
FastLED.show();
}
}
}
void setup() {
pinMode(bottomMotionSensor, INPUT); // declare input
pinMode(upperMotionSensor, INPUT); // declare input
FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
Serial.begin(115200);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for (int i = 0; i < calibrationTime; i++) {
lightUp(i, 255, 0, 0);
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
lightUpAll();
delay(2000);
shutdownAll();
}
void loop() {
static uint8_t hue = 0;
int16_t lightLevel = (1023 - analogRead(LIGHT_SENSOR_ANALOG_PIN)) / 10.23;
Serial.print("Light Level ");
Serial.println(lightLevel);
if (digitalRead(bottomMotionSensor) == HIGH && lightLevel < LIGHT_LEVLE_LIMIT) {
if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
lightUp(true, false);
bottomActivated = true;
Serial.println("---");
Serial.print("BOTTOM motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if (bottomActivated && digitalRead(bottomMotionSensor) == LOW) {
if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
lightUp(false, true);
bottomActivated = false;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause) / 1000);
Serial.println(" sec");
delay(50);
}
}
if (digitalRead(upperMotionSensor) == HIGH && lightLevel < LIGHT_LEVLE_LIMIT) {
if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
lightUp(false, false);
upperActivated = true;
Serial.println("---");
Serial.print("UPPER motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if (upperActivated && digitalRead(upperMotionSensor) == LOW) {
if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
lightUp(true, true);
upperActivated = false;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause) / 1000);
Serial.println(" sec");
delay(50);
}
}
}
void lightUp(int stair, int r, int g, int b) {
for (int i = 0; i < 15; i++) {
leds[i + (stair * 15)].r = r;
leds[i + (stair * 15)].g = g;
leds[i + (stair * 15)].b = b;
}
FastLED.show();
}
void lightUpAll() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].r = 0;
leds[i].g = 255;
leds[i].b = 0;
}
FastLED.show();
}
void shutdownAll() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].r = 0;
leds[i].g = 0;
leds[i].b = 0;
}
FastLED.show();
}
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment