#include <Adafruit_NeoPixel.h>
// Define pins
#define PIN_NEOPIXEL 21
#define NUMPIXELS 16 // Number of NeoPixels in the ring
// Initialize NeoPixel library
Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
int distance;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize NeoPixel ring
pixels.begin();
pixels.show(); // Initialize all pixels to 'off'
// Print instructions to Serial Monitor
Serial.println("Enter distance in cm:");
}
void loop() {
// Check if there is any input from the Serial Monitor
if (Serial.available() > 0) {
// Read the input and convert it to an integer
distance = Serial.parseInt();
// Print the entered distance to the Serial Monitor
Serial.print("Entered distance: ");
Serial.print(distance);
Serial.println(" cm");
// Change NeoPixel color based on distance
if (distance < 10) {
setRingColor(pixels.Color(255, 0, 0)); // Red for close distances
} else if (distance < 20) {
setRingColor(pixels.Color(255, 255, 0)); // Yellow for medium distances
} else {
setRingColor(pixels.Color(0, 255, 0)); // Green for far distances
}
// Print instructions again for the next input
Serial.println("Enter distance in cm:");
}
delay(100); // Delay for a bit before next loop
}
// Function to set color of the NeoPixel ring
void setRingColor(uint32_t color) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, color);
}
pixels.show();
}