Hello Everyone!
First of all, apologies for my english, I am not a native speaker.
I am trying to built a parking assistant with several inputs from SR04 distance sensors that depending on a measured distance would change the color of addressable led (WS2812). Working with one sensor and LED was easy, however when I tried to extend the project I got stuck. Basically I would like to use one sensor in front of the car, and two on the right wall as my garage is quite narrow. Each sensor would be 'connected' to one of the leds, and their colour would change depending on car distance (close - red, mid range - yellow, safe distance - green).
It looks like second sensor is not lighting up second led (it doesn't light up at all). I am not sure if one Arduino is capable of doing it all so any input from you guys would be really appreciated. My intuitions so far:
- code stops when condition is met, hence second sensor data is not read as code is written in a way that it will always fulfill any of 3 conditions
- I tried to look into millis(); method and I cannot figure out how to use in that project
- I found I2C bus for SR04 sensors on the web but if possible I would like to avoid additional cost.
Please see my code below:
#include <Adafruit_NeoPixel.h>
#include <NewPing.h>
#define PIN 6
#define NUM_LEDS 2
#define MAX_DISTANCE 200
int C = 10;
int M = 25;
int ping1 = 0;
int ping2 = 0;
//create a NeoPixel strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
NewPing sonar1(12, 11, MAX_DISTANCE);
NewPing sonar2(10, 9, MAX_DISTANCE);
void setup() {
// put your setup code here, to run once:
strip.begin();
strip.show();
Serial.begin(9600);
}
void loop() {
int dist = sonar1.ping_median(5); //median off 5 values
ping1 = sonar1.convert_cm(dist);
int dist2 = sonar2.ping_median(5); //median off 5 values
ping2 = sonar2.convert_cm(dist2);
if (ping1 <= C)
{
strip.setBrightness(220);
strip.setPixelColor(0, 255, 0, 0);
strip.begin();
strip.show();
delay(10);
}
else if (ping1 > C && ping1 < M)
{
strip.setPixelColor(0, 255, 255, 0);
strip.show();
delay(10);
}
if (ping1 > M) {
strip.setPixelColor(0, 0, 255, 0);
strip.show();
delay(10);
}
else if (ping2 <= C)
{ strip.setBrightness(220);
strip.setPixelColor(1, 255, 0, 0);
strip.show();
delay(10);
}
else if (ping2 > C && ping2 < M)
{
strip.setPixelColor(1, 255, 255, 0);
strip.show();
delay(10);
}
else if (ping2 > M) {
strip.setPixelColor(1, 0, 255, 0);
strip.show();
delay(10);
}
}
Kind regards,
Tom