contactless toilet ocupied indicator

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.

Sorry...
I create another code which is checking actual value of sensor with prev value of sensor , it work bit better
but still got no idea how to stop it when in reach desire state for me .
also sensor is set to 160 cm if it see further than this its returning value 0 .
i just need litlle advice how i can stop it when it reaches what i want and it will wait to do oposite way

#include <Adafruit_NeoPixel.h>
#define PIN 4
#define NUMPIXELS 4
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 13
#define MAX_DISTANCE 160
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int prev = 0;

void setup() {
Serial.begin (9600);
    pixels.begin(); // This initializes the NeoPixel library.   

}

void loop() {
 pixels.clear();
  delay(50);
//  Serial.print("Ping: ");
//  Serial.print(sonar.ping_cm());
  //set the value for distance
  int val = sonar.ping_cm();
  

    
  
  
  //Serial.println("cm");
  Serial.println(val);
  Serial.println(prev);
  
  if (val > prev && val>0)
  { 
  
  {  
    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(val<prev){

    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();
 
  }
  prev = val;
  delay(800);
}

hello!
i thought i can use switch statement, and it works great for part of it but when im using it sewcond time to change light from red to green with another switch statement , nothing happend

void loop() {
int count=0; // counting in
int countb=3;  counting out
pixels.clear();
  delay(50);
  int val = sonar.ping_cm();
  if (val>0);
 { Serial.println(val);
 }
//  Serial.println(prev);

//prev=val;
  
switch(val)
{
  case 50:
count+1;
 Serial.println("reached50");
 Serial.println(count);
 break;
  case 100:
count+1;
 Serial.println("reached100");
 Serial.println(count);
break;
  case 150:
 Serial.println("reached150");
 Serial.println("wartosccount=");
 Serial.println(count);
  count+1;
if (count==3);{
pixels.setPixelColor(0,pixels.Color(150,10,0));
    pixels.setPixelColor(1,pixels.Color(150,10,0));
    pixels.setPixelColor(2,pixels.Color(150,10,0));
    pixels.setPixelColor(3,pixels.Color(150,0,0));
    pixels.show();
Serial.println("red on");}
break;

}

switch(val)
{
  case 150:
  countb-1;
  Serial.println("jest150powrotu");
  Serial.println(countb);
  break;
  case 100:
  countb-1;
  Serial.println("jest100powrotu");
  Serial.println(countb);
  break;  
  case 50:
  countb-1;
  Serial.println("jest50powrotu");
  Serial.println(countb);
  break;
  if (countb==0);
   {pixels.setPixelColor(1,pixels.Color(0,255,0));
    pixels.setPixelColor(0,pixels.Color(0,150,0));
    pixels.setPixelColor(2,pixels.Color(0,150,0));
    pixels.setPixelColor(3,pixels.Color(0,150,0));
    pixels.show();}
  break;
   



 
 
}

}
void green()// green led on
{
  pixels.setPixelColor(1,pixels.Color(0,255,0));
    pixels.setPixelColor(0,pixels.Color(0,150,0));
    pixels.setPixelColor(2,pixels.Color(0,150,0));
    pixels.setPixelColor(3,pixels.Color(0,150,0));
    pixels.show();
}
void red()// red led on
{
 pixels.setPixelColor(0,pixels.Color(150,10,0));
    pixels.setPixelColor(1,pixels.Color(150,10,0));
    pixels.setPixelColor(2,pixels.Color(150,10,0));
    pixels.setPixelColor(3,pixels.Color(150,0,0));
    pixels.show();

count should be qualified static.count+1; does precisely nothing.

Thanks !

you are right it was doing nothing
already changed it looks like this now:

  if (x = 0);
  {
    switch (val)
    {
      case 50:
        x = 1;
        Serial.println("reached50");
        Serial.println(x);
        break;
      case 100:
        x = 2;
        Serial.println("reached100");
        Serial.println(x);
        break;
      case 150:
        x = 3;
        Serial.println("reached150");
        Serial.println("x=");
        Serial.println(x);

if (x = 0);Oops2

looks like is all wrong now completely.

why i put if(x=0); i want this part of code to work just when x=0 thats why i put in each case x+1
so this part will only starts working when x=0 then going trough all case and reach x=3.
then when x=3 it will change led to red, and i wish that x=3 works for another part to count it oposite way.
anothe if starts with if(x=3); and counting down with all case x-1 to become x=0 again and starts from begining .

looks like i made too many errors already . My apologizes but its my third day with arduino :slight_smile:

= for assignment, == for comparison.
And lose the semicolon.

Thanks my bad.

Anyway switch statement was a bad choice in this situation ...
its checking each case separately i thought it will check if all cases are true then it works.
but as you see in code i put turning on led strip in last case.

maybe you got better idea what i should use .

im just trying to acheive , that i will get red light when someone is moving away from sensor(increasing distance) and stay red as long as someone will start walking forward the sensor (decreasing distance) and then turn green light.