Skip to content

Add own functionality

Aircoookie edited this page Feb 25, 2018 · 18 revisions

This page is intended for those wishing to modify the WLED code to add their own functionality.

Basics

Programming is done in the Arduino IDE. There is a special file, wled06_usermod.ino, to write your own code. (however, if you think your code may be relevant to many users, feel free to code it in directly and open a pull request)

This file has three empty methods:

  • userBeginPreConnection() is called after loading settings but before connecting to WiFi. Use it to load custom settings or to specify own server pages with the server.on() method.
  • userBegin() is the main initialization. Use it to start own interfaces (IR, UDP ports, Websockets, GPIOs,...).
  • userLoop() is called by the main loop() function.

Modify WLED values

If you know what you're doing, you may choose to change the value of any global variable declared in wled00.ino. However, for basic color and brightness changes, these are the most important:

Variable Name Type Purpose
bri byte (0-255) Master Brightness (0 is off, 255 is maximum)
col[0] byte (0-255) Red color value
col[1] byte (0-255) Green color value
col[2] byte (0-255) Blue color value
white byte (0-255) White color value

After updating the color, you must call the colorUpdated(int) method. If you want to send a notification with the new color to other ESPs, use colorUpdated(1), otherwise colorUpdated(5).

Timing

If you'd just like a simple modification that requires timing (like sending a request every 2 seconds), please never use the delay() function in your userLoop()! It will block everything else and WLED will become unresponsive and effects won't work! Instead, try this instead:

long lastTime = 0;
int delayMs = 2000; //we want to do something every 2 seconds

void userLoop()
{
  if (millis()-lastTime > delayMs)
  {
    lastTime = millis();
    //do something you want to do every 2 seconds
  }
}
Clone this wiki locally