Hi All
Excuse my naivete in this as I am very new to Arduino and the programming of. I am trying to put together a project for stair lighting triggered by a ultrasonic sensor at the bottom of the stairs and turned off by an ultrasonic sensor at the top of the stairs. I am using an WS2812 LED strip and have the lights travel up the stairs when going up and down when going down. I'm sure this is a project many of you have seen. I have searched the net and youtube and have found many examples of this project, none that I can find using the HC-SR04 sensor. I have managed to cobble together a code which works fine in one direction but am now stumped as how to get it to work the other way.
I would also like to add an LDR to the project so it is only active at night. If any of you can point me in the right direction I would be really grateful. I have attached the code so you can see where I am at.
#include <FastLED.h>
#define trigPin_up 2
#define echoPin_up 3
#define trigPin_down 4
#define echoPin_down 5
#define LED_PIN 7
#define NUM_LEDS 13
CRGB leds[NUM_LEDS];
int tOn = 400; //LED On delay
int tOff = 750; //LED Off delay
int del = 0; //Delay after action (used for debugging - set to 500-1000 to introduce a delay between each action)
void setup() {
//Set Serial
Serial.begin(9600);
Serial.println("");
Serial.println("Activated");
delay(del);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
pinMode(trigPin_up, OUTPUT);
pinMode(echoPin_up, INPUT);
pinMode(trigPin_down, OUTPUT);
pinMode(echoPin_down, INPUT);
}
void loop() {
short duration, distance;
digitalWrite(trigPin_up, LOW);
delayMicroseconds(2);
digitalWrite(trigPin_up, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin_up, LOW);
duration = pulseIn(echoPin_up, HIGH);
distance = (duration/2) / 35.1;
if (distance <= 50) {
leds[0] = CRGB(255, 225, 255);
FastLED.show();
delay(100);
leds[1] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[2] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[3] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[4] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[5] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[6] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[7] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[8] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[9] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[10] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[11] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[12] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
leds[13] = CRGB(255, 255, 255);
FastLED.show();
delay(100);
}
{ short duration, distance;
digitalWrite(trigPin_down, LOW);
delayMicroseconds(2);
digitalWrite(trigPin_down, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin_down, LOW);
duration = pulseIn(echoPin_down, HIGH);
distance = (duration/2) / 35.1;
if (distance <= 50) {
leds[0] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[1] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[2] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[3] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[4] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[5] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[6] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[7] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[8] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[9] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[10] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[11] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[12] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
leds[13] = CRGB(0, 0, 0);
FastLED.show();
delay(100);
}
}
}
This is just a test code, the final project will have around 200 LEDS.
Many thanks