Hey,
I'm relatively new to arduino, but I've decided to delve into building an automated greenhouse. The basic design is to have two motors controlled via syren 10, along with a rtc and some inductive sensors to pul a tarp along a frame I'm building, and blackout the greenhouse. Classic Light Deprivation. The one issue I've run into, funny enough, happens to be a delay. I'll post the code to make it clear.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
#include <Sabertooth.h>
Sabertooth ST1[2] = { Sabertooth(128), Sabertooth(129) } ;
LiquidCrystal_I2C lcd(0x27,20,4) ;
DS3231 rtc(SDA, SCL) ;
Time t ;
int Hor ;
int Min ;
int Sec ;
boolean relay_on = false ;
int induct = A0 ;
//int inductB = A1 ;
int Relay = 8 ;
int val = 0 ;
//....................................
void setup()
{ Serial.begin(9600) ;
Wire.begin() ;
rtc.begin() ;
SabertoothTXPinSerial.begin(9600) ;
Sabertooth::autobaud(SabertoothTXPinSerial) ;
lcd.init() ;
lcd.begin(20,4) ;
lcd.backlight() ;
delay(3000) ;
pinMode(Relay,OUTPUT) ;
digitalWrite(Relay,HIGH) ;
lcd.setCursor(0,0) ;
lcd.print("Flowers") ;
delay(2000) ;
lcd.clear() ; }
//....................................
void loop()
{ val = analogRead(induct) ;
t = rtc.getTime() ;
Hor = t.hour ;
Min = t.min ;
Sec = t.sec ;
lcd.setCursor(0,0) ;
lcd.print("Time: ") ;
lcd.print(rtc.getTimeStr()) ;
lcd.setCursor(0,1) ;
lcd.print("Date: ") ;
lcd.print(rtc.getDateStr()) ;
delay(1000) ;
lcd.clear() ;
//....................................
if( Hor == 12 && (Min == 36 || Min == 37))
{ OpenFunc() ; }
//....................................
if(Hor == 19 && (Min == 30 || Min == 31))
{ CloseFunc() ; }}
//....................................
void OpenFunc ()
{{digitalWrite(Relay,LOW) ;
relay_on = true ;}//without a delay here the inductive sensor on A0 can be monitored and the timing //for turning off the motors and relay is on point. Obviously the delay can cause some issues, like the //reading never being registered, and the motor continues to pull on something it shouldn't
if (relay_on)
{lcd.setCursor(0,3) ;
lcd.print("Opening...") ;
ST1[0].motor(1, 60) ;
ST1[1].motor(1, 60) ;
if (val <= 500 or Hor == 12 && Min == 38)
{ST1[0].motor(1, 0) ;
ST1[1].motor(1, 0) ;
delay(3000) ;
digitalWrite(Relay,HIGH) ;
relay_on = false ;
delay(60000);
lcd.clear() ;
lcd.setCursor(8,3) ;
lcd.print("Open") ;
delay(2000) ; }}}
//....................................
void CloseFunc ()
{{digitalWrite(Relay,LOW) ;
delay(4000); // this delay I'm trying to replace with a millis() timer or something
relay_on = true ;} //needs to be at least two seconds so the desktop power supply
//has time to power the motor controller
if (relay_on)
{lcd.setCursor(0,3) ;
lcd.print("Closing...") ;
ST1[0].motor(1, -60) ;
ST1[1].motor(1, -60) ;
if (val <=500 or Hor == 19 && Min == 33)
{ ST1[0].motor(1, 0) ;
ST1[1].motor(1, 0) ;
delay(3000) ;
digitalWrite(Relay,HIGH) ;
relay_on = false ;
delay(60000);
lcd.clear() ;
lcd.setCursor(7,3) ;
lcd.print("Closed.") ;
delay(2000) ; }}}
To put it simply, I'd like to make a millis timer similar to
unsigned long futuresight = 0 ;
unsigned long interval= 0;
void setup(){
Serial.begin(9600);
}
void loop(){
unsigned long currentMillis = millis();
if( (currentMillis - futuresight <= interval)){
futuresight = currentMillis + 10000UL;
Serial.print("worked");
}}
that happens to be predictive, called upon initiation of the relay, only activates the if statement the once and is not horribly complicated. If I can't figure a means out now no delay is no big deal until I do