#include <TM1637.h>
#include<EEPROM.h>
TM1637 TM1;
TM1637 TM2;
//-------Pins-----//
//
int start_stop = 4; //Start/Stop button
int rst_sp = 5; // Reset Set Point Button
const int sensor_pulse =2; // Sensor Pulse In
//----Analog as Input-----//
int cal_2=1;
int led = 7;
int pinPulse = 3;
//-----Variables for debouncing-----//
boolean currentstart_stop = LOW;
boolean laststart_stop =LOW;
boolean lastsensor_pulse = LOW;
boolean currentsensor_pulse = LOW;
boolean lastrst_sp = LOW;
boolean currentrst_sp = LOW;
boolean lastadd_one = LOW;
boolean currentadd_one = LOW;
boolean lastadd_ten = LOW;
boolean currentadd_ten = LOW;
//-----Storage state for toggle function---//
boolean RelayState = LOW;
int TotalCount_2= 0;
long TotalCount= 0;
int set_point_2= 0;
int counter= 0;
unsigned long resetStartTime = 0;
float reset_total = 0;
unsigned long lastTime = 0;
const unsigned long sampleInterval = 1000;
volatile uint16_t flow_count = 0;
float flow_rate = 0.0;
float total_volume;
int volume_rst;
int volume_int0;
char state;
int batas_volumeMin = 0;
int batas_volumeMax = 99999999;
const int batas_pulsaMin = 1;
const int batas_pulsaMax = 999;
boolean ledState = LOW;
unsigned int PULSES_PER_LITER = 250;
void setup(){
Serial.begin(9600);
pinMode(sensor_pulse, INPUT);
pinMode(pinPulse, OUTPUT);
TM1.begin(11, 8, 4); // clockpin, datapin, #digits
TM1.displayClear();
TM1.setBrightness(7);
TM2.begin(9, 10, 4); // clockpin, datapin, #digits
TM2.displayClear();
TM2.setBrightness(7);
attachInterrupt(digitalPinToInterrupt(sensor_pulse), countPulse, FALLING);
}
//----Debouncing function for all buttons----//
boolean debounce(boolean last, int pin)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}
void loop (){
//Debounce for +1 Button
currentsensor_pulse = debounce(lastsensor_pulse, sensor_pulse); //Debounce for +10 Button
/*
if (lastsensor_pulse== LOW && currentsensor_pulse == HIGH){
digitalWrite(pinPulse, HIGH);
}
else{
digitalWrite(pinPulse, LOW);
}
lastsensor_pulse = currentsensor_pulse;
*/
if (currentsensor_pulse == LOW){
digitalWrite(pinPulse, HIGH);
}
else{
digitalWrite(pinPulse, LOW);
}
if ((millis() - lastTime) >= sampleInterval) {
detachInterrupt(digitalPinToInterrupt(sensor_pulse));
// Menonaktifkan interrupt sementara
flow_rate = (flow_count * 60.0) / (sampleInterval / 1000.0) / PULSES_PER_LITER; // Menghitung flow rate dalam L/min
total_volume += flow_rate * (sampleInterval / 60000.0); // Menghitung total volume air dalam 1 detik (dalam satuan Liter)
reset_total += flow_rate * (sampleInterval / 60000.0);
int volume_int = int( total_volume);
int volume_rst = int( reset_total);
TM2.displayInt(flow_rate);
TM1.displayInt(volume_int);
Serial.print (flow_rate);
Serial.print (" - ");
Serial.print (volume_int);
Serial.print (" - ");
Serial.println (flow_count);
flow_count = 0; // Reset jumlah pulsa
lastTime = millis(); // Update waktu terakhir
attachInterrupt(digitalPinToInterrupt(sensor_pulse), countPulse, FALLING);
}
//EEPROM.write(pulsa1, TotalCount);
}
void countPulse() {
flow_count++;
}