Hey,
I have pretty much no clue what im actually doing so please be patient with me, and thanks for reading through this even if you dont reply.
So i got the following code to read data off the YF-S201 sensor and publish it to events in the cloud.
However i keep getting "0"as output.
no idea where im going wrong .
( i got this code off another project built by someone else AKA not Mine)
byte statusLed = D7;
byte sensorInterrupt = D2; // 0 = digital pin 2
byte sensorPin = D2;
// The hall-effect flow sensor outputs approximately 4.5 pulses per second per litre/minute of flow.
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
void setup()
{
// Initialize a serial connection for reporting values to the host
Serial.begin(38400);
// Set up the status LED line as an output
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH); // We have an active-low LED attached
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
if((millis() - oldTime) > 1000) // Only process counters once per second
{
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
unsigned int frac;
char str[10];
char str2[10];
Serial.print('Flow rate: ');
Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print('.'); // Print the decimal point
frac = (flowRate - int(flowRate)) * 10;
Serial.print(frac, DEC) ; // Print the fractional part of the variable
Serial.print('L/min');
Serial.print(' Current Liquid Flowing: '); // Output separator
Serial.print(flowMilliLitres);
Serial.print('mL/Sec');
sprintf(str, "%d", flowMilliLitres);
Particle.publish("FlowRatesmlPerSec", str);
Serial.print(" Output Liquid Quantity: "); // Output separator
Serial.print(totalMilliLitres);
Serial.println("mL");
sprintf(str2, "%d", totalMilliLitres);
Particle.publish("totalFlowInML", str2);
}
}
void pulseCounter()
{
pulseCount++;
}