Two interrupts at or around the same time.

I have two interrupts attached to my arduino uno. reed switch 1 to calculate RPM and reed switch 2 to calculate mph. The code works except for the part were i can’t get the Serial.print to come out right in the serial monitor. It will only read one or the other not both. any ideas on to get around it. its not for any high speed calculations only a bicycle trainer.

int sense0 = 2;
int sense1 = 3;
int rpmcounter0 = 0;
int mphcounter1 = 0;
long lastDebounce0 = 0;
long lastDebounce1 = 0;
long debounceDelay = 500;
float rpm;
float mph;






unsigned long timeold;

void setup()
{
  Serial.begin(9600);
  pinMode(sense0, INPUT);
  digitalWrite(sense0, HIGH);
  pinMode(sense1, INPUT);
  digitalWrite(sense1, HIGH);
  attachInterrupt(0, rpm_fun, RISING);
  attachInterrupt(1, mph_fun, RISING);
  rpm = 0;
  timeold = 0;


}

void loop(){
  rpms();
  mphs();


}
void rpms(){
  if (rpmcounter0 >= 1) {
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update
    rpm = ((30*1000/(millis() - timeold)*rpmcounter0)*2);
    //mph = (7.06*rpm*60/5280);
    timeold = millis();
    rpmcounter0 = 0;
    Serial.println(rpm);
  }
}
void mphs(){
  if (mphcounter1 >= 1) {
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update
    rpm = ((30*1000/(millis() - timeold)*mphcounter1)*2);
    mph = (7.06*rpm*60/5280);
    timeold = millis();
    mphcounter1 = 0;
    Serial.println(mph);
  }
}

void rpm_fun()
{
  if( (millis() - lastDebounce0) > debounceDelay){
    rpmcounter0++;
    lastDebounce0 = millis();
    //Each rotation, this interrupt function is run twice
}
}

void mph_fun()
{
  if( (millis() - lastDebounce0) > debounceDelay){
  mphcounter1++;
  lastDebounce0 = millis();
  //Each rotation, this interrupt function is run twice
}
}

You set timeold = millis() both inside the rpms() function and inside mphs() function.

Are you sure there shouldnt be one timeold for rpm and one for mph ?

I note that lastDebounce0 is used in both ISRs and lastDebounce1 is not used at all. That aside I think the ISRs may need to read a register before returning from interrupt if a simultaneous interrupt is to be acted upon. IIRC the interrupts pend but when the normal Arduino code will clear the interrupt register when the RPI from the 1’st interrupt occurs. The following indicate that perhaps the interrupt enable flag may be disabled in an ISR per normal Arduino code, and so the interrupt doesn’t pend. You’ll have to dig into details to figure out which it is and the solution.

http://www.avrfreaks.net/index.php?name … 40&start=0

http://www.avrfreaks.net/index.php?name … &start=all