How do i accumulate the value within the ISR

I am currently trying to accumulate the variable moved_steps but seems not to be able do so.

    ISR(TIMER1_COMPA_vect)
    {
      unsigned int new_first_step_delay;
    
      static int last_accel_delay;
        
      static unsigned int rest = 0;
      OCR1A = profile.first_step_delay;
      switch (profile.run_state)
      {
    
        case STOP:
          profile.moved_steps = 0;        
          global_state = false;
          rest = 0;
          TCCR1B &= ~((1 << CS12) | (1 << CS11) | (1 << CS10)); // Stop the timer,  No clock source
          break;
    
        case ACCEL:
          digitalWrite(step_pin, !digitalRead(step_pin));
          profile.moved_steps++;
          profile.accel_count++;
          new_first_step_delay = profile.first_step_delay - (((2 * (long)profile.first_step_delay) + rest) / (4 * profile.accel_count + 1));
          rest = ((2 * (long)profile.first_step_delay) + rest) % (4 * profile.accel_count + 1);
    
          if(profile.moved_steps >= profile.decel_start)
          {
            profile.accel_count = profile.decel_length;
            profile.run_state = DECEL;
          }
    
          else if (new_first_step_delay <= profile.min_time_delay)
          {
            last_accel_delay = new_first_step_delay;
            new_first_step_delay = profile.min_time_delay;
            rest = 0;
            profile.run_state = RUN;
          }
          break;
        case RUN:
          digitalWrite(step_pin, !digitalRead(step_pin));
          profile.moved_steps++;
          new_first_step_delay = profile.min_time_delay;
          if (profile.moved_steps >= profile.decel_start)
          {
            profile.accel_count = profile.decel_length;
            // Start decelration with same delay as accel ended with.
            new_first_step_delay = last_accel_delay;
            profile.run_state = DECEL;
          }
          break;
        case DECEL:
          digitalWrite(step_pin, !digitalRead(step_pin));
          profile.moved_steps++;
          profile.accel_count++;
          new_first_step_delay = profile.first_step_delay - (((2 * (long)profile.first_step_delay) + rest) / (4 * profile.accel_count + 1));
          rest = ((2 * (long)profile.first_step_delay) + rest) % (4 * profile.accel_count + 1);
          if (profile.accel_count >= 0)
          {
            digitalWrite(en_pin, !digitalRead(en_pin));
            profile.run_state = STOP;
          }
    
          break;
    
      }
      if (profile.dir == CCW)
      {
         profile.current_step_position =   profile.moved_steps;
      }
      else
      {
        profile.current_step_position =  profile.moved_steps;
      }  
      
      profile.first_step_delay = new_first_step_delay;
      
    }

profile.current_step_position and profile.moved_steps are the only varying variable, the other member variable of profile are constants.

The obvious way of doing this would be to

profile.current_step_position += profile.moved_steps;

but since the current_step_position is calculated within the ISR, while the moved_steps is incremented will current_step_position increment even more..

and to ensure that constant values are scaled properly is moved_steps set to 0 when run_state = stop, such that moved_steps is properly scaled according to the constants it is compared against.

Further on should the method also be capable handling run.dir which can either be 1 or 0, if 1 should moved_steps be added to the current_step_position and if run.dir = 0 should moved_steps be decremented to the current_steps_position.

An different way of doing this would be to increment current_step in the STOP state this, problem with this would be that other part of the code won't be able to process the intermediate values of the current_step_position which would be nice...

The way it has been setup now, works only for one run, through all the states of the switch case.

When you post code, delete all the commented out code. I am tired of trying to find the useful code in among all the commented out crap. If it isn't being executed, do not post it.

PaulS:
When you post code, delete all the commented out code. I am tired of trying to find the useful code in among all the commented out crap. If it isn't being executed, do not post it.

removed everythin that was commented.

you need to use volatile as part of the data type when you define profile.moved_steps

volatile int moved_steps = 0;
ISR(TIMER1_COMPA_vect) {
  moved_steps ++;
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  // initialize timer1
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;

  OCR1A = 31250;            // compare match register 16MHz/256/2Hz
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();             // enable all interrupts
}

void loop() {
  // put your main code here, to run repeatedly:
  static unsigned long _ATimer;
  int t = 100;
  if ( (unsigned long)(millis() - _ATimer) >= (t)) {
    _ATimer = millis();
    Serial.println(moved_steps);
  }
}