Hi All;
Sorry for my english not very well.
I was trying to do a project by used IR break bream to control a PC fan with PWM signal.
similar to this Basic Arduino Tachometer (IR Break-Beam) - YouTube
The system I wanna do is when the fan speed drop, then the system will increase the speed (two stage) try to maintain the speed at the speed I want.
But once the speed over Ref speed ,then the rpm value freeze unchanged no matter rpm is drop.
Is it about the interrupt function I misunderstand?
Many thanks!
code :
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
int fanOut = 3;
int RFspeed = 900;
int fanMode = 0;
int fanSpeed = 0;
void rpm_fun()
{
rpmcount++;
}
void setup() {
pinMode(fanOut, OUTPUT);
lcd.begin(16,2);
attachInterrupt(0,rpm_fun,FALLING);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop() {
if (rpm < RFspeed){
if(fanMode<1){
fanMode++;
}else{
fanMode = 0;
}
switch (fanMode) {
case 0:
fanSpeed = 150;
lcd.setCursor(1,1);
lcd.print("Mode:");
lcd.print("Normal");
delay(500);
break;
case 1:
fanSpeed = 200;
lcd.setCursor(1,1);
lcd.print("Mode:");
lcd.print("SpeedUp2");
break;
default:
fanSpeed = 150;
}
delay(1000); //update RPM every second
detachInterrupt(0);
rpm = 6.7*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
lcd.clear();
lcd.print("RPM=");
lcd.print(rpm);
analogWrite (fanOut, fanSpeed);
attachInterrupt(0,rpm_fun,FALLING);
}
}