Blinking an LED
This sketch shows the simplest case of digital out.
- Connect an LED in series with a 470ohm resistor between digital pin
gOutputPin and ground.
The led is blinked on and off by setting the digital pin 1 and 0 every `gInterval seconds.
In setup() the pin mode must be set to output mode via pinMode(). For example: pinMode(context, 0, gOutputPin, OUTPUT). In render() the output of the digital pins is set by digitalWrite(). For example: digitalWrite(context, n, gOutputPin, status) where status can be equal to either 1 or 0. When set 1 the pin will give 3.3V, when set to 0 0V.
To keep track of elapsed time we have a sample counter count. When the count reaches a certain limit it switches state to either 1 or 0 depending on its current value. In this case the limit is context->digitalSampleRate * gInterval which is the desired interval expressed in samples.
int gOutputPin = 0;
float gInterval = 0.5;
int gCount = 0;
bool gStatus = false;
{
pinMode(context, 0, gOutputPin, OUTPUT);
return true;
}
{
gCount = 0;
if(gStatus == 0)
gStatus = 1;
else
gStatus = 0;
}
gCount++;
}
}
{
}
static void digitalWrite(BelaContext *context, int frame, int channel, int value)
Write a digital output, specifying the frame number (when to write) and the pin.
Definition Bela.h:1525
static void pinMode(BelaContext *context, int frame, int channel, int mode)
Set the direction of a digital pin to input or output.
Definition Bela.h:1548
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 digitalFrames
Number of digital frames per period.
Definition Bela.h:365
const float digitalSampleRate
Digital sample rate in Hz (currently always 44100.0)
Definition Bela.h:371