simonredfern What do I need to do so I can control the basic colour of the LED? :-)
From the page you link to:
this can be a soft-PWM rectangle wave with a frequency of samplingRate/32, whose duty cycle you can adjust to balance the brightness of the two colors, see this Pd patch for instance). Alternatively, you could use the PWM peripheral on that pin.
The referenced Pd patch is this:

You can assume an additional [>~ 0.5] before [dac~ 18], so this is a PWM which spends 25% of the time low and 75% of the time high. This is done to balance the color of the two LEDs. IIRC, the red LED was much brighter than the yellow one, so this compensates for those. As the PWM period is 32 samples, it means that it is low for 8 samples and high for 24. You would achieve something similar in C++ as follows:
void drivePwm(BelaContext* context, int pwmPin)
{
static unsigned int count = 0;
pinMode(context, 0, pwmPin, OUTPUT);
for(unsigned int n = 0; n < context->digitalFrames; ++n)
{
digitalWriteOnce(context, n, pwmPin, count > 8);
count++;
if(32 == count)
count = 0;
}
}
just call this function once per block from within render().
- set the LED pin as an INPUT: LED off
- set the LED pin as an OUTPUT, value 0: LED ON, red
- set the LED pin as an OUTPUT, value 1: LED ON, yellow
To dim/fade the LEDs, you would have to alternate states of INPUT and OUTPUT (with the desired value): the longer the pin spends in INPUT mode, the lower the brightness.
You should be able to obtain an orange LED (the mix of red and yellow), by setting the pin to an output and toggle the pin high/low at high speed. For instance, calling the following function once per block for each LED from render():
void setLed(BelaContext* context, int ledPin, int color)
{
switch(color)
{
case 0: // off
pinMode(context, 0, ledPin, INPUT);
break;
case 1: // red (I think? yellow otherwise)
case 2: // yellow (I think? red otherwise)
pinMode(context, 0, ledPin, OUTPUT);
digitalWrite(context, 0, ledPin, color - 1); // note the -1 so that it's 0 or 1
break;
case 4: // mix (orange?)
pinMode(context, 0, ledPin, OUTPUT);
// mix the two colors in equal proportion
for(unsigned int n = 0; n < context->digitalFrames; ++n)
digitalWrite(context, 0, ledPin, n & 1); // this is low on even frames and high on odd frames
break;
}
}