PWM From Variable

Hallo!

I have a problem with the PWM… all the data I get by an UDP Connection. These part is working fine, but the PWM isn’t Working… Maybe someone can help me. Here is my code:

red = strtok(value,splitValue);
green = strtok(NULL,splitValue);
blue = strtok(NULL,splitValue);
brightness = strtok(NULL,splitValue);
                        
Serial.write(red);
                        
analogWrite(redPWM, (int)red, 65535);
analogWrite(greenPWM, (int)green, 65535);
analogWrite(bluePWM_LEDDimmer, (int)blue, 65535);

Thanks for your help!

Best greetings!

Your problem is trying to cast a char* to an int. You should be using the C function, atoi to convert your chars to ints. Since the value of a char is the starting address of the char array it points to, casting it to an int will give you that address, not the numeric equivalent of your string.

2 Likes

Also, since we can’t see it, have you set pinMode(pin, OUTPUT); for your PWM pins?
And have you confirmed your chosen pins are PWM capable?

1 Like

Thx! That was the solution.