Make ISR function from polling function

Yes, im using atmega328
And yes, "Let timer 1 do the job when your trigger fires." thats the plan.

enum ScheduleStatus {OFF, PENDING, RUNNING};
ScheduleStatus isrStatus;
unsigned int startCompare; //The counter value of the timer when this will start
unsigned int endCompare;

void setup() {
  DDRB = (1 << PB2);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(0, trigger, FALLING);

  //Setup 16 bit timer1
  TCCR1A = 0x00;        //Disable Timer1 while set it up
  TCNT1 = 0;            //Reset Timer Count
  TIFR1 = 0x00;         //Timer1 INT Flag Reg: Clear Timer Overflow Flag
  TCCR1A = 0x00;        //Timer1 Control Reg A: Wave Gen Mode normal
  TCCR1B = (1 << CS12); //Timer1 Control Reg B: Timer Prescaler set to 256
}

void loop() {
}

void setSolonoidSchedule(unsigned long timeout, unsigned long duration) {
  if (isrStatus == RUNNING) {
    return;
  }
  startCompare = TCNT1 + (timeout >> 4);
  endCompare = startCompare + (duration >> 4);
  OCR1A = startCompare;  //Use the A copmare unit of timer 1
  isrStatus = PENDING;  //Turn this schedule on
  TIMSK1 |= (1 << OCIE1A);  //Turn on the A compare unit (ie turn on the interrupt)
}

ISR(TIMER1_COMPA_vect) {  //solonoid interupt service routine
  if (isrStatus == PENDING) { //Check to see if this schedule is turned on
    PORTB = (1 << PB2);
    isrStatus = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback)
    OCR1A = endCompare;
  }
  else if (isrStatus == RUNNING) {
    PORTB &= ~(1 << PB2);
    isrStatus = OFF; //Turn off the schedule
    TIMSK1 &= ~(1 << OCIE1A);      //Turn off this output compare unit (This simply writes 0 to the OCIE1A bit of TIMSK3)
  }
}

void trigger() {
  setSolonoidSchedule(10000, 2000);
}

But how to make PWM after that pulse?

And yes, i truly need to study more datasheet of the mega328 :smiley: