Timer question

Simple Question Like most I'm new, but learning fast, and have a question.

How do I start the program over in 7 days?

Delay wont work, the program is doing all kinds of stuff, I think I need a timer or clock function but everything I have seen deals with building clocks or LCD clocks I don't need that complicated.

Tnx

Delay wont work,

Why not? 7UL * 24UL * 60UL * 60UL * 1000UL

the program is doing all kinds of stuff,

...that you haven't shown us. (hint)

How do I start the program over in 7 days?

Hit the reset button after 7 days. Maybe connect a servo to do it for you.

Seriously, what exactly needs to restart after 7 days? There should be no reason to restart the Arduino after 7 days. Making the code repeat some action(s) every 7 days is understandable, and there is even a TimedAction library that makes this easy.

//Ok for the code here goes
I’m really just trying to combine 2 pieces of code together
1st one is a bunch of relays that will go off once, and then again “every 7 days”.
The second is a PH sensor that should be on, all the time, except when the relays go off.
I’m lost how to do this it shouldn’t be that hard.

//The 1st one… relays

void setup() {

pinMode(13, OUTPUT); //drain
pinMode(12, OUTPUT); //solinoid
pinMode(11, OUTPUT); //A nutrient solution
pinMode(10, OUTPUT); //B nutrient solution
pinMode(9, OUTPUT); //PH down
}

void loop() {
digitalWrite(13, HIGH); // set the drain on
delay (1000UL * 10UL * 60UL); //10 min delay
digitalWrite(13, LOW); // set the drain off
digitalWrite(9, HIGH); //set the PH down on
delay (30000); //30 second delay
digitalWrite(9, LOW); //sets the PH down off
digitalWrite(11,HIGH); //sets nutrient A on
digitalWrite(10, HIGH); //sets nudtent B on
delay (1000ul * 1UL * 60UL); //1 min delay
digitalWrite(11, LOW); //sets nutrient A off
digitalWrite(10, LOW); //sets nutrient B off
digitalWrite(12,HIGH); //sets the solinoid on
delay (1000UL * 10UL * 60UL); //10 min delay
digitalWrite(12, LOW); //sets solinoid off

delay (7UL * 24UL * 60UL * 60UL * 1000UL); //7 day delay
}

///The second one… PH

#include <NewSoftSerial.h>
NewSoftSerial mySerial = NewSoftSerial(2, 3);
char stamp_data[15];
byte holding;
byte i;
byte startup=0;
void setup(){
mySerial.begin(38400);
Serial.begin(38400);
}
void loop() {

if(startup==0){
for(i=1; i <= 2;i++){
delay(1000);
mySerial.print(“l0”);
mySerial.print(13,BYTE);
delay(1000);
mySerial.print(“l1”);
mySerial.print(13,BYTE);
}
startup=1;
delay(1000);
mySerial.print(“c”);
mySerial.print(13,BYTE);
}
if(mySerial.available() > 3) {
holding=mySerial.available();
for(i=1; i <= holding;i++){
stamp_data*= mySerial.read();*

  • }*
  • for(i=1; i <= holding;i++){*
    Serial.print(stamp_data*);
    _
    }_
    _
    Serial.println("");_
    _
    }_
    _
    }*_

The delay() function really should be renamed to twiddleYourThumbsAndDoNothingElseForAWhile() to get the point across that nothing else happens while delay() is going on. If you delay(oneWeek), you won’t be reading the PH sensor during that thumb twiddling time.

On the other hand, if, on every pass through loop, you see if enough time has elapsed to warrant reading the pH sensor, and if so, read it, then see it enough time has elapsed to warrant running the pumps for a while, and if so, do it, you CAN do both at the same time.

The millis() function will tell you how long the Arduino has been running. If you track when the pumps were last run, you can determine if it is time to do it again. Like this:

unsigned long lastPumpTime = 0;
unsigned long pumpInterval = 7UL * 24UL * 60UL * 60UL * 1000UL;

void loop()
{
   unsigned long now = millis();
   if(now - lastPumpTime > pumpInterval)
   {
      // run the pumps
      lastPumpTime = now;
   }

   // Same concept for PH sensor
}

"millis() function" I was thinking Id need an actual clock code since I couldn’t figure this out. Going to read all I can on the millis() function, and try it out. Thanks your wonderful Id buy you a beer anytime.

OK tried writing the code, used only one millis function, as I want everything to to loop in 7 days.
Sketch doesn’t like it, it gives me an error at the bottom }
tnx

#include <NewSoftSerial.h>
NewSoftSerial mySerial = NewSoftSerial(2, 3);
char stamp_data[15];
byte holding;
byte i;
byte startup=0;

void setup(){
mySerial.begin(38400);
Serial.begin(38400);
pinMode(13, OUTPUT); //drain
pinMode(12, OUTPUT); //solinoid
pinMode(11, OUTPUT); //A nutrient solution
pinMode(10, OUTPUT); //B nutrient solution
pinMode(9, OUTPUT); //PH down
}

unsigned long lastPumpTime = 0;
unsigned long pumpInterval = 7UL * 24UL * 60UL * 60UL * 1000UL;

void loop() {
{
unsigned long now = millis();
if(now - lastPumpTime > pumpInterval)
digitalWrite(13, HIGH); // set the drain on
delay (1000UL * 10UL * 60UL); //10 min delay
digitalWrite(13, LOW); // set the drain off
digitalWrite(9, HIGH); //set the PH down on
delay (30000); //30 second delay
digitalWrite(9, LOW); //sets the PH down off
digitalWrite(11,HIGH); //sets nutrient A on
digitalWrite(10, HIGH); //sets nutrient B on
delay (1000ul * 1UL * 60UL); //1 min delay
digitalWrite(11, LOW); //sets nutrient A off
digitalWrite(10, LOW); //sets nutrient B off
digitalWrite(12,HIGH); //sets the solinoid on
delay (1000UL * 10UL * 60UL); //10 min delay
digitalWrite(12, LOW); //sets solinoid off
delay (1UL * 24UL * 60UL * 60UL * 1000UL);
if(startup==0){
for(i=1; i <= 2;i++){
delay(1000);
mySerial.print(“l0”);
mySerial.print(13,BYTE);
delay(1000);
mySerial.print(“l1”);
mySerial.print(13,BYTE);
}
startup=1;
delay(1000);
mySerial.print(“c”);
mySerial.print(13,BYTE);
}
if(mySerial.available() > 3) {
holding=mySerial.available();
for(i=1; i <= holding;i++){
stamp_data*= mySerial.read();*

  • }*
  • for(i=1; i <= holding;i++){*
    Serial.print(stamp_data*);
    _
    }_
    _
    Serial.println("");_
    _
    lastPumpTime = now;_
    _
    } *_

}

You're first IF statement needs brackets enclosing the commands you want to run once every week.

well I don't see a 7 day delay, a one day delay but I have that one because I don't want any readings for a day after all the nutrient changes gives it time to stabilize. The bracket thing I'm lost can you give me a hint?.

if(now - lastPumpTime > pumpInterval)

How much of the following code do you want executed when this is true? If just the next line, no { or } are needed. If you want more than one statement executed, all the statements to be executes need to be enclosed in { and }.

well Id like the whole thing to start over
added some brackets no luck

code
#include <NewSoftSerial.h>
NewSoftSerial mySerial = NewSoftSerial(2, 3);
char stamp_data[15];
byte holding;
byte i;
byte startup=0;

void setup(){
mySerial.begin(38400);
Serial.begin(38400);
pinMode(13, OUTPUT); //drain
pinMode(12, OUTPUT); //solinoid
pinMode(11, OUTPUT); //A nutrient solution
pinMode(10, OUTPUT); //B nutrient solution
pinMode(9, OUTPUT); //PH down
}

unsigned long lastPumpTime = 0;
unsigned long pumpInterval = 7UL * 24UL * 60UL * 60UL * 1000UL;

void loop() {
{
unsigned long now = millis();
if(now - lastPumpTime > pumpInterval)
{ //added these no luck
digitalWrite(13, HIGH); // set the drain on
delay (1000UL * 10UL * 60UL); //10 min delay
digitalWrite(13, LOW); // set the drain off
digitalWrite(9, HIGH); //set the PH down on
delay (30000); //30 second delay
digitalWrite(9, LOW); //sets the PH down off
digitalWrite(11,HIGH); //sets nutrient A on
digitalWrite(10, HIGH); //sets nutrient B on
delay (1000ul * 1UL * 60UL); //1 min delay
digitalWrite(11, LOW); //sets nutrient A off
digitalWrite(10, LOW); //sets nutrient B off
digitalWrite(12,HIGH); //sets the solinoid on
delay (1000UL * 10UL * 60UL); //10 min delay
digitalWrite(12, LOW); //sets solinoid off
delay (1UL * 24UL * 60UL * 60UL * 1000UL); // one day delay, to stabilize before reading PH
}
if(startup==0){
for(i=1; i <= 2;i++){
delay(1000);
mySerial.print(“l0”);
mySerial.print(13,BYTE);
delay(1000);
mySerial.print(“l1”);
mySerial.print(13,BYTE);
}
startup=1;
delay(1000);
mySerial.print(“c”);
mySerial.print(13,BYTE);
}
if(mySerial.available() > 3) {
holding=mySerial.available();
for(i=1; i <= holding;i++){
stamp_data*= mySerial.read();*

  • }*
  • for(i=1; i <= holding;i++){*
    Serial.print(stamp_data*);
    _
    }_
    _
    Serial.println("");_
    _
    lastPumpTime = now;_
    _
    } *_

}

Here’s my take on a scheduler that will do roughly what you want. It’s designed to be expandable.

All the intervals are currently constants but variables and other unsigned long integer expressions will work as well.

#include <NewSoftSerial.h>
NewSoftSerial mySerial =  NewSoftSerial(2, 3);

//  Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)

unsigned long CycleStartTime = 0;
unsigned long LastTaskTime = 0;
unsigned long CurrentTaskInterval = 0;
unsigned Task = 0;

void setup()
{
  mySerial.begin(38400);
  Serial.begin(38400);
  pinMode(13, OUTPUT);   //drain   
  pinMode(12, OUTPUT);  //solinoid
  pinMode(11, OUTPUT);  //A nutrient solution
  pinMode(10, OUTPUT);  //B nutrient solution
  pinMode(9, OUTPUT);   //PH down
}

void loop()
{
  unsigned long currentTime = millis();

  // If the time has not yet come to perform a task
  if (currentTime - LastTaskTime < CurrentTaskInterval)
    return;   // Nothing to do

  LastTaskTime = currentTime;

  switch (Task++)
  {
  case 0:
    CycleStartTime = currentTime;  // Remember this time
    digitalWrite(13, HIGH);   // set the drain on
    CurrentTaskInterval = minutes_in_ms(10);
    break;

  case 1:
    digitalWrite(13, LOW);  // set the drain off
    digitalWrite(9, HIGH);  // set the PH down on
    CurrentTaskInterval = seconds_in_ms(30);
    break;

  case 2:
    digitalWrite(9, LOW);  //sets the PH down off
    digitalWrite(11,HIGH); //sets nutrient A on
    digitalWrite(10, HIGH); //sets nutrient B on
    CurrentTaskInterval = minutes_in_ms(1);
    break;

  case 3:
    digitalWrite(11, LOW); //sets nutrient A off
    digitalWrite(10, LOW); //sets nutrient B off
    digitalWrite(12,HIGH); //sets the solinoid on
    CurrentTaskInterval = minutes_in_ms(10);
    break;

  case 4:
    digitalWrite(12, LOW); //sets solinoid off
    CurrentTaskInterval = days_in_ms(1);
    break;


  case 5:
    //  Put some stuff here to do 1 day after the solenoid turns off

    // Start over again in one week from the START of the cycle
    Task = 0;
    LastTaskTime = CycleStartTime;
    CurrentTaskInterval = weeks_in_ms(1);
    break;
  } 
}

Wow tnx but I Tried this no go

#include <NewSoftSerial.h>
NewSoftSerial mySerial = NewSoftSerial(2, 3);
char stamp_data[15];
byte holding;
byte i;
byte startup=0;
// Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)1000UL)
#define minutes_in_ms(m) ((m)60UL1000UL)
#define hours_in_ms(h) ((h)60UL60UL
1000UL)
#define days_in_ms(d) ((d)24UL60UL60UL1000UL)
#define weeks_in_ms(w) ((w)7UL24UL60UL60UL1000UL)
unsigned long CycleStartTime = 0;
unsigned long LastTaskTime = 0;
unsigned long CurrentTaskInterval = 0;
unsigned Task = 0;
void setup()
{
mySerial.begin(38400);
Serial.begin(38400);
pinMode(13, OUTPUT); //drain
pinMode(12, OUTPUT); //solinoid
pinMode(11, OUTPUT); //A nutrient solution
pinMode(10, OUTPUT); //B nutrient solution
pinMode(9, OUTPUT); //PH down
}
void loop()
{
unsigned long currentTime = millis();
// If the time has not yet come to perform a task
if (currentTime - LastTaskTime < CurrentTaskInterval)
return; // Nothing to do
LastTaskTime = currentTime;
switch (Task++)
{
case 0:
CycleStartTime = currentTime; // Remember this time
digitalWrite(13, HIGH); // set the drain on
CurrentTaskInterval = minutes_in_ms(10);
break;
case 1:
digitalWrite(13, LOW); // set the drain off
digitalWrite(9, HIGH); // set the PH down on
CurrentTaskInterval = seconds_in_ms(30);
break;
case 2:
digitalWrite(9, LOW); //sets the PH down off
digitalWrite(11,HIGH); //sets nutrient A on
digitalWrite(10, HIGH); //sets nutrient B on
CurrentTaskInterval = minutes_in_ms(1);
break;
case 3:
digitalWrite(11, LOW); //sets nutrient A off
digitalWrite(10, LOW); //sets nutrient B off
digitalWrite(12,HIGH); //sets the solinoid on
CurrentTaskInterval = minutes_in_ms(10);
break;
case 4:
digitalWrite(12, LOW); //sets solinoid off
CurrentTaskInterval = days_in_ms(1);
break;
case 5:
// Put some stuff here to do 1 day after the solenoid turns off
// Start over again in one week from the START of the cycle
if(startup==0){
for(i=1; i <= 2;i++){
delay(1000);
mySerial.print(“l0”);
mySerial.print(13,BYTE);
delay(1000);
mySerial.print(“l1”);
mySerial.print(13,BYTE);
}
startup=1;
delay(1000);
mySerial.print(“c”);
mySerial.print(13,BYTE);
}
if(mySerial.available() > 3) {
holding=mySerial.available();
for(i=1; i <= holding;i++){
stamp_data
= mySerial.read();*

  • }*
  • for(i=1; i <= holding;i++){*
    Serial.print(stamp_data*);
    _
    }_
    _
    Serial.println("");_
    _
    }_
    _
    }_
    _
    Task = 0;_
    _
    LastTaskTime = CycleStartTime;_
    CurrentTaskInterval = weeks_in_ms(1);
    _
    break;_
    _
    }_
    _
    }*_

The code you added was badly formed and had an extra close-bracket at the end.

When you take code that compiles, and add stuff, and it then DOESN’T compile: you probably added an error. :\

Try adding this stuff in place of the stuff you added:

    //  Put some stuff here to do 1 day after the solenoid turns off
    if(startup == 0)
    {
      for(i=1; i <= 2;i++)
      {
        delay(1000);
        mySerial.print("l0");
        mySerial.print(13,BYTE);
        delay(1000);
        mySerial.print("l1");
        mySerial.print(13,BYTE);
      }
      startup=1;
      delay(1000);
      mySerial.print("c");
      mySerial.print(13,BYTE);
    }
    
    if(mySerial.available() > 3) 
    {
      holding=mySerial.available();
      for(i=1; i <= holding;i++)
      {
        stamp_data[i]= mySerial.read();
      }
      for(i=1; i <= holding;i++)
      {
        Serial.print(stamp_data[i]);
      }
      Serial.println("");
    }

    // Start over again in one week from the START of the cycle

I owe you a beer
it works
tnx

code

EDIT: Moderator code put in code box. OP, please go back over previous posts, click “modify”, highlight your code, then click on the # icon on the toolbar, then click “save”. AWOL

#include <NewSoftSerial.h>
NewSoftSerial mySerial =  NewSoftSerial(2, 3);
char stamp_data[15];
byte holding;
byte i;
byte startup=0;
//  Some macros for defining time intervals in milliseconds
#define seconds_in_ms(s) ((s)*1000UL)
#define minutes_in_ms(m) ((m)*60UL*1000UL)
#define hours_in_ms(h)   ((h)*60UL*60UL*1000UL)
#define days_in_ms(d)    ((d)*24UL*60UL*60UL*1000UL)
#define weeks_in_ms(w)   ((w)*7UL*24UL*60UL*60UL*1000UL)
unsigned long CycleStartTime = 0;
unsigned long LastTaskTime = 0;
unsigned long CurrentTaskInterval = 0;
unsigned Task = 0;
void setup()
{
  mySerial.begin(38400);
  Serial.begin(38400);
  pinMode(13, OUTPUT);   //drain   
  pinMode(12, OUTPUT);  //solinoid
  pinMode(11, OUTPUT);  //A nutrient solution
  pinMode(10, OUTPUT);  //B nutrient solution
  pinMode(9, OUTPUT);   //PH down
}
void loop()
{
  unsigned long currentTime = millis();
  // If the time has not yet come to perform a task
  if (currentTime - LastTaskTime < CurrentTaskInterval)
    return;   // Nothing to do
  LastTaskTime = currentTime;
  switch (Task++)
  {
  case 0:
    CycleStartTime = currentTime;  // Remember this time
    digitalWrite(13, HIGH);   // set the drain on
    CurrentTaskInterval = minutes_in_ms(10);
    break;
  case 1:
    digitalWrite(13, LOW);  // set the drain off
    digitalWrite(9, HIGH);  // set the PH down on
    CurrentTaskInterval = seconds_in_ms(30);
    break;
  case 2:
    digitalWrite(9, LOW);  //sets the PH down off
    digitalWrite(11,HIGH); //sets nutrient A on
    digitalWrite(10, HIGH); //sets nutrient B on
    CurrentTaskInterval = minutes_in_ms(1);
    break;
  case 3:
    digitalWrite(11, LOW); //sets nutrient A off
    digitalWrite(10, LOW); //sets nutrient B off
    digitalWrite(12,HIGH); //sets the solinoid on
    CurrentTaskInterval = minutes_in_ms(10);
    break;
  case 4:
    digitalWrite(12, LOW); //sets solinoid off
    CurrentTaskInterval = days_in_ms(1);
    break;
  case 5:
    //  Put some stuff here to do 1 day after the solenoid turns off
    // Start over again in one week from the START of the cycle
if(startup == 0)
    {
      for(i=1; i <= 2;i++)
      {
        delay(1000);
        mySerial.print("l0");
        mySerial.print(13,BYTE);
        delay(1000);
        mySerial.print("l1");
        mySerial.print(13,BYTE);
      }
      startup=1;
      delay(1000);
      mySerial.print("c");
      mySerial.print(13,BYTE);
    }
    
    if(mySerial.available() > 3) 
    {
      holding=mySerial.available();
      for(i=1; i <= holding;i++)
      {
        stamp_data[i]= mySerial.read();
      }
      for(i=1; i <= holding;i++)
      {
        Serial.print(stamp_data[i]);
      }
      Serial.println("");
    }

    // Start over again in one week from the START of the cycle
    Task = 0;
    LastTaskTime = CycleStartTime;
    CurrentTaskInterval = weeks_in_ms(1);
    break;
  }
}