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
}
}