#include // Define the LED strip parameters #define NUM_LEDS 6 #define LED_PIN 13 Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); // Define the HC-SR04 parameters #define TRIGGER_PIN 9 #define ECHO_PIN 10 long duration; int distance; void setup() { // Start the serial communication Serial.begin(9600); // Initialize the LED strip strip.begin(); strip.show(); // Initialize all pixels to 'off' // Initialize the HC-SR04 pinMode(TRIGGER_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); } void loop() { // Trigger a pulse to the HC-SR04 digitalWrite(TRIGGER_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIGGER_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIGGER_PIN, LOW); // Measure the duration of the pulse duration = pulseIn(ECHO_PIN, HIGH); // Calculate the distance distance = duration * 0.034 / 2; // Print the distance to the serial monitor Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); // Update the LED strip if (distance <= 15) { for (int i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(255,0,0)); } } else { for (int i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(0, 0, 0)); } } // Show the updated LED strip strip.show(); }