#include <FastLED.h>
#define LED_PIN 32 // Pin connected to the LEDs
#define LED_WIDTH 14 // Number of LEDs per row
#define LED_HEIGHT 4 // Number of rows
#define COLOR_DELAY 100 // Speed of the animation (lower is faster)
// #define NUM_LEDS 56 // Total number of LEDs (14 columns x 4 rows)
#define NUM_LEDS (LED_WIDTH*LED_HEIGHT)
CRGB leds[NUM_LEDS];
// Define the 4 diagonal colors
CRGB colors[] = {CRGB::Purple, CRGB::Blue, CRGB::Aqua, CRGB::Green};
int row_spotlight = 2;
int col_spotlight = 0;
int direction = +1; // direction for column
void setup()
{
Serial.begin(115200);
Serial.println("Hello");
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
// Walk the leds to test if they are in the right order.
Serial.print("Walking leds ... ");
for(int i=0; i<NUM_LEDS; i++)
{
leds[i] = CRGB::Red;
FastLED.show();
delay(25);
leds[i] = CRGB::Black;
}
FastLED.show();
Serial.println("done");
}
void loop()
{
drawMovingPattern();
FastLED.show();
delay(COLOR_DELAY); // Control the speed of movement
col_spotlight += direction;
if(col_spotlight == (LED_WIDTH - 1) || col_spotlight == 0)
direction = -direction;
}
void drawMovingPattern()
{
// Loop through all rows and columns
for (int row = 0; row < LED_HEIGHT; row++)
{
for (int col = 0; col < LED_WIDTH; col++)
{
// Calculate the distance to the spotlight.
int delta_row = abs(row - row_spotlight);
int delta_col = abs(col - col_spotlight);
float distance = sqrt(float(delta_row*delta_row + delta_col*delta_col));
float brightness = 255.0 - 30.0*distance;
if(brightness < 0.0)
brightness = 0.0;
int r = (int) brightness;
int g = 0;
int b = 0;
setLed(row,col,CRGB(r,g,b));
}
}
}
void setLed(int _row, int _col, CRGB color)
{
int ledIndex = _row * LED_WIDTH + _col;
leds[ledIndex] = color;
}
/*
void setLed(int x, int y, CRGB color)
{
int ledIndex = (LED_HEIGHT - y - 1) * LED_WIDTH + x;
leds[ledIndex] = color;
}
*/