DYI - Programmed Christmas lights: code

  • avatar
  • 324 Views
  • 4 mins read

Now that the hardware is set up, it’s time to add some magic with a custom program. Programming the Arduino Nano is where your project truly comes to life. This step transforms your fairy lights from simple decorations into a dynamic display that reacts and behaves exactly how you design it to. By using code, you can experiment with different effects, creating a lighting system that feels personal and unique. In this article, we’ll explore a sample code that controls the lights using predefined programs, offering a starting point for further creativity.

Programmed Christmas lights project

Programmed Christmas lights: concept

Programmed Christmas lights: mounting

Programmed Christmas lights: code

Arduino code

The provided code is designed with simplicity and modularity in mind, making it easy to adapt and expand. Below is the main structure:

#define LED_PIN 3

void setup()
{
pinMode(LED_PIN, OUTPUT);
}

void loop()
{
// Program + delay
program1(LED_PIN);
delay(1000);

// Program + delay
program2(LED_PIN);
delay(1000);

// Program + delay
program3(LED_PIN);
delay(1000);
}
  • LED_PIN: defines the pin used to control the fairy lights. Here, it’s pin D3, which supports PWM output.

  • Setup function: prepares the LED_PIN as an output to ensure proper control of the lights.

  • Loop function: executes the lighting programs (program1, program2, program3) in sequence, each followed by a delay to give a clear pause between routines.

The lighting routines are kept in a separate file for better organization and flexibility. This setup makes it easy to introduce new routines or modify existing ones without altering the main structure.

Program 1

Activates the lights for 5 seconds before turning them off.

void program1(int ledPin)
{
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}

Program 2

Smoothly increases and decreases the brightness of the lights five times using PWM.

void program2(int ledPin)
{
int iterations = 0;

while (iterations < 5) {
increaseLight(ledPin);
delay(500);
decreaseLight(ledPin);

iterations++;
}
}

Program 3

Creates a blinking effect, turning the lights on and off 10 times.

void program3(int ledPin)
{
int iterations = 0;

while (iterations < 10) {
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);

iterations++;
}
}

Customizing the Code

This modular approach allows you to easily add new routines or adjust existing ones. Want to create a breathing light effect, alternating patterns, or a unique rhythm? Simply define a new function, then call it within the loop function. The possibilities are as broad as your imagination.

Testing

Before diving into customizing your code further, it’s important to test the initial setup to ensure everything works as expected. The video below demonstrates the fairy lights in action, cycling through the predefined programs smoothly.

This demonstration serves as a practical check to confirm that the wiring, power supply, and code are all functioning correctly.

Conclusion

Programming your Arduino Nano gives you the freedom to create a lighting setup that’s entirely your own. The sample code serves as a foundation, offering a starting point for crafting routines that match your vision. By experimenting with different patterns and effects, you can transform a simple project into a personalized display that brings joy and creativity to your holiday decorations.

Credits

Official GitHub: https://github.com/hibit-dev/fairy-lights

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.