0

This is mostly a copy-paste of the code I found on Google,

I want to make a project using 2 waterflow sensors in which inflow() which shows how many liters i have taken in and outflow() which shows how many liters flown out.

This is how far ive reached, Need help with the code please, I am not a advanced coder so descriptive code and support is HIGHLY appreciated. also please see the maincode(), in that section i am trying to achieve a loop, i mean if sensor1 is high it should display sensor1(inflow()) output ,and if sensor 2 is high it should display sensor2(outflow()) output.

Problems faced: the output doesn't work when i call both the inflow() and outflow() together, one function works,(i think it has something to do with the Interrupt Pins of the board?).


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "SECRET";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wifi";
char pass[] = "password";

//byte statusLed    = 13;

byte inFlowSensor = D2;  
byte outFlowSensor= D3;

float calibrationFactor = 4.5;
BlynkTimer timer;

volatile byte pulseCount;  

float inFlowRate; // V2 - inflowrate 
float outFlowRate; // V4 - outFowRate
boolean sensorInput = 0;
unsigned int inFlowMilliLitres; 
unsigned int outFlowMilliLitres; 
unsigned long inTotalMilliLitres; // V1 - inTotalLitres
//unsigned long totalLitres;
unsigned long outTotalMilliLitres; // V3 - outTotalLitres
unsigned long oldTime;

BLYNK_CONNECTED() { // runs once at device startup, once connected to server.

  Blynk.syncVirtual(V1); //gets last known value of V1 virtual pin
  Blynk.syncVirtual(V3); //gets last known value of V4 
}

BLYNK_WRITE(V1)
{
  inTotalMilliLitres = param.asFloat();

}

BLYNK_WRITE(V2)
{
  inFlowRate = param.asFloat();

}

BLYNK_WRITE(V3)
{
  outTotalMilliLitres = param.asFloat();

}

BLYNK_WRITE(V4)
{
  outFlowRate = param.asFloat();

}


BLYNK_WRITE(V5) {  // reset all data with button in PUSH mode on virtual pin V4
  int resetdata = param.asInt();
  if (resetdata == 0) {
    Serial.println("Clearing Data");
    Blynk.virtualWrite(V1, 0);
    Blynk.virtualWrite(V2, 0);
    inFlowRate = 0;
    outFlowRate = 0;
    inFlowMilliLitres = 0;
    outFlowMilliLitres = 0;
    inTotalMilliLitres = 0;
    outTotalMilliLitres = 0;
    //totalLitres = 0;
    //totalLitresold = 0;
  }
}

ICACHE_RAM_ATTR void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

void inflow()
{

   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    detachInterrupt(inFlowSensor);

    inFlowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

    oldTime = millis();

    inFlowMilliLitres = (inFlowRate / 60) * 1000;

    // Add the millilitres passed in this second to the cumulative total
    inTotalMilliLitres += inFlowMilliLitres;

    unsigned int frac;

    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(inFlowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (inFlowRate - int(inFlowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.print("L/min");
    // Print the number of litres flowed in this second
    Serial.print("  Current Fuel Flowing: ");             // Output separator
    Serial.print(inFlowMilliLitres);
    Serial.print("mL/Sec");

    // Print the cumulative total of litres flowed since starting
    Serial.print("  Input Fuel Quantity: ");             // Input separator
    Serial.print(inTotalMilliLitres);
    Serial.println("mL"); 

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;

    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(inFlowSensor, pulseCounter, FALLING);
  }
}

void outflow()
{

   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    detachInterrupt(outFlowSensor);

    outFlowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

    oldTime = millis();

    outFlowMilliLitres = (outFlowRate / 60) * 1000;

    // Add the millilitres passed in this second to the cumulative total
    outTotalMilliLitres += outFlowMilliLitres;

    unsigned int frac;

    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(outFlowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (outFlowRate - int(outFlowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.print("L/min");
    // Print the number of litres flowed in this second
    Serial.print("  Current Fuel Flowing: ");             // Output separator
    Serial.print(outFlowMilliLitres);
    Serial.print("mL/Sec");

    // Print the cumulative total of litres flowed since starting
    Serial.print("  Out Fuel Quantity: ");             // Input separator
    Serial.print(outTotalMilliLitres);
    Serial.println("mL"); 

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;

    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(outFlowSensor, pulseCounter, FALLING);
  }
}


void sendtoBlynk()  // In this function we are sending values to blynk server
{
  Blynk.virtualWrite(V2, inFlowRate);
  Blynk.virtualWrite(V1, inTotalMilliLitres);
  Blynk.virtualWrite(V4, outFlowRate);      
  Blynk.virtualWrite(V3, outTotalMilliLitres);
}

void setup()
{

  Serial.begin(9600); //38400
  Blynk.begin(auth,ssid,pass);
  Serial.println("Setup Started");

  pulseCount        = 0;
  inFlowRate          = 0.0;
  outFlowRate          = 0.0;
  inFlowMilliLitres   = 0;
  outFlowMilliLitres   = 0;
  inTotalMilliLitres  = 0;
  outTotalMilliLitres  = 0;
  oldTime           = 0;

  attachInterrupt(inFlowSensor, pulseCounter, FALLING);
  //attachInterrupt(outFlowSensor, pulseCounter, FALLING);
  timer.setInterval(10000L, sendtoBlynk);
}

void maincode(){
  inflow();
  //outflow();
}

/**
 *  program loop
 */
void loop(){
  Blynk.run();
  timer.run();
  Serial.println("Timer and Blynk Started");
  Serial.println(inFlowSensor);
  Serial.println(outFlowSensor);
  maincode();
}```
fuse
  • 61
  • 8
  • Concerning `BLYNK_WRITE(V1)` (and others): no return type, no argument type? Are these macros which expand to proper function signatures? – Scheff's Cat Feb 13 '20 at 07:59
  • @Scheff `BLYNK_WRITE()` Function is a loop that gets called whenever the associated linked item (i.e. a button) gets triggered – fuse Feb 13 '20 at 08:06

0 Answers0