const int colLed[] = {2, 3, 4, 5, 6, 7, 8, 9}; // Anode connection Points (columns)
const int rowLed[] = {10, 11, 12, 13, 14}; // Cathode connection points (rows) for 40 Leds
//const int rowLed[] = {10, 11, 12, 13, 14, 15}; // Cathode connection points (rows) for 48 Leds
const int numRows = sizeof(rowLed) / sizeof(rowLed[0]);
const int numCols = sizeof(colLed) / sizeof(colLed[0]);
const int numLeds = numRows * numCols; // Total number of LEDs (40 in this case)
int speed = 20; // Speed of transitions
const int potPin = A7; // Analog pin connected to the potentiometer
// Define the patterns for 40 Leds
byte patterns[][2] = {
{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0},
{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1},
{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}, {5, 2}, {6, 2}, {7, 2},
{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}, {5, 3}, {6, 3}, {7, 3},
{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}, {5, 4}, {6, 4}, {7, 4}
};
//// Define the patterns for 48 Leds
//byte patterns[][2] = {
// {0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0},
// {0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1},
// {0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}, {5, 2}, {6, 2}, {7, 2},
// {0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}, {5, 3}, {6, 3}, {7, 3},
// {0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}, {5, 4}, {6, 4}, {7, 4},
// {0, 5}, {1, 5}, {2, 5}, {3, 5}, {4, 5}, {5, 5}, {6, 5}, {7, 5}
//};
void setup() {
// Initialize column and row pins
for (int col = 0; col < numCols; col++) {
pinMode(colLed[col], OUTPUT);
}
for (int row = 0; row < numRows; row++) {
pinMode(rowLed[row], OUTPUT);
}
allOff();
}
void loop() {
test();
//patternsLoop(); // Loop through patterns continuously
//DoubleXylon();
//XylonMiddleToSides();
//XylonSidesToMiddle();
//pingPong();
//goUp();
//snake();
//diagonalWipe();
//wave();
//blinkRandom();
//twinkle();
//cascade(3);
//bouncingBall();
//vuMeter();
//vuMeter1();
//vuMeter2();
//vuMeter3();
}
void test() {
static int simulatedValue = 0; // Simulated value that increases and decreases
static int direction = 1; // Direction: 1 for up, -1 for down
// Increment or decrement the simulated value
simulatedValue += direction * 5; // Adjust speed by changing the step size (10 in this case)
// Reverse direction when it reaches the boundaries
if (simulatedValue >= 1023) {
direction = -1; // Start decreasing
} else if (simulatedValue <= 0) {
direction = 1; // Start increasing
}
int ledCount = map(simulatedValue, 0, 1023, 0, numLeds); // Map to number of LEDs (0 to numLeds)
// Light up LEDs based on the mapped value
for (int i = 0; i < numLeds; i++) {
if (i < ledCount) {
turnOn(i); // Turn on LED at index i
} else {
turnOff(i); // Turn off LED at index i
}
//delayMicroseconds(2); // Optional delay to control the speed
}
}
void patternsLoop() {
goUp();
delay(5);
goDown();
delay(5);
Xylon();
delay(5);
DoubleXylon();
delay(5);
XylonMiddleToSides();
delay(5);
XylonSidesToMiddle();
delay(5);
pingPong();
delay(5);
snake();
delay(5);
diagonalWipe();
delay(5);
wave();
delay(5);
blinkRandom();
delay(5);
twinkle();
delay(5);
cascade(3);
delay(5);
bouncingBall();
delay(5);
}
void vuMeter4() {
int potValue = analogRead(potPin); // Read the potentiometer value (0 to 1023)
int ledCount = map(potValue, 0, 1023, 0, numLeds); // Map to number of LEDs (0 to numLeds)
//allOff(); // Turn off all LEDs
// Light up LEDs based on the mapped value
for (int i = 0; i < ledCount; i++) {
turnOn(i); // Turn on LED at index i
}
for (int i = ledCount - 1; i >= 0; i--) {
turnOff(i); // Turn on LED at index i
}
delay(50); // Optional delay to slow down the updates
}
void vuMeter() {
int potValue = analogRead(potPin); // Read the potentiometer value (0 to 1023)
int ledCount = map(potValue, 0, 1023, 0, numLeds); // Map to number of LEDs (0 to numLeds)
// Light up LEDs based on the mapped value
for (int i = 0; i < numLeds; i++) {
if (i < ledCount) {
turnOn(i); // Turn on LED at index i
}
else {
turnOff(i); // Turn off LED at index i
}
}
}
void vuMeter1() {
int potValue = analogRead(potPin); // Read the potentiometer value (0 to 1023)
int ledCount = map(potValue, 0, 1023, 0, numLeds); // Map to number of LEDs (0 to numLeds)
// Light up LEDs based on the mapped value
for (int i = 0; i < numLeds; i++) {
if (i < ledCount) {
turnOn(numLeds - i - 1); // Turn on LED at index i
} else {
turnOff(numLeds - i - 1); // Turn off LED at index i
}
}
}
int previousLedCount = 0; // Variable to track the previous number of LEDs lit
void vuMeter3() {
int potValue = analogRead(potPin); // Read the potentiometer value (0 to 1023)
int ledCount = map(potValue, 0, 1023, 0, numLeds); // Map to number of LEDs (0 to numLeds)
// Update only changed LEDs
if (ledCount != previousLedCount) {
for (int i = 0; i < ledCount; i++) {
turnOn(i); // Turn on LED at index i
}
for (int i = ledCount; i < previousLedCount; i++) {
turnOff(i); // Turn off LED at index i
}
previousLedCount = ledCount; // Update the previous count to the current
}
}
void vuMeter2() {
int potValue = analogRead(potPin); // Read the potentiometer value (0 to 1023)
int ledCount = map(potValue, 0, 1023, 0, numLeds); // Map to number of LEDs (0 to numLeds)
allOff(); // Turn off all LEDs
// Light up LEDs based on the mapped value
for (int i = 0; i < ledCount; i++) {
turnOn(i); // Turn on LED at index i
}
// Optionally, keep the last LED on or implement a visual effect
// Example: turn on one more LED for a visual effect
if (ledCount < numLeds) {
turnOn(ledCount); // Optional: turn on the next LED for visual effect
}
delayMicroseconds(500); // Optional delay to slow down the updates
}
void goUp() {
for (int i = 0; i < numLeds; i++) {
turnOn(i);
delay(speed);
turnOff(i);
}
}
void goDown() {
for (int i = numLeds - 1; i >= 0; i--) {
turnOn(i);
delay(speed);
turnOff(i);
}
}
void Xylon() {
goUp();
goDown();
}
void DoubleXylon() {
for (int i = 0; i < numLeds; i++) {
turnOn(i);
delay(speed);
turnOff(i);
}
for (int i = numLeds - 1; i >= 0; i--) {
turnOn(i);
delay(speed);
turnOff(i);
}
}
void XylonMiddleToSides() {
int mid = numLeds / 2;
for (int t = 0; t < 10; t++) {
for (int i = 0; i < mid; i++) {
turnOn(mid - 1 - i);
delay(10);
turnOff(mid - 1 - i);
turnOn(mid + i);
delay(10);
turnOff(mid + i);
}
}
}
void XylonSidesToMiddle() {
int mid = numLeds / 2;
for (int t = 0; t < 10; t++) {
for (int i = 0; i < mid; i++) {
turnOn(i);
delay(10);
turnOff(i);
turnOn(numLeds - i - 1);
delay(10);
turnOff(numLeds - i - 1);
}
}
}
void pingPong() {
// Turn on LEDs from left to right
for (int i = 0; i < numLeds; i++) {
turnOn(i);
delay(speed);
}
// Turn off LEDs from right to left
for (int i = numLeds - 1; i >= 0; i--) {
turnOff(i);
delay(speed);
}
}
void snake() {
int trailLength = 3; // Adjust trail length for the "snake" effect
// Iterate over all LEDs
for (int i = 0; i < numLeds; i++) {
turnOn(i); // Turn on the current LED
// If we are past the trail length, start turning off LEDs from the start
if (i >= trailLength) {
turnOff(i - trailLength);
}
delay(speed);
}
// After the snake has finished, turn off the remaining LEDs in the trail
for (int i = numLeds - trailLength; i < numLeds; i++) {
turnOff(i);
delay(speed);
}
}
void diagonalWipe() {
int diagonalLength = min(numRows, numCols);
//int diagonalLength = random(4, 10);
for (int i = 0; i < diagonalLength; i++) {
for (int j = 0; j <= i; j++) {
int col = i - j;
int row = j;
turnOn(row * numCols + col); // Map the diagonal to the LED index
delay(speed);
}
delay(speed * 2); // Additional pause at each diagonal stage
for (int j = 0; j <= i; j++) {
int col = i - j;
int row = j;
turnOff(row * numCols + col);
}
}
}
void wave() {
const int waveLength = 5; // Length of the wave
const int waveDelay = 50; // Delay between each LED activation
const int totalSteps = numLeds + waveLength; // To ensure the wave can move off the end
// Move wave from left to right
for (int step = 0; step < totalSteps; step++) {
allOff(); // Turn off all LEDs
// Light up the current LED and the next few for the wave effect
for (int offset = 0; offset < waveLength; offset++) {
if (step - offset >= 0 && step - offset < numLeds) {
turnOn(step - offset);
}
}
delay(waveDelay); // Delay for the effect
}
// Move wave from right to left
for (int step = totalSteps - 1; step >= 0; step--) {
allOff(); // Turn off all LEDs
// Light up the current LED and the next few for the wave effect
for (int offset = 0; offset < waveLength; offset++) {
if (step + offset < totalSteps && step + offset < numLeds) {
turnOn(step + offset);
}
}
delay(waveDelay); // Delay for the effect
}
allOff(); // Ensure all LEDs are turned off at the end
}
void blinkRandom() {
int randomLed = random(0, numLeds); // Pick a random LED
turnOn(randomLed);
delay(speed);
turnOff(randomLed);
}
void twinkle() {
for (int i = 0; i < 10; i++) { // Adjust the number of twinkles
int randomLed = random(0, numLeds);
turnOn(randomLed);
delay(random(speed, speed * 3)); // Random delay for each twinkle
turnOff(randomLed);
}
}
void cascade(int blockSize) {
for (int i = 0; i < numLeds; i += blockSize) {
for (int j = 0; j < blockSize && i + j < numLeds; j++) {
turnOn(i + j);
}
delay(speed);
for (int j = 0; j < blockSize && i + j < numLeds; j++) {
turnOff(i + j);
}
}
}
void bouncingBall() {
float velocity = 1.0;
float acceleration = 1.05; // Adjust for speed change
for (int i = 0; i < numLeds; i++) {
turnOn(i);
delay((int)(speed / velocity)); // Speed decreases with each step
turnOff(i);
velocity *= acceleration; // Increase speed over time
}
// Reverse direction with the same effect
velocity = 1.0;
for (int i = numLeds - 1; i >= 0; i--) {
turnOn(i);
delay((int)(speed / velocity));
turnOff(i);
velocity *= acceleration;
}
}
void turnOn(int index) {
// Get row and column from the patterns array
int col = patterns[index][0];
int row = patterns[index][1];
allOff(); // Turn off all LEDs before turning on a specific one
digitalWrite(colLed[col], HIGH);
digitalWrite(rowLed[row], LOW); // Active-low for row pin
}
void turnOff(int index) {
int col = patterns[index][0];
int row = patterns[index][1];
digitalWrite(colLed[col], LOW);
digitalWrite(rowLed[row], HIGH); // Deactivate row pin
}
void allOff() {
for (int col = 0; col < numCols; col++) {
digitalWrite(colLed[col], LOW);
}
for (int row = 0; row < numRows; row++) {
digitalWrite(rowLed[row], HIGH);
}
}