Someone on this topic, please guide me…
I understand that arduino mega cannot handle two interrupts the way I have set them up below;
I am trying to capture the RPM of two rotating parts and calculate a ratio to guide me with roto-molding.
Is there a way to attach and detach the interrupts to achieve this?
Presently, the code capture the first (fan_e) but ignore the second…
Help would be most welcome!
Alain (Newbe from Canada)
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 5, 9, 10, 11, 12);
volatile float time_e = 0;
volatile float time_e_last = 0;
volatile int rpm_e_array[4] = {0,0,0,0};
volatile float time_i = 0;
volatile float time_i_last = 0;
volatile int rpm_i_array[4] = {0,0,0,0};
volatile int rap = 0;
void setup()
{
//Digital Pin 2 and 21 Set As An Interrupt
attachInterrupt(0, fan_e_interrupt, FALLING);
attachInterrupt(2, fan_i_interrupt, FALLING);
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“RPM_e RPM_i Rap:”);
}
//Main Loop To Calculate RPM and Update LCD Display
void loop()
{
int rpm_e = 0;
int rpm_i = 0;
int rap = 0;
while(1){
//Slow Down The LCD Display Updates
delay(400);
//Clear The Bottom Row
lcd.setCursor(0, 1);
lcd.print(" ");
//Update The Rpm_e Count
lcd.setCursor(0, 1);
lcd.print(rpm_e);
//Update The Rpm_i Count
lcd.setCursor(6, 1);
lcd.print(rpm_i);
//Update The Rap Count
lcd.setCursor(12, 1);
lcd.print(rpm_e / (rpm_i - rpm_e));
//Update The RPM_e
if(time_e > 0)
{
//4 Sample Moving Average To Smooth Out The Data
rpm_e_array[0] = rpm_e_array[1];
rpm_e_array[1] = rpm_e_array[2];
rpm_e_array[2] = rpm_e_array[3];
rpm_e_array[3] = 60*(1000000/(time_e*2));
//Last 4 Average RPM Counts Eqauls…
rpm_e = (rpm_e_array[0] + rpm_e_array[1] + rpm_e_array[2] + rpm_e_array[3]) / 4;
}
//Update The RPM_i
if(time_i > 0)
{
//4 Sample Moving Average To Smooth Out The Data
rpm_i_array[0] = rpm_i_array[1];
rpm_i_array[1] = rpm_i_array[2];
rpm_i_array[2] = rpm_i_array[3];
rpm_i_array[3] = 60*(1000000/(time_i*2));
//Last 4 Average RPM Counts Eqauls…
rpm_i = (rpm_i_array[0] + rpm_i_array[1] + rpm_i_array[2] + rpm_i_array[3]) / 4;
}
}
}
//Capture The IR Break-Beam Interrupt
void fan_e_interrupt()
{
time_e = (micros() - time_e_last);
time_e_last = micros();
}
void fan_i_interrupt()
{
time_i = (micros() - time_i_last);
time_i_last = micros();
}