#include <Arduino.h>
#define NUM_LEDS 8
#define ANIMATION_DURATION 800 // 1 second for the movement
#define HOLD_DURATION 200 // 0.5 seconds to hold after movement
#define OFF_DURATION 200 // 0.5 seconds off before repeating
// Define the digital output pins for the LEDs
int ledPins[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8, 9};
// Define the array to store LED states at each step of the animation
int numSteps = 20; // Number of steps to approximate the bezier curve
int ledState[20][NUM_LEDS];
// Cubic Bezier function to calculate the point at a given time t
float cubicBezier(float t, float p0, float p1, float p2, float p3) {
float u = 1 - t;
float tt = t * t;
float uu = u * u;
float ttt = tt * t;
float uuu = uu * u;
// Calculate the Bezier curve's point at time `t`
float x = (uuu * p0) + (3 * uu * t * p1) + (3 * u * tt * p2) + (ttt * p3);
return x;
}
// Setup function to initialize the LEDs and create the animation array
void setup() {
// Set the LED pins as output
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Create the animation array based on the cubic-bezier function
float p0 = 0; // Start point
float p1 = 0.1; // Control point 1
float p2 = 5; // Control point 2
float p3 = 2; // End point
for (int i = 0; i < numSteps; i++) {
float t = (float)i / (numSteps - 1);
float x = cubicBezier(t, p0, p1, p2, p3) * (NUM_LEDS - 1);
int ledIndex = round(x);
for (int j = 0; j < NUM_LEDS; j++) {
ledState[i][j] = (j <= ledIndex) ? HIGH : LOW;
}
}
}
// Loop function to run the animation
void loop() {
// Run the animation for the specified duration
unsigned long startTime = millis();
unsigned long elapsed = 0;
while (elapsed < ANIMATION_DURATION) {
elapsed = millis() - startTime;
int step = map(elapsed, 0, ANIMATION_DURATION, 0, numSteps - 1);
// Set the LED states based on the current step in the animation array
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(ledPins[i], ledState[step][i]);
}
}
// Hold all LEDs for the specified duration
delay(HOLD_DURATION);
// Turn off all LEDs
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(ledPins[i], LOW);
}
// Wait for the off duration
delay(OFF_DURATION);
}