#include <AsyncTimer.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
static long double rpm = 20;
AsyncTimer timer;
unsigned long measure_time = millis();
int steps;
double speed_measure;
const int LM393 = 2;
int counter = 0;
int disp = 4;
int ledR = 3;
int ledG = 5;
double time = millis();
void setup() {
pinMode(disp, OUTPUT);
attachInterrupt(digitalPinToInterrupt(LM393), count, RISING);
Serial.begin(115200);
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
timer.setInterval(RPM,1000);
//timer.start();
}
void count() {
counter++;
}
void RPM() {
//speed_measure = ((steps/(millis() - measure_time))*1000*60/200);
speed_measure = (steps/((millis()-measure_time)/1000)*60)/200;
if (speed_measure == rpm){
analogWrite(ledG, 245);
analogWrite(ledR, 0);
// lcd.setCursor(0,1);
// lcd.print("GREEN");
}
else{
analogWrite(ledR, 245);
analogWrite(ledG, 0);
// lcd.setCursor(0,1);
// lcd.print("RED");
}
steps = 0;
measure_time = millis();
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("RPM ");
lcd.print(speed_measure);
counter = 0;
}
void loop() {
//lcd.print("started");
//timer.handle();
//delay(200);
//lcd.clear();
// timer.update();
speed();
display();
}
void display() {
static unsigned long int delayy = 200;
timer.handle();
if (millis() - time >= delayy){
lcd.clear();
time = millis();
}
}
void speed() {
// static long double rpm = 20;
static long double speed = ((double)300)/rpm;
if (millis() - time >= speed){
digitalWrite(disp, HIGH);
digitalWrite(disp, LOW);
steps++;
time = millis();
}
}