#include <Adafruit_NeoPixel.h>
// Define pin numbers
#define LED_START_PIN 2 // Starting pin for LEDs
#define LED_END_PIN 8 // Ending pin for LEDs (inclusive)
#define NEOPIXEL_PIN 11 // Pin connected to NeoPixel ring
#define NUM_NEOPIXELS 24 // Number of LEDs in NeoPixel ring
// Initialize NeoPixel ring
Adafruit_NeoPixel ring = Adafruit_NeoPixel(NUM_NEOPIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Variables for LED chasing directions
bool chase_forward = true;
int current_led = LED_START_PIN;
// Variables for NeoPixel chasing directions
bool ring_forward = true;
int current_ring_led = 0;
void setup() {
// Set up standard LEDs (pins 2 to 8) as outputs
for (int pin = LED_START_PIN; pin <= LED_END_PIN; pin++) {
pinMode(pin, OUTPUT);
}
// Initialize NeoPixel ring
ring.begin();
ring.show(); // Initialize all pixels to 'off'
}
void loop() {
// Chase LEDs on pins 2 to 8
chase_standard_leds();
delay(100); // Adjust delay for desired speed
// Chase NeoPixel ring with rainbow colors
chase_neopixel_ring();
delay(50); // Adjust delay for desired speed
}
void chase_standard_leds() {
// Turn off all LEDs first
for (int pin = LED_START_PIN; pin <= LED_END_PIN; pin++) {
digitalWrite(pin, LOW);
}
// Turn on the current LED
digitalWrite(current_led, HIGH);
// Update LED position
if (chase_forward) {
current_led++;
if (current_led > LED_END_PIN) {
current_led = LED_END_PIN - 1; // Reverse direction
chase_forward = false;
}
} else {
current_led--;
if (current_led < LED_START_PIN) {
current_led = LED_START_PIN + 1; // Reverse direction
chase_forward = true;
}
}
}
void chase_neopixel_ring() {
// Clear all NeoPixel LEDs
ring.clear();
// Set the color for the current LED with a rainbow effect
uint32_t color = ring.ColorHSV((current_ring_led * 65536L / NUM_NEOPIXELS), 255, 255);
ring.setPixelColor(current_ring_led, color);
ring.show();
// Update NeoPixel LED position
if (ring_forward) {
current_ring_led++;
if (current_ring_led >= NUM_NEOPIXELS) {
current_ring_led = NUM_NEOPIXELS - 2; // Reverse direction
ring_forward = false;
}
} else {
current_ring_led--;
if (current_ring_led < 0) {
current_ring_led = 1; // Reverse direction
ring_forward = true;
}
}
}