24 hours Loop help with code

Need help with code. Using UNO R3. its simple 24 hrs loop. for chicken feed, opens 3 times in 24hrs at different delay time. open/close time 6seconds.
after 24 hrs it adds total of 114 seconds (-/+1sec).

// defines pins
#define stepPin 13
#define dirPin 12 
#define enbPin 11
#define belPin 8
#define SECOND 1000UL
#define MINUTE (SECOND * 60UL)
#define HOUR (MINUTE * 60UL)

void setup() {
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(enbPin,OUTPUT);
  pinMode(belPin,OUTPUT);
}
void loop() {
  
  
  digitalWrite(dirPin,HIGH); 
  for(int x = 0; x < 3000; x++) {
   
    digitalWrite(belPin,HIGH);
    digitalWrite(enbPin,LOW);
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(1000);    
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(1000); 
    digitalWrite(enbPin,HIGH);
    digitalWrite(belPin,LOW);
    
  
  }
  delay((MINUTE*20)-(SECOND*6)); 
  
  digitalWrite(dirPin,LOW); 
  for(int x = 0; x < 3000; x++) {
    digitalWrite(enbPin,LOW);
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
    digitalWrite(enbPin,HIGH);
  }
  delay(((HOUR*5)+(MINUTE*40))-(SECOND*6));

    digitalWrite(dirPin,HIGH); 
  for(int x = 0; x < 3000; x++) {
    
   digitalWrite(belPin,HIGH);
    digitalWrite(enbPin,LOW);
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(1000);    
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(1000); 
     digitalWrite(enbPin,HIGH);
    digitalWrite(belPin,LOW);
  
  }
  delay((MINUTE*15)-(SECOND*6)); 
  
  digitalWrite(dirPin,LOW); 
  for(int x = 0; x < 3000; x++) {
    digitalWrite(enbPin,LOW);
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
    digitalWrite(enbPin,HIGH);
  }
  delay(((HOUR*13)+(MINUTE*45))-(SECOND*6));
  
  digitalWrite(dirPin,HIGH); 
  for(int x = 0; x < 3000; x++) {
    digitalWrite(belPin,HIGH);
    digitalWrite(enbPin,LOW);
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(1000);    
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(1000); 
     digitalWrite(enbPin,HIGH);
    digitalWrite(belPin,LOW);
  
  }
  delay((MINUTE*30)-(SECOND*6)); 
  
  digitalWrite(dirPin,LOW); 
  for(int x = 0; x < 3000; x++) {
    digitalWrite(enbPin,LOW);
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
    digitalWrite(enbPin,HIGH);
  }
  delay(((HOUR*3)+(MINUTE*30))-(SECOND*6));



}

Welcome and thanks for using Code Tags. I suggest you get a RTC (Real Time Clock) module as the Arduino is probably not using a crystal and there will be timing errors especially over time. They have an alarm function so you can use that and not worry about timing. Get the better one, I find mine very close after running a year or so.

Posting your schematic would help us verify what you are doing.

If you wish to work with what you have for the moment, consider this.
Each cycle, 3 per day, is running 38 seconds over, more or less. So adjust your delay by 38 s. I made some other minor edits:

// defines pins
#define stepPin 13
#define dirPin 12
#define enbPin 11
#define belPin 8
#define SECOND 1000UL
#define MINUTE (SECOND * 60UL)
#define HOUR (MINUTE * 60UL)

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enbPin, OUTPUT);
  pinMode(belPin, OUTPUT);
}
void loop() {
  open();    
  delay((MINUTE * 20) - (SECOND * (6+38)));
  close();   
  delay(((HOUR * 5) + (MINUTE * 40)) - (SECOND * 6));
  open();    
  delay((MINUTE * 15) - (SECOND * (6+38)));
  close();   
  delay(((HOUR * 13) + (MINUTE * 45)) - (SECOND * 6));
  open();    
  delay((MINUTE * 30) - (SECOND * (6+38)));
  close();   
  delay(((HOUR * 3) + (MINUTE * 30)) - (SECOND * 6));
}

void open() {
  digitalWrite(dirPin, HIGH);
  for (int x = 0; x < 3000; x++) {
    digitalWrite(belPin, HIGH);
    digitalWrite(enbPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
    digitalWrite(enbPin, HIGH);
    digitalWrite(belPin, LOW);
  }
}
void close() {
  digitalWrite(dirPin, LOW);
  for (int x = 0; x < 3000; x++) {
    digitalWrite(enbPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
    digitalWrite(enbPin, HIGH);
  }
}
1 Like

RTC is the way to do this.
What happens if the module loss power? It will over feed the chicks? That could be a problem.

RTC + log.
Also, check for sleep modes. There is no need to keep the code running 24 hs.

What kind of help do you need? Does the code you have not work? What does it do wrong?

Thank you. Yes, adjusting the difference in delay time would work. it will be subtracting 38s each opening because it adds 114s. but i am trying find out why Arduino adding time or is it the code. its my first Arduino so just trying to understand problems.

you are talking phd and i am still in elementary. its my first code. i don't know what RTC is, I will read up on it.

it adds 114s every 24hrs. just trying to figure out why.

You have to use a '24h clock', you can't use delays to simulate a clock ( as you found it won't work ).
An rtc is a circuit/module ( hardware ) that gives you the '24h clock'.

1 Like

Your delay times add up to 24 hours, but that's not all your code does. The time taken by the other steps does add up.

If you're satisfied with how it works except for that then just take 114 seconds worth of delay out somewhere.

Or better yet use the Time library or even an RTC or NTP to get the real time.

First, there is the delay and also the time it does take to do stuff when it's not just delaying.

Also in play is the fact that the processor clock is not necessarily all that accurate. The best you can hope for is that it will be dependable, that is to say it will either gain or lose time consistently.

For that sketch you wrote, checking over some longish period and using the error to inform a regular hard adjustment of skipping or adding 42 seconds or whatever can give pretty good results.

An RTC is superior for all the reasons mentioned so far, and while @maikarg worries

It will over feed the chicks?

I am just the opposite - will the chicks go hungry?

The RTC is an inexpensive module and is very easy to use in this straightforward application, doing something three times a day on schedule.

a7

1 Like

Thank you. so, its the Arduino, not the code. will go with RTC.

yes, and no. If the Arduino clock were perfect, we'd help you correct your code, so that it didn't prevent good timing; as it is, an RTC will give you better timing, and allow less-than-perfect code to exist. In time, you'll realize what I'm talking about, and understand that I'm not being mean, just observing reality.

1 Like

Thank you, everyone is suggesting RTC. And yes, over feeding chickens not a problem. going hungry not a huge issue because they are not locked. plenty of bugs. And This Arduino project is more of learning thing for myself and my 6yo. so he can build toys and i can automate little chores and spend more time with kids.

Compared to the +/-0.5% specification on the Arduino’s clock speed, at 114/86400=0.13%, your clock is pretty good. It would still meet specification at up to 86400x0.005=+/-432sec/day.

1 Like

There's also a tempco to consider; IIRC, a chicken coop isn't the place you'd find temperature regulation; ours went from -15 to +30 C. Of course, even an RTC may be taxed in that environment. Just sayin'...

1 Like

If your board support NTP you can also use the net to synchronize time ( chicken will also enjoy streaming in the long winter nights ; - )

4 Likes

For now, i just subtracted additional 114sec. Working on RTC. Thank you everyone.