analogRead() of value of analogWrite() value

First documentation for analogRead() seems to have disappeared with 0.6.0 updates?

Quick query - having written a value to an output pin - e.g analogWrite(pin, 150);

Is there anyway of finding the last value written? From memory analogRead() does not work like digitalRead() for a PWM output.

The problem is I want to control dimming of an TFT screen but not dim it if it is already off - hope that makes sense. Clearly, I could just maintain the last written value as a global variable - but this doesn’t seem too clever.

Why not just retain the last setting in a persistent variable?

You put it in that state, all you have to do is remember!

Has it?

But if you only want to know if the pin is off/LOW (had been set to analogWrite(pin, 0)) you should be able to digitalRead(pin) since 0 will be treated as digitalWrite(pin, LOW) and 255 as digitalWrite(pin, HIGH) - so this could work, but I’ve not tested that.
The other way would be to read out the PWM timer registers, but that’s a even less elegant than the straight forward “just remember” approach.

Thanks - the firmware/photon/#analogread-adc had definitely disappeared. It is back now.

I have gone done the “just remember” approach.

There is a HAL function actually that can provide the active PWM value on a pin: HAL_PWM_Get_AnalogValue_Ext(pin). It’s mainly used for PWM on-device tests.

1 Like

Thanks - the remember last value set is working. Good to know there is a more elegant solution.

OK now, maintaining the the pin state in a variable isn’t lipstick on a pig!


1 Like