#include <Adafruit_NeoPixel.h>
// Pin where the NeoPixel data line is connected
#define NEOPIXEL_PIN 6
// Pin where the button is connected
#define BUTTON_PIN 2
// Number of NeoPixels in your strip
#define NUM_PIXELS 4
// Create a NeoPixel object
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Time intervals for normal and fast speeds
const unsigned long normalInterval = 600;
const unsigned long fastInterval = 50;
const unsigned long rampUpTime = 5000; // Time in milliseconds to reach fast interval
// Variable to store the last update time
unsigned long previousMillis = 0;
unsigned long buttonPressMillis = 0;
// Variable to keep track of the current LED and fade level
int currentLED = 0;
void setup() {
// Initialize the NeoPixel strip
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Initialize the button pin as input with internal pull-up resistor
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
unsigned long currentMillis = millis();
bool buttonPressed = digitalRead(BUTTON_PIN) == LOW;
// Handle the ramp-up mechanism
if (buttonPressed) {
if (buttonPressMillis == 0) {
buttonPressMillis = currentMillis; // Start timing when button is first pressed
}
} else {
buttonPressMillis = 0; // Reset timing when button is released
}
unsigned long interval = normalInterval;
if (buttonPressMillis > 0) {
unsigned long elapsed = currentMillis - buttonPressMillis;
if (elapsed < rampUpTime) {
// Calculate the interval based on the elapsed time for a smooth ramp-up
interval = normalInterval - ((normalInterval - fastInterval) * elapsed / rampUpTime);
} else {
// Set to fast interval if ramp-up time has passed
interval = fastInterval;
}
}
// Debugging information
Serial.print("Button Pressed: ");
Serial.print(buttonPressed);
Serial.print(" | Elapsed: ");
Serial.print(currentMillis - buttonPressMillis);
Serial.print(" | Interval: ");
Serial.println(interval);
// Check if it's time to update the LED
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
updateCyclotron(interval);
}
}
void updateCyclotron(unsigned long interval) {
// Fade out the current LED over the interval
for (int i = 255; i >= 0; i -= (255 / (interval / 10))) {
strip.setPixelColor(currentLED, strip.Color(i, 0, 0));
strip.show();
delay(interval / 255 * 10); // Adjust fade speed according to the current interval
}
// Move to the next LED
currentLED = (currentLED + 1) % NUM_PIXELS;
// Turn on the next LED at full brightness
strip.setPixelColor(currentLED, strip.Color(255, 0, 0));
strip.show();
}