/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:
Tutorial: Arduino sequential LED project!
The purpose of this sketch is to use the Arduino board to control the lighting of LED lights,
and make them light up sequentially, one after the other, in different patterns, up to 32 patterns.
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line
Connections of the leds after placing them on the breadboard:
• Positive terminal of the first led → Pin 2 on the Arduino board.
• Negative terminal of the first led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the second led → Pin 3 on the Arduino board.
• Negative terminal of the second led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the third led → Pin 4 on the Arduino board.
• Negative terminal of the third led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the fourth led → Pin 5 on the Arduino board.
• Negative terminal of the fourth led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the fifth led → Pin 6 on the Arduino board.
• Negative terminal of the fifth led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the sixth led → Pin 7 on the Arduino board.
• Negative terminal of the sixth led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the seventh led → Pin 8 on the Arduino board.
• Negative terminal of the seventh led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the eighth led → Pin 9 on the Arduino board.
• Negative terminal of the eighth led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the ninth led → Pin 10 on the Arduino board.
• Negative terminal of the ninth led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the tenth led → Pin 11 on the Arduino board.
• Negative terminal of the tenth led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the eleventh led → Pin 12 on the Arduino board.
• Negative terminal of the eleventh led → 220-ohm resistor → Negative terminals on the breadboard.
• Positive terminal of the twelfth led → Pin 13 on the Arduino board.
• Negative terminal of the twelfth led → 220-ohm resistor → Negative terminals on the breadboard.
*/
// Define the pin numbers connected to the LEDs
const int startPin = 2; // The first pin of the LEDs
const int endPin = 13; // The last pin of the LEDs
const int numPatterns = 32; // Number of different patterns
const int delayTime = 200; //The delay time
void setup() {
// set all pins as "OUTPUT"
for (int pin = startPin; pin <= endPin; pin++) {
pinMode(pin, OUTPUT);
}
}
void loop() {
// Play different Patterns
for (int i = 0; i < numPatterns; i++) {
playPattern(i);
}
}
// A function to play different pattern
void playPattern(int pattern) {
switch (pattern) {
case 0:
//Turn on the LEDs in sequence
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
delay(delayTime);
digitalWrite(pin, LOW);
}
break;
case 1:
// Turn the lights on in reverse
for (int pin = endPin; pin >= startPin; pin--) {
digitalWrite(pin, HIGH);
delay(delayTime);
digitalWrite(pin, LOW);
}
break;
case 2:
// Turn on the lights alternately (even/odd)
for (int i = 0; i < 2; i++) {
for (int pin = startPin; pin <= endPin; pin++) {
if (pin % 2 == i) digitalWrite(pin, HIGH);
else digitalWrite(pin, LOW);
}
delay(delayTime);
}
break;
case 3:
// Turn all the lights on and off together
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
}
delay(delayTime);
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, LOW);
}
delay(delayTime);
break;
case 4:
// turn on the lights alternately (dancing lights)
for (int step = 0; step < (endPin - startPin + 1); step++) {
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, (pin - startPin + step) % 2 == 0 ? HIGH : LOW);
}
delay(delayTime);
}
break;
case 5:
// Turn the lights on from the edges to the middle
for (int i = 0; i <= (endPin - startPin) / 2; i++) {
digitalWrite(startPin + i, HIGH);
digitalWrite(endPin - i, HIGH);
delay(delayTime);
digitalWrite(startPin + i, LOW);
digitalWrite(endPin - i, LOW);
}
break;
case 6:
// Turn the lights on from the center to the edges
for (int i = 0; i <= (endPin - startPin) / 2; i++) {
digitalWrite(startPin + (endPin - startPin) / 2 - i, HIGH);
digitalWrite(startPin + (endPin - startPin) / 2 + i, HIGH);
delay(delayTime);
digitalWrite(startPin + (endPin - startPin) / 2 - i, LOW);
digitalWrite(startPin + (endPin - startPin) / 2 + i, LOW);
}
break;
case 7:
// Turn on the lights in quick succession
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
}
delay(delayTime / 4);
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, LOW);
}
delay(delayTime / 4);
break;
case 8:
// Random flash
for (int i = 0; i < 20; i++) {
int randomPin = random(startPin, endPin + 1);
digitalWrite(randomPin, HIGH);
delay(delayTime / 2);
digitalWrite(randomPin, LOW);
}
break;
case 9:
// Turn both lights on together
for (int pin = startPin; pin <= endPin - 1; pin += 2) {
digitalWrite(pin, HIGH);
digitalWrite(pin + 1, HIGH);
delay(delayTime);
digitalWrite(pin, LOW);
digitalWrite(pin + 1, LOW);
}
break;
case 10:
//turn on leds with sin() function
for (int i = 0; i < 360; i += 30) {
int pin = startPin + int((endPin - startPin) * (sin(radians(i)) + 1) / 2);
digitalWrite(pin, HIGH);
delay(delayTime / 4);
digitalWrite(pin, LOW);
}
break;
case 11:
// Turn all lights on and off randomly
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
}
delay(delayTime);
for (int pin = startPin; pin <= endPin; pin++) {
int randomPin = random(startPin, endPin + 1);
digitalWrite(randomPin, LOW);
delay(delayTime / 2);
}
break;
case 12:
// Turn the lights on gradually (Fade In) and then turn them off gradually (Fade Out)
for (int i = 0; i < 256; i += 25) {
for (int pin = startPin; pin <= endPin; pin++) {
analogWrite(pin, i); // ضبط الإضاءة بشكل تدريجي
}
delay(delayTime / 10);
}
for (int i = 255; i >= 0; i -= 25) {
for (int pin = startPin; pin <= endPin; pin++) {
analogWrite(pin, i); // خفض الإضاءة تدريجيًا
}
delay(delayTime / 10);
}
break;
case 13:
// Turn on all 3 lights together and keep moving
for (int offset = 0; offset <= (endPin - startPin - 2); offset++) {
for (int pin = startPin; pin <= endPin; pin++) {
if (pin >= startPin + offset && pin <= startPin + offset + 2) {
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
}
}
delay(delayTime);
}
break;
case 14:
// Turn on the lights in a circular pattern (Looping LEDs)
for (int step = 0; step < (endPin - startPin + 1); step++) {
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, (pin - startPin == step) ? HIGH : LOW);
}
delay(delayTime);
}
break;
case 15:
// LEDs turn on simultaneously in reverse (from edges to center and vice versa)
for (int i = 0; i <= (endPin - startPin) / 2; i++) {
digitalWrite(startPin + i, HIGH);
digitalWrite(endPin - i, HIGH);
delay(delayTime / 2);
digitalWrite(startPin + i, LOW);
digitalWrite(endPin - i, LOW);
}
break;
case 16:
//Turn on one random light at a time
for (int i = 0; i < 10; i++) {
int randomPin = random(startPin, endPin + 1);
digitalWrite(randomPin, HIGH);
delay(delayTime / 2);
digitalWrite(randomPin, LOW);
}
break;
case 17:
// Turn on the lights in fireworks style
for (int i = 0; i < 5; i++) {
int randomPin = random(startPin, endPin + 1);
for (int brightness = 0; brightness <= 255; brightness += 51) {
analogWrite(randomPin, brightness);
delay(delayTime / 10);
}
for (int brightness = 255; brightness >= 0; brightness -= 51) {
analogWrite(randomPin, brightness);
delay(delayTime / 10);
}
}
break;
case 18:
// Turns all the lights on with a quick flash
for (int i = 0; i < 10; i++) {
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
}
delay(delayTime / 10);
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, LOW);
}
delay(delayTime / 10);
}
break;
case 19:
// turn on the lights like a "heartbeat"
for (int i = 0; i < 3; i++) {
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
}
delay(delayTime / 2);
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, LOW);
}
delay(delayTime / 2);
}
delay(delayTime * 2);
break;
case 20:
// turn on leds with cos() function
for (int i = 0; i < 360; i += 30) {
int pin = startPin + int((endPin - startPin) * (cos(radians(i)) + 1) / 2);
digitalWrite(pin, HIGH);
delay(delayTime / 4);
digitalWrite(pin, LOW);
}
break;
case 21:
// Turn on the lights in sequence with the previous lights remaining lit
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
delay(delayTime);
}
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, LOW);
delay(delayTime);
}
break;
case 22:
// Turn the lights on and off simultaneously and oppositely
for (int i = 0; i <= (endPin - startPin) / 2; i++) {
digitalWrite(startPin + i, HIGH);
digitalWrite(endPin - i, HIGH);
delay(delayTime / 2);
digitalWrite(startPin + i, LOW);
digitalWrite(endPin - i, LOW);
}
break;
case 23:
// turn on the lights in double motion (both together)
for (int pin = startPin; pin <= endPin - 1; pin += 2) {
digitalWrite(pin, HIGH);
digitalWrite(pin + 1, HIGH);
delay(delayTime);
digitalWrite(pin, LOW);
digitalWrite(pin + 1, LOW);
}
break;
case 24:
// Plays a waveform that passes twice
for (int step = 0; step < (endPin - startPin + 1) * 2; step++) {
int pin = startPin + step % (endPin - startPin + 1);
digitalWrite(pin, HIGH);
delay(delayTime / 2);
digitalWrite(pin, LOW);
}
break;
case 25:
// Random flashing of all lights continuously
for (int i = 0; i < 20; i++) {
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, random(0, 2));
}
delay(delayTime / 2);
}
break;
case 26:
// Turning on the lights in a zigzag manner: Turning on the odd and then even lights
for (int pin = startPin; pin <= endPin; pin += 2) {
digitalWrite(pin, HIGH);
}
delay(delayTime);
for (int pin = startPin; pin <= endPin; pin += 2) {
digitalWrite(pin, LOW);
}
delay(delayTime);
for (int pin = startPin + 1; pin <= endPin; pin += 2) {
digitalWrite(pin, HIGH);
}
delay(delayTime);
for (int pin = startPin + 1; pin <= endPin; pin += 2) {
digitalWrite(pin, LOW);
}
break;
case 27:
//Turn on two adjacent lights sequentially
for (int pin = startPin; pin <= endPin - 1; pin++) {
digitalWrite(pin, HIGH);
digitalWrite(pin + 1, HIGH);
delay(delayTime);
digitalWrite(pin, LOW);
digitalWrite(pin + 1, LOW);
}
break;
case 28:
// “Double wave” effect from both ends to the middle
for (int i = 0; i <= (endPin - startPin) / 2; i++) {
digitalWrite(startPin + i, HIGH);
digitalWrite(endPin - i, HIGH);
delay(delayTime / 2);
digitalWrite(startPin + i, LOW);
digitalWrite(endPin - i, LOW);
}
break;
case 29:
// turn on the lights gradually and progressively (like a waterfall)
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
delay(delayTime / 2);
}
for (int pin = startPin; pin <= endPin; pin++) {
digitalWrite(pin, LOW);
delay(delayTime / 2);
}
break;
case 30:
// Turn the lights on in a completely random pattern
for (int i = 0; i < 20; i++) {
int randomPin = random(startPin, endPin + 1);
digitalWrite(randomPin, HIGH);
delay(delayTime / 4);
digitalWrite(randomPin, LOW);
}
break;
case 31:
// Turn on the lights in the form of "traffic lights"
for (int pin = startPin; pin <= startPin + 3; pin++) {
digitalWrite(pin, HIGH);
}
delay(delayTime);
for (int pin = startPin; pin <= startPin + 3; pin++) {
digitalWrite(pin, LOW);
}
for (int pin = startPin + 4; pin <= startPin + 7; pin++) {
digitalWrite(pin, HIGH);
}
delay(delayTime);
for (int pin = startPin + 4; pin <= startPin + 7; pin++) {
digitalWrite(pin, LOW);
}
for (int pin = startPin + 8; pin <= endPin; pin++) {
digitalWrite(pin, HIGH);
}
delay(delayTime);
for (int pin = startPin + 8; pin <= endPin; pin++) {
digitalWrite(pin, LOW);
}
break;
case 32:
//Turn on lights with "flashing down" effect
for (int pin = endPin; pin >= startPin; pin--) {
digitalWrite(pin, HIGH);
delay(delayTime / 2);
digitalWrite(pin, LOW);
}
break;
default:
// If no valid pattern is specified
break;
}
}