Photon Hall-Water-Flow-Sensor Problem with "Zero-Data" in Particle console

Hi community,
as a newbie and more or less non-coder

I am trying to set up a Photon @ home (for future an Electron in field) as water flow observing device for a breeding fish-tank for fertiled salmonid-eggs.

I am using a common flow-sensor (turbine, hall-effect) and getting pulses out of it. Checked with a LED, works well so far.

The problem(s): Console writes “0” Data : /

My simple code is, …

volatile int flow_frequency; // Measures flow meter pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowmeter = D2; // Flow Meter Pin number
unsigned long currentTime;
unsigned long cloopTime;
char l_hour_chars[8];
String l_hour_str;

void flow () { // Interrupt function
  flow_frequency++;
}

void setup() {
  pinMode(flowmeter, INPUT);
  Serial.begin(9600);
  attachInterrupt(D0, flow, RISING); // Setup Interrupt
  // see [...]; // 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. (Results in +/- 3% range)
    l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour
    flow_frequency = 0; // Reset Counter
    Serial.print(l_hour, DEC); // Print litres/hour
    Serial.println(" L/hour");
    l_hour_str = String(l_hour);
    l_hour_str.toCharArray(l_hour_chars, 8);
    Particle.publish("L/hour", l_hour_chars, PRIVATE);
    delay(5000);
  }
}

Has anyone an idea, why Photon resp. the code sends Data as a zero?
Pinconnections and wiring check trice.

Thank you in advance.
btw, if anybody has a code of different style/idea, I would appreciate to try it , )

I took the liberty to reformat your code (proper indentation makes reading someone else’s - and even your own - much more easy :wink: ) and also replaced the anonymous pin designations 0 and 2 with the preferable D0 & D2 respectively.

Having that out the way, do you at least see the Serial.print() output you are expecting?

And some further advice:

  • your time check should better be if (currentTime - cloopTime >= 1000) to avoid possible issues when mixing data types
  • you don’t need (and should avoid) String l_hour_str and rather go with snprintf(l_hour_chars, sizeof(l_hour_chars), "%.2f", l_hour);

BTW, what are you checking with a LED and how?

However, the issue I see is that you are setting the pinMode() for D2 but then attach the interrupt to D0 - that won’t do.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.