Skip to content

Instantly share code, notes, and snippets.

@pat1
Created March 18, 2020 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pat1/57326fdf15f78a2e1d5e1c30d6fc9db9 to your computer and use it in GitHub Desktop.
Save pat1/57326fdf15f78a2e1d5e1c30d6fc9db9 to your computer and use it in GitHub Desktop.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
# define pinPWM 11
# define pinTachimetrico 2
volatile int half_revolutions = 0;
int rpm = 0;
int PWM = 0;
int incrementoPWM = 5;
int rpmMIN = 2000;
int rpmMAX = 2050;
int ostruzione = 90;
//inizializzo lo schermo LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
void init_lcd() {
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0);
lcd.print("Parametri:");
}
void init_ventola() {
pinMode(pinPWM,OUTPUT);
pinMode(pinTachimetrico,INPUT);
}
void rpm_fan(){
half_revolutions++;
}
void setup() {
init_lcd();
init_ventola();
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(pinTachimetrico), rpm_fan, FALLING);
}
int read_rpm() {
half_revolutions = 0;
delay(1000);
noInterrupts();
int rpm = half_revolutions * 30; // perchè da due segnali ogni giro, se fosse solo uno *60
interrupts();
return rpm;
}
void loop(){
rpm = read_rpm();
if (rpm<rpmMIN && PWM<255) {
PWM=PWM+incrementoPWM;
analogWrite(pinPWM,PWM);
} else if (rpm>rpmMAX && PWM>0) {
PWM=PWM-incrementoPWM;
analogWrite(pinPWM,PWM);
}
Serial.print("RPM =\t");
Serial.print(rpm);
Serial.print("\t Hz=\t");
Serial.print(half_revolutions);
Serial.print("\t PWM=\t");
Serial.println(PWM);
if (PWM>=ostruzione){
Serial.println("\t Warning: Raggiunto Livello di ostruzione filtri\t");
lcd.setCursor (0,1);
lcd.print("ostruzione");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment