Hi there
I have a sketch to read a CPU fan RPM
Nothing fancy
volatile int rpmcount = 0;
int RPM = 0;
unsigned long lastmillis = 0;
void setup()
{
attachInterrupt (digitalPinToInterrupt(2), rpm_fan, FALLING);
}`
void loop()
{
if (millis() - lastmillis >= 1000) { /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
detachInterrupt(digitalPinToInterrupt(2)); //Disable interrupt when calculating
RPM = rpmcount * 60; /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(digitalPinToInterrupt(2), rpm_fan, FALLING); //enable interrupt
}
}
void rpm_fan() { /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount++;
// analogWrite(PWM, 124);
}
My inquiry is if I can put inside the rpm_fan() function wich is activated in each interrupt call the commented line (analog write).
Im trying to simulate a EFI fuel inyection system, and I´m currently using a PC fan to trick the RPM count and to control a fuel inyector opening in each pulse.
Is it feasible this way? Also, can I change the 124 value to a variable (I will later use a formula to calculate the ms that the PWM needs to be HIGH and will come in handy)
Thanks in advance