Volatile variable not changing in main loop

I have a PWM signal input on an interrupt pin on an Arduino Pro Micro.
This is my code:

#define SW_PIN 6
#define OUT_PIN 9
#define PWM_IN_PIN 7

#define PWM_MAX 799
#define PWM_MIN 0

#define PERIOD 2000 //in uS for 1kHz

volatile int pwm_value = 0;
volatile int prev_time = 0;
int interruptPin;

void setup() {
  interruptPin = digitalPinToInterrupt(PWM_IN_PIN);
  pinMode(OUT_PIN, OUTPUT);
  pinMode(SW_PIN, INPUT_PULLUP);
  pinMode (PWM_IN_PIN, INPUT);
  attachInterrupt(interruptPin, rising, RISING);

  Serial.begin(9600);

	TCCR1A = 0;
	TCCR1B = 0;

	TCCR1A = bit (COM1A1) | bit (WGM11);
	TCCR1B = bit (WGM13) | bit (WGM12) | bit (CS10);
	ICR1 = PWM_MAX; //sets to 20kHz

	OCR1A = 0; // Set this to change duty cycle
}

void rising() {
  attachInterrupt(interruptPin, falling, FALLING);
  prev_time = micros();
}
 
void falling() {
  attachInterrupt(interruptPin, rising, RISING);
  int tmp = micros()-prev_time; //HERE
  Serial.println(tmp);
  tmp = (int)((float)tmp/PERIOD*PWM_MAX);
  if(tmp < PWM_MIN || tmp > PWM_MAX){
    tmp = 0;
  }
  pwm_value = tmp;
}

void loop() {
  Serial.println(micros()-prev_time); //AND HERE
  OCR1A = pwm_value;

  analogWrite(OUT_PIN, pwm_value);

}

The volatile variable prev_time is updated by the interrupt function rising() and reading it in falling() works as expected. However, reading it in the main loop doesn't. It's not updating. Why is this?

Times should be unsigned long, aka uint32_t.
These variables cannot be accessed atomically on the eight bit AVRs and should be copied in a critical section in non-interrupt context
Don't do serial I/O in interrupt context.

why re-attach the interrupts within the interrupt routines? they don't become de-attached

why not set a bool flag in the interrupt that is monitored in loop()?

Because i have to change it between RISING and FALLING. Also i want to only catch the first positive cycle of PWM so CHANGING wouldn't work.

Thanks. How would i create a critical section?
Just with noInterrupts() and interrupts()? Would that make me miss PWM cycles?
The serial I/O was just for debugging to check that i'm not going crazy.

'CHANGE' will work just fine if your first check when entering the interrupt is the 'pinstate' If that is HIGH then it was rising and do what is appropriate and if it is LOW idem.

noInterrupts();
uint32_t temp = volatileValue;
interrupts();

after which the temp value holds the value. reference
What you are preventing is that the value changes while it is being read/processed.

Thanks for the reply.
I did what you suggested with CHANGE and it works.

I also tried the critical section but the values i'm getting are erratic
image
For the output on the left the right should be a stable 1000.

  noInterrupts();
  uint32_t tmp_time = prev_time;
  interrupts();

 
  Serial.print(pwm_value);
  Serial.print("   ");
  Serial.println(micros()-tmp_time);

I think that loop is not updating tmp_time fast enough since the value rises over time.

Also, I'm not using the critical section for the pwm_value and it's read just fine?

What i'm really trying to do here is check if the PWM went to 0 ie. more than PERIOD time has passed since last interrupt. Could i make a workaround for this?

We can't see your code

Sorry, the code i posted is in loop(). That's all i changed if that's what you're reffering to.

tmp = (int)((float)tmp/PERIOD*PWM_MAX);

A floating point calculation is going to take to long. 2 FP calculations like you do, even longer.

Post the latest version, regardless.

But that bit works just fine. And reading pwm_value works without critical section?

No. 'int' is a 16-bit value, and the MCU is 8-bit.

Sure.

#define SW_PIN 6
#define OUT_PIN 9
#define PWM_IN_PIN 7
#define TRIGGER_PIN A1

#define PWM_MAX 799
#define PWM_MIN 0

#define PERIOD 2000 //in uS for 1kHz

volatile int pwm_value = 0;
volatile uint32_t prev_time = 0;
int interruptPin;
int trigger;
int sw;

void setup() {
  interruptPin = digitalPinToInterrupt(PWM_IN_PIN);
	pinMode(OUT_PIN, OUTPUT);
  pinMode(SW_PIN, INPUT_PULLUP);
  pinMode (PWM_IN_PIN, INPUT);
  pinMode (TRIGGER_PIN, INPUT);
  attachInterrupt(interruptPin, change, CHANGE);

  Serial.begin(9600);

	TCCR1A = 0;
	TCCR1B = 0;

	TCCR1A = bit (COM1A1) | bit (WGM11);
	TCCR1B = bit (WGM13) | bit (WGM12) | bit (CS10);
	ICR1 = PWM_MAX; //sets to 20kHz

	OCR1A = 0; // Set this to change duty cycle
}

void change(){
  int state = digitalRead(PWM_IN_PIN);
  if(state){
    prev_time = micros();
  }
  else{
    int tmp = micros()-prev_time;

    tmp = (int)((float)tmp/PERIOD*PWM_MAX);
    if(tmp < PWM_MIN || tmp > PWM_MAX){
      tmp = 0;
    }
    pwm_value = tmp;
  }
}

void loop() {
  sw = digitalRead(SW_PIN);
  if(sw == 0){
    
    trigger = digitalRead(TRIGGER_PIN);
    if(trigger){
       OCR1A = pwm_value;
       analogWrite(OUT_PIN, pwm_value);
    }
    else{
      OCR1A = PWM_MAX;
      analogWrite(OUT_PIN, PWM_MAX);
    }
    
  }
  else{
    OCR1A = PWM_MIN;
    analogWrite(OUT_PIN, PWM_MIN);
  }

  noInterrupts();
  uint32_t tmp_time = prev_time;
  interrupts();

  
  Serial.print(sw);
  Serial.print("   ");
  Serial.print(pwm_value);
  Serial.print("   ");
  Serial.println(micros()-tmp_time);
  
}

I added some logic for other things in the meantime but it's not relevant

What i mean to say is, i tried it and it works. Why is that?

Sure it works, but let's try and be as efficient inside the ISR as possible changing it to

tmp = tmp * PWM_MAX / PERIOD;

will retain the accuracy and perform the calculation in 32 bit integer if you upgrade the type of 'tmp' to uin32_t (aka unsigned long)

That makes sense, thanks.
But it doesn't fix my issue.

That is unfortunate, Well i guess in that case the Serial prints may be disturbing the measurements. How about you store a few of them in an array, and print those out in one go. Also you may try a higher baud-rate.

I tried both. Stored 100 values in an array and then printed them and they're still erratic.
Also increased baudrate to 115200
Neither worked.

Well i guess in that case the Serial prints may be disturbing the measurements.

The measurement is fine since i'm getting correct values in pwm_value. But when i copy prev_time into main loop it does weird stuff. My only explanation so far is black magic. But more likely something is messing with timings.