#include <avr/sleep.h>
#include <FastLED.h>
#define LED_PIN 4
#define NUM_LEDS 8
CRGB leds[NUM_LEDS];
byte sensorInterrupt = 0;
byte sensorPin = 2;
float calibrationFactor = 4;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
int delayTime = 400;
int ON=10;
unsigned long lastTimeFlowRateAboveZero;
unsigned long lastTimeRESET;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
attachInterrupt(digitalPinToInterrupt(sensorPin), pulseCounter , FALLING);
pinMode (2, INPUT);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
if (flowRate > 0.2){
if(totalMilliLitres>100000) {
leds[3] = CRGB(0, 0, ON);
FastLED.show();
leds[4] = CRGB(0, 0, ON);
FastLED.show();
leds[5] = CRGB(0, 0, ON);
FastLED.show();
leds[6] = CRGB(0, 0, ON);
FastLED.show();
leds[7] = CRGB(0, 0, ON);
FastLED.show();
leds[0] = CRGB(ON, 0, 0);
FastLED.show();
delay (200);
leds[3] = CRGB(0, 0, 0);
FastLED.show();
leds[4] = CRGB(0, 0, 0);
FastLED.show();
leds[5] = CRGB(0, 0, 0);
FastLED.show();
leds[6] = CRGB(0, 0, 0);
FastLED.show();
leds[7] = CRGB(0, 0, 0);
FastLED.show();
leds[0] = CRGB(0, 0, 0);
FastLED.show();
delay (200);
}
if(totalMilliLitres>100000) {
leds[1] = CRGB(0, 0, 0);
FastLED.show();
leds[2] = CRGB(0, 0, 0);
FastLED.show();
}
}
if((millis() - oldTime) > 300) {
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60)*2000;
totalMilliLitres += flowMilliLitres;
unsigned int frac;
Serial.print("Flow rate: ");
Serial.print(int(flowRate));
Serial.print("L/min");
Serial.print("\t");
Serial.print(totalMilliLitres/1000);
Serial.print("L");
Serial.print("\t");
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.println("mL");
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
pulseCount = 0;
}
if (flowRate < 0.5) {
lastTimeFlowRateAboveZero = millis();
}
if (millis() - lastTimeFlowRateAboveZero >= 8000) {
pinMode (7, OUTPUT);
digitalWrite (7, LOW);
}
if (flowRate < 0.5) {
delay (200);
leds[4] = CRGB(0, 0, 0);
FastLED.show();
leds[3] = CRGB(0, 0, 0);
FastLED.show();
leds[2] = CRGB(0, 0, 0);
FastLED.show();
leds[1] = CRGB(0, 0, 0);
FastLED.show();
leds[0] = CRGB(0, 0, 0);
FastLED.show();
leds[5] = CRGB(0, 0, 0);
FastLED.show();
leds[6] = CRGB(0, 0, 0);
FastLED.show();
leds[7] = CRGB(0, 0, 0);
FastLED.show();
delay (300);
}
if (flowRate < 0.5 && totalMilliLitres < 10) {
delay (2000);
leds[4] = CRGB(0, 0, 0);
FastLED.show();
leds[3] = CRGB(0, 0, 0);
FastLED.show();
leds[2] = CRGB(0, 0, 0);
FastLED.show();
leds[1] = CRGB(0, 0, 0);
FastLED.show();
leds[0] = CRGB(0, 0, 0);
FastLED.show();
leds[5] = CRGB(0, 0, 0);
FastLED.show();
leds[6] = CRGB(0, 0, 0);
FastLED.show();
leds[7] = CRGB(0, 0, 0);
FastLED.show();
delay (300);
ADCSRA = 0;
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
noInterrupts ();
attachInterrupt (0, wake, HIGH);
EIFR = bit (INTF0);
MCUCR = bit (BODS) | bit (BODSE);
MCUCR = bit (BODS);
interrupts ();
sleep_cpu ();
attachInterrupt(digitalPinToInterrupt(sensorPin),pulseCounter , FALLING);
}
}
void pulseCounter(){
pulseCount++;
}
void wake (){
sleep_disable();
detachInterrupt (0);
}