#include #include #include #include #include #include #include #include #include RTC_DS1307 rtc; int electro_in1 = 12; int electro_in2 = 11; byte sensorInterrupt = 0; // 0 = digital pin 2 const byte sensorPin = 2; const byte pulsadorPin = 3; float calibrationFactor = 7.11; volatile unsigned int pulseCount; float flowRate; float totalLitres; unsigned long oldTime; float volumen; float volumenMaximo = 0.5; bool flag = false; int state = HIGH; int reading; int previous = LOW; int lastDay; int eeAddress = 0; int EE_ADDR2 = 1; void pulseCounter() { pulseCount++; } void flash() { if (analogRead(A1) < 920) { pinMode(A1, OUTPUT); digitalWrite(A1, HIGH); return; } pinMode(A1, INPUT); if (analogRead(A1) > 1000) { digitalWrite(electro_in1, HIGH); digitalWrite(electro_in2, HIGH); EEPROM.put(eeAddress, volumen); //EEPROM.put(EE_ADDR2, lastDay); } } void setup() { Serial.begin(9600); pinMode(electro_in1, OUTPUT); pinMode(electro_in2, OUTPUT); digitalWrite(electro_in1, HIGH); digitalWrite(electro_in2, HIGH); pinMode(sensorPin, INPUT); pinMode(pulsadorPin, INPUT); pulseCount = 0; flowRate = 0.0; oldTime = 0; attachInterrupt(sensorInterrupt, pulseCounter, FALLING); EEPROM.get(eeAddress, volumen); MsTimer2::set(10, flash); MsTimer2::start(); Serial.print("Litros iniciales: "); Serial.print(volumen); pinMode(13, OUTPUT); digitalWrite(13, LOW); //EEPROM.get(EE_ADDR, lastDay); } void loop() { reading = digitalRead(pulsadorPin); if (reading == HIGH && previous == LOW) { delay(500); if (state == HIGH) state = LOW; else state = HIGH; } previous = reading; if (volumen < volumenMaximo && state == LOW) if ((millis() - oldTime) > 1000) { detachInterrupt(sensorInterrupt); flowRate = (((1000.0) / (millis() - oldTime)) * pulseCount) / calibrationFactor; volumen += flowRate / 60; Serial.print (volumen, 3); Serial.println (" L"); pulseCount = 0; attachInterrupt(sensorInterrupt, pulseCounter, FALLING); oldTime = millis(); flag = true; } if (volumen >= volumenMaximo ) { flag = false; } if (flag == true && state == LOW) { digitalWrite(electro_in1, LOW); digitalWrite(electro_in2, LOW); } else { digitalWrite(electro_in1, HIGH); digitalWrite(electro_in2, HIGH); } /* DateTime now; if (now.day() != lastDay) // this happens exactly once a day. volumen = 0; // reset the accumulated volume lastDay = now.day(); */ }