I'm working with an esp32 pico kit, and pwm output has proven to be a difficult challenge for me. I'm hoping one of you braniac wizards will be able to shed some light on what I'm not doing right.
The end goal is to be able to have a couple different functions that can be passed a struct, and then access that structs members to output a pwm signal to the pin associated with that struct.
So far I've been able to piece together example code to get a function to read the value of a potentiometer, but I do not know how to implement ledcWrite to correctly take that potentiometer reading and then apply that value as a duty cycle to the associated ledPin.
Here is the code I have:
///////////////////////////////////////
typedef struct channel {
const int potPin;
const int ledPin;
int potVal;
}; // {potPin, ledPin, potVal};
int freq = 5000;
int ledChannel = 0;
int resolution = 12;
channel channel1 = {35, 25, 0};
// {potPin, ledPin, potVal};
void setup() {
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(channel1.ledPin, ledChannel);
}
void loop() {
readChannelInput( channel1);
writeChannelOutput( channel1);
}
//--
void readChannelInput( channel& Channel)
{
Channel.potVal = analogRead(Channel.potPin);
}
//--
void writeChannelOutput (channel& Channel)
{
ledcWrite(Channel.ledPin, Channel.potVal);
}