Fading LEDs
This sketch uses a sine wave to drive the brightness of a series of LEDs connected to the eight analog out pins. Again you can see the nested for loop structure but this time for the analog output channels rather than the audio.
- connect an LED in series with a 470ohm resistor between each of the analogOut pins and ground.
Within the first for loop in render we cycle through each frame in the analog output channels. At each frame we then cycle through the analog output channels with another for loop and set the output voltage according to the phase of a sine tone that acts as an LFO. The analog output pins can provide a voltage of ~4.092V.
The output on each pin is set with analogWriteOnce() within the for loop that cycles through the analog output channels. This needs to be provided with arguments as follows analogWriteOnce(context, n, channel, out). Channel is where you give the address of the analog output pin (in this case we cycle through each pin address in the for loop), out is the variable that holds the desired output (in this case set by the sine wave) and n is the frame number (given by the other for loop).
Notice that the phase of the brightness cycle for each led is different. This is achieved by updating a variable that stores a relative phase value. This variable is advanced by pi/4 (1/8 of a full rotation) for each channel giving each of the eight LEDs a different phase.
 
#include <cmath>
 
const float kMinimumAmplitude = (1.5 / 5.0);
const float kAmplitudeRange = 1.0 - kMinimumAmplitude;
 
float gFrequency = 3.0;
float gPhase;
float gInverseSampleRate;
 
{
 
        
                rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
                return false;
        }
 
        gPhase = 0.0;
 
        return true;
}
 
{
                
                float relativePhase = 0.0;
                        float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
 
 
                        
                        relativePhase += (float)M_PI * 0.25f;
                }
 
                
                gPhase += 2.0f * (float)M_PI * gFrequency * gInverseSampleRate;
                if(gPhase > M_PI)
                        gPhase -= 2.0f * (float)M_PI;
        }
}
 
{
 
}
static void analogWriteOnce(BelaContext *context, int frame, int channel, float value)
Write an analog output, specifying the frame number (when to write) and the channel.
Definition Bela.h:1491
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition render.cpp:68
bool setup(BelaContext *context, void *userData)
User-defined initialisation function which runs before audio rendering begins.
Definition render.cpp:51
void cleanup(BelaContext *context, void *userData)
User-defined cleanup function which runs when the program finishes.
Definition render.cpp:96
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition Bela.h:231
const uint32_t audioFrames
The number of audio frames per block.
Definition Bela.h:322
const uint32_t analogFrames
The number of analog frames per block.
Definition Bela.h:341
const uint32_t analogOutChannels
The number of analog output channels.
Definition Bela.h:351
const float analogSampleRate
Analog sample rate in Hz.
Definition Bela.h:362