serialEvent as an interrupt

Howdy,
I'm trying to call an interrupt when input is entered into the Serial Monitor. From what I can find it looks like the serialEvent() is called at the beginning of each loop(). Is there a way to create a interrupt that will work anywhere in the loop instead? Thanks in advance.

Actually at the end of loop() to be pedantic

Why do you think that you need an interrupt?

Properly written loop() code loops several hundred to thousands of times per second. You should not need an interrupt to catch serial input using the available function (which is all that serialEvent does, anyway).

What I have at the moment is a loop() that reads a bunch of sensors and then spends 1 min doing stuff with that input. So I'm having to wait for 1 min for the serialEvent() to run.

Please post your full sketch
What are you doing for a whole minute ?

I'm running a PID. So as not to overheat the heating coil the PID tell the coil to run for so many sec and then is down for the rest of the min. I would like to be able to adjust some of the set point on the fly with the serial monitor.

#include <PID_v1.h>
int pin =10; 
double Output, Setpoint, Input, stepp; 
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

void setup() {
  Serial.begin(9800); 
  Input = 100; 

 //turn the PID on
  myPID.SetOutputLimits(0, 60);  
  // I'm setting the max to be 60 so that it will stay on so may second in a min.   
  myPID.SetMode(AUTOMATIC);
    pinMode(pin, OUTPUT);

  
}

void loop() {
  
  Input = digitalRead(pin); 
  myPID.Compute();
  
  delay(1000); 
}



void serialEvent() 
{
  if (Serial.available() > 2)
  {
    Serial.println("Inputs can include: ");
    Serial.println("o:Output ");
    Serial.println("p:Setpoint ");
    Serial.println("t:Print the Current Temperature: ");
    Serial.println("s:Step: ");
    Serial.println("c:Print current var: ");
   }
  else
  {
  
    char in = Serial.read();
    while (Serial.available()){Serial.read();} // Clear the buffer for next input

    switch(in)
    {
      case 'o' :
      {
        Serial.println("Please enter the new output:");
        while (Serial.available()==0){delay(100);}
        Output = Serial.parseFloat();   
        break; 
      }

      case 'p' :
      {
        Serial.println("Please enter the new setpoint:");
        while (Serial.available()==0){delay(100);}
        Setpoint = Serial.parseFloat(); 
        break; 
      }
  
      case 't' :
      {
        Serial.print("Current Temperature:");
        Serial.println(Input);
        break; 
      }

      case 's' :
     {
        Serial.println("Please enter the step to jump to:");
        while (Serial.available()==0){delay(100);}
        stepp = Serial.parseInt(); 
        break; 
      }
      

    }
  }
  while (Serial.available()){Serial.read();}  
}//------------------------------------------------------------------------------

You're playing in hazardous territory as serial is already interrupt driven to the Rx, TX buffers. Implementing another interrupt is certainly doable, but you may need to implement additional (secondary) buffering. I'm not sure this is the best use of SRAM.

Don't use delay().

See this tutorial to learn how to use millis(): https://www.baldengineer.com/blink-without-delay-explained.html

I try some of that with no luck. I might be wrong but even if I use millis() the serialEvent() will still only happen at the end of the loop() right?

Proper use of millis() takes vanishingly little time, and has no effect on other processes. Check out that tutorial!

Yes, but if millis() is used properly loop() will repeat very frequently

IMO, this really is not a good idea. Move all of the PID calc stuff and voluminous serial I/O to a separate Arduino. Move calculated PID from the second Arduino back to the first; add some additional commands if you desire, but get rid of delays() in functions called from loop().

Ok, but I'm still confused. The timing of the loop does really matter as long as it is long enough for the coil to not burn up. I could go longer but not really shorter and my problem is that if I need to interrupt it to change the inputs I can't for that 1 min. millis() would make the timing better but I still can't change anything on the fly right?

So use two Arduinos? Can you elaborate?

Save the value of millis() when you turn on the coil then keep going round loop() until the current value of millis() minus the start value equals or is greater than the required period

Do whatever you want in loop() in the meantime as long as it does not block its free running

Better to have a secondary fail-safe to ensure coil cannot burn... easily done mechanically.
https://en.wikipedia.org/wiki/Thermal_cutoff

Thank you all for the help. I appreciate the help but no one seems to be talking about interrupts. Am I to assume it is not possible to use a serialEvent as an interrupt and I need to rewrite the code to run the loop() faster?

Unless there is a LOT of code you left out, you are not using your PID at all. And your "Input" is only 0 or 1 (LOW or HIGH) since your input is from a digital pin. Any Setpoint over 1 will mean the Input will never reach the Setpoint and your Output will be pinned at whatever extreme tends toward a higher Input. You seem to ignore the Output completely.

Do you have a temperature sensor or just a temperature switch of some kind? If you have a temperature switch to sense a temperature limit, just turn off the 'coil' until the overheat switch stops reporting an overheat.

2nd Arduino does all console printing and serial interaction with You. It can report or act on any serial received from 1 st Arduino. The 2nd Arduino does all PID recalcs and sends to 1st Arduino to implement... even implement checksum or validity algorithm.

By separating the controller (1st) from the user interface and PID grinding, there is no need to be worried about delta-time associated with user interactions ... the 1st Arduino can now use a thermistor or thermocouple to deal with overheating (still think a mechanical cutoff is not a bad idea.)

The missing key is to recognize serial is already interrupted, both Tx/Rx (in the core.)