Flow Switch to ESP-32S
Posted: Mon Jan 14, 2019 10:31 am
Help me to code my flow switch (YF-S201), i tried this code works on arduino mega 2560, but why not worked here?
this is my code found in internet
this is my code found in internet
also, i tried this worked toovolatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 23; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
flow_frequency++;
}
void setup()
{
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
Serial.begin(115200);
attachInterrupt(0, flow, RISING); // Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
flow_frequency = 0; // Reset Counter
Serial.print(l_hour, DEC); // Print litres/hour
Serial.println(" L/hour");
}
}
int TURBINE; //pengukuran SINYAL data yang bersifat incremental
int HSensor = 23; //nama alias pada pin 2
int Calc;
void speedrpm () //fungsi penghitungan dan interrupt
{
TURBINE++; //bersifat incrementing (dengan mode falling edge)
}
void setup()
{
pinMode(HSensor, INPUT); //inisialisasi sebagai input
Serial.begin(115200);
attachInterrupt(0, speedrpm, RISING); //cara penulisan perintah interrupt
}
void loop ()
{
TURBINE = 0; //data awal = 0
sei(); //perintah aktifnya mode interrupt
delay (1000); //nilai delay 1 detik
cli(); //perintah untuk matinya program interrupt
Calc = (TURBINE * 60 / 7.5); //Pulsa * 60 / 7.5
//satuan FLOW RATE benda cair yaitu L / hour
Serial.print(Calc, DEC); //menampilkan hasil pembacaan kalkulasi flow rate dalam bentuk dec di serial monitor
Serial.print(" L/hour\r\n"); //Tampilkan L / hour pada baris baru
}