I've managed to splice together a sketch where gps calculates the distance to the location I set and when distance criteria is met, the simple 5mm led turns on. The way the led is activated is with simple digitalWrite.
if (shortestDist < 2) //meters
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
However, I want to use a small neopixel strip (7 leds) that would progressively light up and change color depending on distance criteria, I would like to have 3 of them not just one.
Distance 1 = 8m
Distance 2 = 4m
Distance 3 = 2m
And the behavior of the strip could be something along the lines of
Distance 1 criteria met - first 2 leds are on
Distance 2 - 2 more leds are on
Distance 3 - all leds
And naturally if distance is increasing then they turn off by the same pattern, backwards
I tried the hack approach and I think it works but perhaps someone can assist me to clean this code up a bit?
if (shortestDist > 8)
{
pixels.clear();
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.setPixelColor(1, pixels.Color(0, 0, 0));
pixels.setPixelColor(2, pixels.Color(0, 0, 0));
pixels.setPixelColor(3, pixels.Color(0, 0, 0));
pixels.setPixelColor(4, pixels.Color(0, 0, 0));
pixels.setPixelColor(5, pixels.Color(0, 0, 0));
pixels.setPixelColor(6, pixels.Color(0, 0, 0));
pixels.show();
}
else if (shortestDist < 8)
{
pixels.clear();
pixels.setPixelColor(0, pixels.Color(0, 250, 0));
pixels.setPixelColor(1, pixels.Color(0, 250, 0));
pixels.setPixelColor(2, pixels.Color(0, 0, 0));
pixels.setPixelColor(3, pixels.Color(0, 0, 0));
pixels.setPixelColor(4, pixels.Color(0, 0, 0));
pixels.setPixelColor(5, pixels.Color(0, 0, 0));
pixels.setPixelColor(6, pixels.Color(0, 0, 0));
pixels.show();
}
else if (shortestDist < 4)
{
pixels.clear();
pixels.setPixelColor(0, pixels.Color(0, 250, 0));
pixels.setPixelColor(1, pixels.Color(0, 250, 0));
pixels.setPixelColor(2, pixels.Color(0, 250, 0));
pixels.setPixelColor(3, pixels.Color(0, 250, 0));
pixels.setPixelColor(4, pixels.Color(0, 0, 0));
pixels.setPixelColor(5, pixels.Color(0, 0, 0));
pixels.setPixelColor(6, pixels.Color(0, 0, 0));
pixels.show();
}
I didn't paste the last else if but I'm sure you get the gist of what I've done. Pretty sure it could be done better, more elegant or "clean" so to speak, anyone with the time to help me out, thank you in advance