Generating a single pulse without using delay and resources

Hello,

For my railroad project I would like to drive a electro magnet. How can I generate a single pulse (-+ 200 - 250 ms)?
Now I use a function who reads all the values each time to check it but it takes to much of the resources of the Arduino MEGA I think.
Delay,... isn't available in this case.
I found some topics already about this but it wasn't my thing.

regards,
minitreintje

void loop() {
  static unsigned long pulseTimer.
  if (timetodoapulse && !doingapulse) {
    doingapulse = true;
    pulseTimer = millis();
    digitalWrite(solenoidPin, HIGH);
  }
 
  if (doingapulse && millis() - pulseTimer >= 225) {
    digitalWrite(solenoidPin, LOW);
    doingapulse=false;
  }
}

Thanks! I will try it. I will post my results ...

regards,
minitreintje