Reading the PWM register on Uno WiFi Rev2

I wanted to avoid a global by reading the value of a PWM register like this:

      pwmValue = 100; // global
      analogWrite(pwmSSRControl, pwmValue);

...

      // read the PWM value so it can be incremented without the global var
      int set = analogRead(pwmSSRControl);
      Serial.print("set =");
      Serial.println(set);    // Nope! Only reads 255

analogWrite works correctly.

analogRead only returns 255. Is there a way to read the correct low level register to get the previously set value on megaavr architecture?

To quote from the reference page for the analogWrite() function:

The analogWrite function has nothing to do with the analog pins or the analogRead function.

A poorly named function indeed.

Mixing a high level function like analogWrite() with low level register access to see what it has modified is a filthy thing to do. Go wash your hands.

I would just run with the global.

I was able to move the global into loop() and make it static so the value persists, with only a little refactoring. No low-level read needed and no global needed!

St3v3C:
I was able to move the global into loop() and make it static so the value persists, with only a little refactoring. No low-level read needed and no global needed!

You have done well. Many people never learn that a 'global' variable that is only used in one function should be a local static.
To answer the original question, on the AVR-based Arduinos (UNO, nano, Mega...) you can read the Output Compare Register associated with a PWM pin to get the a PWM level BUT, the register is not set if the PWM value is 0 or 255. For those two cases you will get the previous value.

analogWrite(3, 32) OCR2B==32
analogWrite(5, 32) OCR0B==32
analogWrite(6, 32) OCR0A==32
analogWrite(9, 32) OCR1A==32
analogWrite(10, 32) OCR1B==32
analogWrite(11, 32) OCR2A==32


analogWrite(3, 64) OCR2B==64
analogWrite(5, 64) OCR0B==64
analogWrite(6, 64) OCR0A==64
analogWrite(9, 64) OCR1A==64
analogWrite(10, 64) OCR1B==64
analogWrite(11, 64) OCR2A==64


analogWrite(3, 128) OCR2B==128
analogWrite(5, 128) OCR0B==128
analogWrite(6, 128) OCR0A==128
analogWrite(9, 128) OCR1A==128
analogWrite(10, 128) OCR1B==128
analogWrite(11, 128) OCR2A==128


analogWrite(3, 255) OCR2B==128
analogWrite(5, 255) OCR0B==128
analogWrite(6, 255) OCR0A==128
analogWrite(9, 255) OCR1A==128
analogWrite(10, 255) OCR1B==128
analogWrite(11, 255) OCR2A==128


analogWrite(3, 100) OCR2B==100
analogWrite(5, 100) OCR0B==100
analogWrite(6, 100) OCR0A==100
analogWrite(9, 100) OCR1A==100
analogWrite(10, 100) OCR1B==100
analogWrite(11, 100) OCR2A==100


analogWrite(3, 50) OCR2B==50
analogWrite(5, 50) OCR0B==50
analogWrite(6, 50) OCR0A==50
analogWrite(9, 50) OCR1A==50
analogWrite(10, 50) OCR1B==50
analogWrite(11, 50) OCR2A==50


analogWrite(3, 0) OCR2B==50
analogWrite(5, 0) OCR0B==50
analogWrite(6, 0) OCR0A==50
analogWrite(9, 0) OCR1A==50
analogWrite(10, 0) OCR1B==50
analogWrite(11, 0) OCR2A==50


analogWrite(3, 10) OCR2B==10
analogWrite(5, 10) OCR0B==10
analogWrite(6, 10) OCR0A==10
analogWrite(9, 10) OCR1A==10
analogWrite(10, 10) OCR1B==10
analogWrite(11, 10) OCR2A==10