#include <Adafruit_NeoPixel.h>
#define LED_PIN 6
#define NUM_PIXELS 50
#define NUM_SENSORS 10
#define LEDS_PER_SENSOR (NUM_PIXELS / NUM_SENSORS)
#define BUTTON_PIN 13
Adafruit_NeoPixel strip(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
// Sensor pins
const int sensorPins[NUM_SENSORS] = {2, 3, 4, 5, 7, 8, 9, 10, 11, 12};
// LED color when ON
const uint8_t activeR = 255, activeG = 255, activeB = 255; // White
// State tracking
int lastState[NUM_SENSORS];
void setup() {
Serial.begin(9600);
strip.begin();
strip.show(); // Start with all off
// Setup sensor pins
for (int i = 0; i < NUM_SENSORS; i++) {
pinMode(sensorPins[i], INPUT_PULLUP);
lastState[i] = digitalRead(sensorPins[i]);
}
// Setup button pin
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.println("IR sensors + button hold-to-turn-on ready!");
}
void loop() {
bool buttonPressed = (digitalRead(BUTTON_PIN) == LOW);
if (buttonPressed) {
// While the button is pressed, all LEDs on
setAll(activeR, activeG, activeB);
} else {
// Otherwise, LEDs respond to sensors
for (int i = 0; i < NUM_SENSORS; i++) {
int val = digitalRead(sensorPins[i]);
if (val != lastState[i]) {
int startLED = i * LEDS_PER_SENSOR;
int endLED = startLED + LEDS_PER_SENSOR;
if (val == LOW) {
// Beam broken → turn ON
setSection(startLED, endLED, activeR, activeG, activeB);
} else {
// Beam clear → turn OFF
setSection(startLED, endLED, 0, 0, 0);
}
lastState[i] = val;
}
}
}
}
// --- Turn all LEDs to one color ---
void setAll(uint8_t r, uint8_t g, uint8_t b) {
for (int j = 0; j < NUM_PIXELS; j++) {
strip.setPixelColor(j, strip.Color(r, g, b));
}
strip.show();
}
// --- Turn a section of LEDs to one color ---
void setSection(int startLED, int endLED, uint8_t r, uint8_t g, uint8_t b) {
for (int j = startLED; j < endLED; j++) {
strip.setPixelColor(j, strip.Color(r, g, b));
}
strip.show();
}