Reading my own pwm-generated voltage

I have (hopefully) just one more problem: I generate a pwm that goes into a resistor circuit; the resistance of the circuit could change and I need to detect that. I thought of using an interrupt to test the value of the pwm I send out, but I'm not sure that works.

int pwmpin = 11;                          // inject pwm here
int testpin = 9;                  // check pwm voltage here
volatile int state = LOW, testvalue;
 
void setup() 
{ 
TCCR2B = (TCCR2B & 0xF8) | 3;            // ~1khz pwm
pinMode(testpin, INPUT);
attachInterrupt(0, test, CHANGE);
analogWrite(pwmpin, 64);           // ~25% duty cycle
Serial.begin(9600);
} 
 
void loop() 
{ 
Serial.println(testvalue);
delay(1000);
} 

void test()
{
testvalue = analogRead(testpin);
}

Why are you using 'analogRead' on the test pin? Surely it's a digital pin (if it's generating an interrupt), and so you should use 'digitalRead'?

Hi Anachrocomputer - thanks for the reply, but I need the actual value of the pwm after it hits the resistive circuit - if somebody changes the resistance that will change the pwm voltage and I have to know by how much. In fact I need both the high and low voltage parts because that tells me there is a diode in the circuit and the change in resistance is warranted.

I thought the interrupt service routine doesn't apply to any particular pin.

Right now my println always returns 0.

I tried both interrupt channels (0 and 1.)

I thought interrupts could only be triggered from digital pins? Maybe you'll need to trigger the interrupt with a digital pin, then read the analog voltage with an analog pin?

You're right - need either pin 2 or 3 and appropriate interrupt.
Tried that - interrupt service is for pin 2 and in the routine I try to read the analog pin 0. No dice.
I think this is not doable because interrupts are being soaked up by the pwm generator.