Thanks for your last post.
I already changed whole concept of device, due to sensor itself (hcsr04) its working , but trying to measure distance in 2 seconds time became quite hard to reach . sensor sometimes is “jumping” on distance , even when i use mapping to get more stable reading , its still doing it from time to time. idea was good to measure is object, person moving forward the sensor or away from sensor, but as i said before , maybe that will work with diffirent (laser) sensor.
I needed ASAP to be honest. so its finished im using while and checking just two values one for green light one for red light, device is already in test phase, and works well, after distance adjustment .
Basicaly its simple device that indicates is toilet room in use (red light) or its free to use (green light)
heres the code :
#include <Adafruit_NeoPixel.h>
#define PIN 4
#define NUMPIXELS 4
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 13
#define MAX_DISTANCE 180
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int range = 0; //reset range value on startup
int pocz = 9; // distance for turning on green light
int srod = 7; //distance for turning on amber light
int kon = 14 ; // distance for turning on red light
void setup()
{
Serial.begin (9600);
pixels.begin(); // This initializes the NeoPixel library.
green(); //green light to be on when device starting up
}
void loop()
{
pixels.clear();
delay(50);
int val = sonar.ping_cm();
range = map(val, 1, 160, 1, 16);
Serial.println(range);
{
while (range == kon) {
red(); //red light when person reaches max range of sensor
break;
}
while (range == srod) {
green(); // green light when person is in the middle of sensor range
break;
}
while (range == pocz) {
green(); // green light when person reaches min range of sensor
break;
}
}
}//koniec loop
void green()// green led on
{
pixels.setPixelColor(1, pixels.Color(0, 250, 0));
pixels.setPixelColor(0, pixels.Color(0, 250, 0));
pixels.setPixelColor(2, pixels.Color(0, 250, 0));
pixels.setPixelColor(3, pixels.Color(0, 250, 0));
pixels.show();
}
void red()// red led on
{
pixels.setPixelColor(0, pixels.Color(250, 0, 0));
pixels.setPixelColor(1, pixels.Color(250, 0, 0));
pixels.setPixelColor(2, pixels.Color(250, 0, 0));
pixels.setPixelColor(3, pixels.Color(250, 0, 0));
pixels.show();
}
void amber()//amber light on
{ pixels.setPixelColor(0, pixels.Color(150, 40, 0));
pixels.setPixelColor(1, pixels.Color(150, 40, 0));
pixels.setPixelColor(2, pixels.Color(150, 40, 0));
pixels.setPixelColor(3, pixels.Color(150, 40, 0));
pixels.show();
}
StefanL38, thanks , for your help and plenty of new information.
will use them as im planning to make another device to use as contactless tempreture check on entry of work place .
regards
Bartek