OK, I had it the other way around. This should turn the Relay pin OFF when the button is pushed and ON a variable time after the WaterLevel input is greater than the FlowRate input (whatever that means).
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const byte LED0Pin = 2;
const byte OLED_RESET = 4;
const byte Relay1Pin = 5;
const byte Relay0Pin = 10;
const byte WaterLevelPin = A0;
const byte Button0Pin = A1;
const byte TimeDelayPin = A2;
const byte FlowRatePin = A3;
Adafruit_SSD1306 display(OLED_RESET);
int FlowRate = 0;
int WaterLevel = 0;
int Button0State = 0;
unsigned long Relay0Time = 0;
unsigned Relay0Interval = 0; // 0 to 15345 (1023*15)
bool Relay0On;
unsigned long multiplier = 15;
void setup()
{
//Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 20); // 0=Column; 5=Row
display.print("Aquaponics");
display.setTextSize(1);
display.setCursor(40, 50); // 0=Column; 5=Row
display.print("Ver.f2");
display.display();
delay (3000);
display.setTextSize(1.8);
pinMode(Relay0Pin, OUTPUT);
pinMode(Relay1Pin, OUTPUT);
pinMode(LED0Pin, OUTPUT);
pinMode(Button0Pin, INPUT);
Relay0On = false;
digitalWrite(Relay0Pin, Relay0On);
}
void loop()
{
unsigned long currentTime = millis();
FlowRate = analogRead(FlowRatePin);
WaterLevel = analogRead(WaterLevelPin);
Button0State = digitalRead(Button0Pin);
Relay0Interval = analogRead(TimeDelayPin) * multiplier; // Set 0 to 15.345 second delay
display.setCursor(2, 0); // 0=Column; 5=Row
display.print("FLOW");
display.setCursor(120, 0);
display.print("%");
display.setCursor(100, 0);
display.println(FlowRate / 10); // to vary 0 to end result, ie 1024/?
display.fillRect(2, 10, FlowRate / 8, 1, 1);
// 2=V from left; 61=H from top; FlowRate/8= lenght of bar ie, FlowRate/16 is half screen with full value; 3= thickness of slide bar; 1= ? colour or brightness
display.setCursor(2, 20); // 0=Column; 30=Row
display.print("H2O LEVEL");
display.setCursor(120, 20);
display.print("%");
display.setCursor(100, 20);
display.println(WaterLevel / 10); // to vary 0 to end result, ie 1024/?
display.fillRect(2, 30, WaterLevel / 8, 1, 1);
// 2=V from left; 61=H from top; WaterLevel/8= lenght of bar ie, WaterLevel/16 is half screen with full value; 3= thickness of slide bar; 1= ? colour or brightness
display.setCursor(2, 39);
display.println(currentTime / 1000 / 60);
display.setCursor(25, 39);
display.print("MIN");
display.setCursor(50, 39);
// display.print(counter);
display.setCursor(72, 39);
display.print("INTERRUPT");
display.setCursor(2, 56); // 2=Column; 40=Row
display.print(analogRead(TimeDelayPin) / 66);
display.setCursor(25, 56);
display.print("SECONDS DELAY");
display.display();
display.clearDisplay(); // Clear display must be used to clear text etc
// If the button is pressed, turn on the LED
digitalWrite (LED0Pin, Button0State);
// If the button is pressed, turn on the relay
if (Button0State)
{
Relay0On = true;
digitalWrite (Relay0Pin, Relay0On);
}
// If the relay is ON anbd the water level is greater then the flow rate (?!?!?) turn off the relay for an adjustable amount of time.
if (Relay0On && (WaterLevel > FlowRate))
{
// Turn off for variable seconds and then turn on
Relay0On = false;
digitalWrite (Relay0Pin, Relay0On);
Relay0Time = currentTime;
digitalWrite (LED0Pin, LOW);
}
// If the relay is OFF and Relay0Interval has elapsed, turn the relay ON
if (!Relay0On && currentTime - Relay0Time > Relay0Interval)
{
Relay0On = true;
digitalWrite(Relay0Pin, Relay0On);
}
}