
The editor of Arduino is notoriously bad when it comes to semantic syntax highlighting. Void Fader::fadeTo(float value, unsigned long duration)įloat currentValue = lerp(_startTime, _startValue, _stopTime, _stopValue, currentTime) ĬurrentValue = constrain(currentValue, _startValue, _stopValue) įloat Fader::lerp (float m1, float M1, float m2, float M2, float v1)įloat o = ( -(M2 * m1) + (m2 * m1) + m2 * d) / d For instance, the syntax Fader::fadeTo indicates that we are going to provide the body for the function fadeTo of the class Fader. It starts with #include "Fader.h", and then it provides a body for all the methods that have been defined in the header. The body of the library is where the code actually is. The remaining components are declared as private and will be used internally. The public methods that we can invoke on a Fader object are just fadeTo and getFade. Void fadeTo(float value, unsigned long duration) įloat _startValue // Start from this valueįloat lerp(float m1, float M1, float m2, float M2, float v1)

The header contains the definition of the Fader class, and it indicates which methods and attributes are available to use. Lines 4-10 are necessary if you want to use the standard Arduino functions or constants from within your code. Lines 1,2 and 14 are used to prevent this header from being included twice, and they are quite common in C++. If you are unfamiliar with C++, the header is like a summary of what the library contains. Every time we want to use our library, we need to import its header by doing so, the compiler knows which functions are available.Īlmost every Arduino library header looks like this: This folder will contain all the files we need. Within that folder, you have to create another folder with the name of your library in this example, Fader. Browse to that folder and look for libraries.

You can find your folder by checking the Sketchbook location in the Arduino Preferences window. For Windows users is usually in C:\Users\\Documents\Arduino\libraries.


Step 1. Setting upīefore start writing our source codes, we have to find the folder which Arduino uses for its libraries. The Arduino IDE comes with its own C++ compiler, so you won’t need any other additional tool for this tutorial. They will contain the header and the body of the class Fader, respectively. Since Arduino libraries are written in C++, we need to create two files: Fader.h and Fader.cpp. This tutorial explains how to create C++ libraries in Arduino.įor this example we will create a toy library called Fader. As the name suggests, it will allows us to have fading timers which we can query at any time.
