Make ISR function from polling function

Ohh, got it now, thanks buddy :handshake:

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

/*In the trigger ISR you set the inital values for timer 1 ( the delay time until your pulse. 
The rest is done in a timer ISR. 
First for your initial pulse length, and than you create the pulses with OCR1 
and count them in the timer ISR. After the last pulse you switch off everything.*/

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
    OCR1A = endCompare;
  }
  else if (isrStatus == RUNNING) {
    PORTB &= ~(1 << PB2);
    OCR1A = endCompare + 20;
    isrStatus = RUNNING2;
  }
  else if (isrStatus == RUNNING2) {
    PORTB = (1 << PB2);
    OCR1A = endCompare + 40;
    isrStatus = RUNNING3;
  }
  else if (isrStatus == RUNNING3) {
    PORTB &= ~(1 << PB2);
    OCR1A = endCompare + 80;
    isrStatus = RUNNING4;
  }
  else if (isrStatus == RUNNING4) {
    PORTB = (1 << PB2);
    OCR1A = endCompare + 100;
    isrStatus = RUNNING5;
  }
  else if (isrStatus == RUNNING5) {
    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);
}

Just need to make it now with some other logic not with 5x -10x times "same" code lines :smiley: