Hello All.
I started project of building simple device which will indicate outside toilet is the toilet ocuupied or not with led light outside.
I decide to use hcsr04 sensor as is able to measure is someone going in or going out of toilet
for now it partly works, so when im walkin away from sensors led is changing to red , now when i will be walking out of toilet lights should change to green and stay green as long as nobody is trying to walk in.
and this point is not working , light is changing to green but when i pass the sensor, so it starts seeing wall in front which is about 210 cm away from sensor is changing it back to red.
I wish to ask for litlle advice how i can stop it from changing it to red
heres the code so far:
#include <Adafruit_NeoPixel.h>
#define PIN 4
#define NUMPIXELS 4
#include <HCSR04.h>
#define trigPin 12
#define echoPin 13
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int odleglosc = 0;
void setup() {
Serial.begin (9600);
pixels.begin(); // This initializes the NeoPixel library.
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// put your setup code here, to run once:
}
void loop() {
pixels.clear();
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
// digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
{
if ( distance>=40)
for (int i= 0; i<=distance; i++)
{
pixels.setPixelColor(1,pixels.Color(150,0,0));
pixels.setPixelColor(0,pixels.Color(150,0,0));
pixels.setPixelColor(2,pixels.Color(150,0,0));
pixels.setPixelColor(3,pixels.Color(150,0,0));
pixels.show();
}
else {
if(distance <=40)
for (int i=0; i>=distance; i--)
// {
pixels.setPixelColor(0,pixels.Color(0,150,0));
pixels.setPixelColor(1,pixels.Color(0,150,0));
pixels.setPixelColor(2,pixels.Color(0,150,0));
pixels.setPixelColor(3,pixels.Color(0,150,0));
pixels.show();
}
}
if (distance >= 210 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
thanks in advance for any help and advice.