I am developing code for a relay timer, with and LCD screen and reset switch. I have the timer, LCD screen and the reset all functioning correctly. However the output to the relay doesn't seem to work. It sets high at the startup as expected, but then doesn't go low.
I am sure the answer is obvious, but I can't seem to find it. Any help would be appreciated.
// include the library code:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
#include<CountUpDownTimer.h>
CountUpDownTimer T(DOWN);
CountUpDownTimer T2(DOWN);
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
long previousMillis = 0; // will store last time RO was flushed
long previousMillis2 = 0;
long interval = 3580000; // interval at which to Flush (milliseconds)
long flushtime = 120000; // set flush time to 2 mins
boolean reset = false;
int pin_out = 3; // Output to Relay
void setup() {
// Debugging output
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(pin_out, OUTPUT); //set pin as output.
//digitalWrite(pin_out, LOW); // Open valve
reset = true; // intialise system and startup flush cycle
T.SetTimer(0,58,0);
T.StartTimer();
T2.SetTimer(0,2,0);
T2.StartTimer();
}
uint8_t i = 0;
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
unsigned long currentMillis = millis();
T.Timer(); // Interval timer
T2.Timer(); // Flush timer
if (reset == false){
lcd.setCursor(0, 0);
lcd.print("Next Flush ");
if (T.TimeHasChanged() ) // this prevents the time from being constantly shown.
{
lcd.setCursor(11, 0);
lcd.print(T.ShowMinutes());
lcd.print(":");
lcd.print(T.ShowSeconds());
}
}
/* Debug for Relay */
lcd.setCursor(0, 1);
lcd.print("Relay State ");
lcd.print(digitalRead(pin_out));
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.clear();
lcd.setCursor(0, 0);
if (buttons & BUTTON_SELECT) {
reset = true;
T2.ResetTimer();
previousMillis = currentMillis; // reset the flush timer
}
}
if (currentMillis - previousMillis >= interval) {
reset = true;
lcd.clear();
T2.ResetTimer();
previousMillis = currentMillis; // reset the flush timer
}
if (reset == true) {
digitalWrite(pin_out, HIGH); // Open valve
lcd.setBacklight(RED);
lcd.setCursor(0, 0);
lcd.print("Flushing RO");
lcd.setCursor(12, 0);
if (T2.TimeHasChanged() ) // this prevents the time from being constantly shown.
{
lcd.print(T2.ShowMinutes());
lcd.print(":");
lcd.print(T2.ShowSeconds());
}
}
if ((reset == true) && (currentMillis - previousMillis >= flushtime)) {
reset = false;
T.ResetTimer();
previousMillis = currentMillis; // reset the flush timer
digitalWrite(pin_out, LOW); // Close valve
lcd.clear();
lcd.setBacklight(BLUE);
}
}