Hi, I am using 2 HC-SR04 ultrasonic sensors for a stair lighting project and am having trouble with the code. I can get it to work perfect travelling one way, but cannot get it to work properly the other.
I have it so the sensor at the bottom turns the leds on going up and the top sensor turns them off from bottom to top. What I want is for the top sensor to turn the lights on going down and the bottom sensor turn them off running down.
I have tried putting an "else" statement in between the code reversed which does work but not every time.
#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];
void setup() {
Serial.begin(9600);
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) / 29.1;
if (distance <= 20) {
leds[0] = CRGB(255, 225, 255);
FastLED.show();
delay(10);
leds[1] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[2] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[3] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[4] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[5] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[6] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[7] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[8] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[9] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[10] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[11] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[12] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
leds[13] = CRGB(255, 255, 255);
FastLED.show();
delay(10);
}
{
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) / 29.1;
if (distance <= 20) {
leds[0] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[1] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[2] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[3] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[4] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[5] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[6] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[7] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[8] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[9] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[10] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[11] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[12] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
leds[13] = CRGB(0, 0, 0);
FastLED.show();
delay(30);
}
}
}
This is where I'm up to can anyone help??