#include <Adafruit_NeoPixel.h>
#define NUM_PIXELS 8
#define NEOPIXEL_PIN 7 // Change this to your Neopixel pin
#define P_TRIG 3 // Trigger pin for HC-SR04
#define P_ECHO 2 // Echo pin for HC-SR04
#define PIN_SWITCH 6 // Pin switch input with pull-up resistor
Adafruit_NeoPixel neopixels = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
float distance = 0.0;
void setup() {
Serial.begin(9600);
neopixels.begin();
pinMode(P_TRIG, OUTPUT);
pinMode(P_ECHO, INPUT);
pinMode(PIN_SWITCH, INPUT_PULLUP);
}
void loop() {
readDistance();
displayParkingStatus();
delay(500);
}
void readDistance() {
digitalWrite(P_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(P_TRIG, HIGH);
delayMicroseconds(5);
digitalWrite(P_TRIG, LOW);
int latency = pulseIn(P_ECHO, HIGH);
distance = latency / 58.31;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm.");
}
void displayParkingStatus() {
int numGreen = 0;
int numYellow = 0;
int numRed = 0;
if (distance < 20.0) {
// Distance is less than 20cm, show all red and blink
for (int i = 0; i < NUM_PIXELS; i++) {
neopixels.setPixelColor(i, 255, 0, 0);
}
neopixels.show();
delay(300);
neopixels.clear();
neopixels.show();
return;
}
neopixels.clear();
if (distance < 50.0) {
neopixels.setPixelColor(7, 0, 255, 0);
neopixels.setPixelColor(6, 0, 255, 0);
neopixels.setPixelColor(5, 255, 255, 0);
neopixels.setPixelColor(4, 255, 255, 0);
neopixels.setPixelColor(3, 255, 255, 0);
neopixels.setPixelColor(2, 255, 0, 0);
neopixels.setPixelColor(1, 255, 0, 0);
neopixels.setPixelColor(0, 255, 0, 0);
} else if (distance < 80.0) {
neopixels.setPixelColor(7, 0, 255, 0);
neopixels.setPixelColor(6, 0, 255, 0);
neopixels.setPixelColor(5, 255, 255, 0);
neopixels.setPixelColor(4, 255, 255, 0);
neopixels.setPixelColor(3, 255, 255, 0);
neopixels.setPixelColor(2, 255, 0, 0);
neopixels.setPixelColor(1, 255, 0, 0);
neopixels.setPixelColor(0, 0, 0, 0);
} else if (distance < 100.0) {
neopixels.setPixelColor(7, 0, 255, 0);
neopixels.setPixelColor(6, 0, 255, 0);
neopixels.setPixelColor(5, 255, 255, 0);
neopixels.setPixelColor(4, 255, 255, 0);
neopixels.setPixelColor(3, 255, 255, 0);
neopixels.setPixelColor(2, 255, 0, 0);
neopixels.setPixelColor(1, 0, 0, 0);
neopixels.setPixelColor(0, 0, 0, 0);
} else if (distance < 120.0) {
neopixels.setPixelColor(7, 0, 255, 0);
neopixels.setPixelColor(6, 0, 255, 0);
neopixels.setPixelColor(5, 255, 255, 0);
neopixels.setPixelColor(4, 255, 255, 0);
neopixels.setPixelColor(3, 255, 255, 0);
neopixels.setPixelColor(2, 0, 0, 0);
neopixels.setPixelColor(1, 0, 0, 0);
neopixels.setPixelColor(0, 0, 0, 0);
} else if (distance < 150.0) {
neopixels.setPixelColor(7, 0, 255, 0);
neopixels.setPixelColor(6, 0, 255, 0);
neopixels.setPixelColor(5, 255, 255, 0);
neopixels.setPixelColor(4, 255, 255, 0);
neopixels.setPixelColor(3, 0, 0, 0);
neopixels.setPixelColor(2, 0, 0, 0);
neopixels.setPixelColor(1, 0, 0, 0);
neopixels.setPixelColor(0, 0, 0, 0);
} else if (distance < 180.0) {
neopixels.setPixelColor(7, 0, 255, 0);
neopixels.setPixelColor(6, 0, 255, 0);
neopixels.setPixelColor(5, 255, 255, 0);
neopixels.setPixelColor(4, 0, 0, 0);
neopixels.setPixelColor(3, 0, 0, 0);
neopixels.setPixelColor(2, 0, 0, 0);
neopixels.setPixelColor(1, 0, 0, 0);
neopixels.setPixelColor(0, 0, 0, 0);
} else if (distance < 200.0) {
neopixels.setPixelColor(7, 0, 255, 0);
neopixels.setPixelColor(6, 0, 255, 0);
neopixels.setPixelColor(5, 0, 0, 0);
neopixels.setPixelColor(4, 0, 0, 0);
neopixels.setPixelColor(3, 0, 0, 0);
neopixels.setPixelColor(2, 0, 0, 0);
neopixels.setPixelColor(1, 0, 0, 0);
neopixels.setPixelColor(0, 0, 0, 0);
} else {
neopixels.setPixelColor(7, 0, 255, 0);
neopixels.setPixelColor(6, 0, 0, 0);
neopixels.setPixelColor(5, 0, 0, 0);
neopixels.setPixelColor(4, 0, 0, 0);
neopixels.setPixelColor(3, 0, 0, 0);
neopixels.setPixelColor(2, 0, 0, 0);
neopixels.setPixelColor(1, 0, 0, 0);
neopixels.setPixelColor(0, 0, 0, 0);
}
if (digitalRead(PIN_SWITCH) == LOW) {
neopixels.clear();
neopixels.show();
}
else {
neopixels.show();
}
delay(10);
}