Delta_G:
If that statement that flowCount - previousFlowCount < flowDose is true, then you'll be stuck in that while forever. Nowhere in that while loop do any of those three things ever change.
flowCount changes indirectly. In the finished version, the pump will be activated within the while loop and this will set off the flow sensor, so that flowCount increases. As of now, I'm just blowing into the flow sensor. This part seems to work, because I can see the flowCount increase on the serial monitor, even within the while loop.
I can post the whole code, but to be honest it's a bit of a mess and there are many halfway-finished parts that don't make sense as they are:
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Bounce2.h>
#define I2C_ADDR 0x20 // Define I2C Address for controller
#define BACKLIGHT_PIN 7
#define En_pin 4
#define Rw_pin 5
#define Rs_pin 6
#define D4_pin 0
#define D5_pin 1
#define D6_pin 2
#define D7_pin 3
#define LED_OFF 0
#define LED_ON 1
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
byte l[8] = {
B00100,
B00100,
B00100,
B00100,
B00100,
B00100,
B00011,
B00000,
};
long lastThDecrease = 0;
long lastThIncrease = 0;
long lastWdDecrease = 0;
long lastWdIncrease = 0;
long previousMillis = 0;
long keyDelay = 1000;
long keyRepeat = 100;
long humidityReadout = 250;
const int wdupPin = 9;
const int wddownPin = 8;
const int thupPin = 7;
const int thdownPin = 6;
const int resetPin = 1;
const int pumpPin = 4;
const int flowPin = 2;
int pumpState = 0;
int previousPumpState = 0;
unsigned long pumpInterval = 10000;
float countsPerMl = 5.35;
volatile unsigned long flowCount;
unsigned long lastFlowCount;
/*
int wdupState;
int wddownState;
int thupState;
int thdownState;
int resetState;
*/
int previousWdupState;
int previousWddownState;
int previousThupState;
int previousThdownState;
unsigned long lastWdupPush;
unsigned long lastWddownPush;
unsigned long lastThupPush;
unsigned long lastThdownPush;
//unsigned long lastWD;
int WD = 500;
int TH = 60;
int N = 3;
int SH;
unsigned long lastPump = 0;
unsigned long previousFlowCount = 0;
long debounceDelay = 20;
Bounce wdupBounce = Bounce();
Bounce wddownBounce = Bounce();
Bounce thupBounce = Bounce();
Bounce thdownBounce = Bounce();
Bounce resetBounce = Bounce();
void setup() {
Serial.begin(9600);
pinMode(wdupPin, INPUT);
pinMode(wddownPin, INPUT);
pinMode(thupPin, INPUT);
pinMode(thdownPin, INPUT);
pinMode(resetPin, INPUT);
pinMode(flowPin, INPUT);
pinMode(pumpPin, OUTPUT);
attachInterrupt(0, flow, FALLING);
digitalWrite(pumpPin, LOW);
wdupBounce.attach(wdupPin);
wdupBounce.interval(debounceDelay);
wddownBounce.attach(wddownPin);
wddownBounce.interval(debounceDelay);
thupBounce.attach(thupPin);
thupBounce.interval(debounceDelay);
thdownBounce.attach(thdownPin);
thdownBounce.interval(debounceDelay);
// resetBounce.attach(resetPin);
// resetBounce.interval(debounceDelay);
// Display
lcd.begin(16, 2);
lcd.createChar(0, l);
lcd.setCursor(0, 0);
lcd.print("SH:");
lcd.setCursor(8, 0);
lcd.print("TH:");
lcd.setCursor(0, 1);
lcd.print("WU:");
lcd.setCursor(8, 1);
lcd.print("WD:");
}
void flow () {
flowCount += 1;
}
void loop() {
//Measure and display soil humidity
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > humidityReadout) {
previousMillis = currentMillis;
SH = analogRead(0);
SH = map(SH, 0, 1023, 100, 0);
lcd.setCursor(3, 0);
lcd.print(SH);
lcd.print("% ");
if (SH < TH) {
digitalWrite(pumpPin, HIGH);
}
else {
digitalWrite(pumpPin, LOW);
}
}
//Displaying and setting the threshold
//Threshold up
thupBounce.update();
int thupState = thupBounce.read();
if (thupState == LOW) {
previousThupState = LOW;
}
if (thupState == HIGH && previousThupState == LOW) {
lastThupPush = currentMillis;
previousThupState = HIGH;
if (TH < 100) {
TH = TH + 1;
}
}
if (thupState == HIGH && previousThupState == HIGH && TH < 100 && (currentMillis - lastThIncrease) > keyRepeat && (currentMillis - lastThupPush) > keyDelay) {
TH = TH + 1;
lastThIncrease = currentMillis;
}
//Threshold down
thdownBounce.update();
int thdownState = thdownBounce.read();
if (thdownState == LOW) {
previousThdownState = LOW;
}
if (thdownState == HIGH && previousThdownState == LOW) {
lastThdownPush = currentMillis;
previousThdownState = HIGH;
if (TH > 0) {
TH = TH - 1;
}
}
if (thdownState == HIGH && previousThdownState == HIGH && TH > 0 && (currentMillis - lastThDecrease) > keyRepeat && (currentMillis - lastThdownPush) > keyDelay) {
TH = TH - 1;
lastThDecrease = currentMillis;
}
lcd.setCursor(11, 0);
lcd.print(TH);
lcd.print("% ");
//Displaying total water usage
unsigned long WU = WD * N;
float WUL = WU * 0.001;
if (WU < 10000) {
lcd.setCursor(3, 1);
lcd.print(WUL,1);
lcd.write(byte(0));
lcd.print(" ");
}
else {
lcd.setCursor(3, 1);
lcd.print(WUL,0);
lcd.write(byte(0));
lcd.print(" ");
}
//Displaying and setting the water dosage
//WD up
wdupBounce.update();
int wdupState = wdupBounce.read();
if (wdupState == LOW) {
previousWdupState = LOW;
}
if (wdupState == HIGH && previousWdupState == LOW) {
lastWdupPush = currentMillis;
previousWdupState = HIGH;
if (WD < 10000) {
WD = WD + 100;
}
}
if (wdupState == HIGH && previousWdupState == HIGH && WD < 10000 && (currentMillis - lastWdIncrease) > keyRepeat && (currentMillis - lastWdupPush) > keyDelay) {
WD = WD + 100;
lastWdIncrease = currentMillis;
}
// WD down
wddownBounce.update();
int wddownState = wddownBounce.read();
if (wddownState == LOW) {
previousWddownState = LOW;
}
if (wddownState == HIGH && previousWddownState == LOW) {
lastWddownPush = currentMillis;
previousWddownState = HIGH;
if (WD > 0) {
WD = WD - 100;
}
}
if (wddownState == HIGH && previousWddownState == HIGH && WD > 0 && (currentMillis - lastWdDecrease) > keyRepeat && (currentMillis - lastWddownPush) > keyDelay) {
WD = WD - 100;
lastWdDecrease = currentMillis;
}
float WDL = WD * 0.001;
lcd.setCursor(11, 1);
lcd.print(WDL,1);
lcd.write(byte(0));
lcd.print(" ");
// Pump
unsigned long flowDose = WD * countsPerMl;
if (currentMillis - lastPump > pumpInterval && SH < TH) {
while (flowCount - previousFlowCount < flowDose) {
//digitalWrite(pumpPin, HIGH);
Serial.print("On");
Serial.print(" ");
Serial.println(flowCount / countsPerMl);
pumpState = 1;
}
}
else {
//digitalWrite(pumpPin, LOW);
Serial.print("Off");
Serial.print(" ");
Serial.println(flowCount / countsPerMl);
pumpState = 0;
}
if (previousPumpState > pumpState) {
lastPump = currentMillis;
previousFlowCount = flowCount;
}
previousPumpState = pumpState;
}