I have an electron that I have connected to a water flow hall effect sensor.
I’m not sure what’s going on, because even when nothing is connected to the board (other than USB and Antenna) I get output out of the serial monitor.
I wanted to play with sleep/wake so I’m playing with pressing a button to force the device to wakeup from sleep or go to sleep.
Attached is the code, as well as sample output from Serial Monitor. Any ideas?
From serial Monitor
Flow rate: 39.8L/min Total Liquid 556938mL/Sec
Continuous seconds of flow: 840
This is the wakeup pin value: 1
Flow rate: 39.8L/min Total Liquid 557602mL/Sec
Continuous seconds of flow: 841
This is the wakeup pin value: 1
const int statusLed = D7;
const int sensorPin = D1;
const int sensorInterrupt = D1;
const int wakeup = D3;
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned int TotalMilliLitres;
bool flag;
bool leakdetected;
int buttonstate;
int contflow; //number of seconds that flow has been more than 0
unsigned long oldTime;
void setup()
{
// Initialize a serial connection for reporting values to the host
Serial.begin(9600);
// Set up the status LED line as an output
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, LOW);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, LOW);
pinMode(wakeup, INPUT);
digitalWrite(wakeup, LOW);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
TotalMilliLitres = 0;
oldTime = 0;
contflow = 0;
flag = false;
leakdetected = false;
buttonstate = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
/**
* Main program loop
*/
void loop()
{
if((millis() - oldTime) > 1000) // Only process counters once per second
{
unsigned int frac;
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
flowMilliLitres = (flowRate / 60) * 1000;
TotalMilliLitres += flowMilliLitres;
oldTime = millis();
if(pulseCount!=0)
{
Serial.print("Flow rate: ");
Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print(".");
frac = (flowRate - int(flowRate)) * 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(" Total Liquid "); // Output separator
Serial.print(TotalMilliLitres);
Serial.print("mL/Sec");
Serial.println();
//Print how long we've seen liquid
Serial.print("Continuous seconds of flow: ");
Serial.print(contflow);
Serial.println();
contflow=contflow+1;
buttonstate = digitalRead(wakeup);
Serial.print("This is the wakeup pin value: ");
Serial.println(buttonstate);
digitalWrite(statusLed, HIGH);
}
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}