need help adjusting millis () and LastTick intervals to achieve better accuracy

cattledog:
Did you look in the reference section to understand what += means?
https://www.arduino.cc/en/Reference/IncrementCompound

Did you consider when this statement is true if lastTick is = to a fixed value and not += some increment?

if (micros() - lastTick >= 1001500) {

Can you answer your own question?

No sir. I tried. But i can't :)) i have to learn a lot. So any time I use a value after subtraction "if (micros() - lastTick >= 1001500" i have to use a fixed value with "+=" rather than lasttick= micros()? Right? And does this += value have to be the same as the one in subtraction line?

arduinoware:
But did you mean?

Answering your question would likely get me labeled a "moaner" so I decline to answer with "no disrespect".

It is so hard to get answers from people in this forum that everyone seems like a moaner around here :)). "moaner" must be a qualification needed to sign up here. So I say don't worry about it. So can you please answer my question. Please. Because I don't accept your declination. :)) I know nothing about programming. Just trying to finish a project.

arduinoware:
It is so hard to get answers from people in this forum that everyone seems like a moaner around here :)).

That's probably because they have even less of a clue of what they're trying to do, than the ones here trying to provide help and learn a bit/get some ideas in the process (the latter is at least why I'm here).

By the way, your suggestion causes more trouble than it is worth. When I used the

lastTick += 1001500;

it is even harder to calibrate time. So, I went back to

lastTick = micros ();

and achieved better results.

arduinoware:
By the way, your suggestion causes more trouble than it is worth.

That is provably false. And has been proven to be false on this forum many many times.

But it is your project.

That's interesting.

Did you remember to update this line as well, when calibrating?

if (micros() - lastTick >= 1001500) {

Actually better code would be:

#define INTERVAL 1001500   // the actual length of a second in micros() as measured by the Arduino,
                           // change this to adjust the clock speed.
static unsigned long lastTick = 0;
if (micros() - lastTick >= INTERVAL) {
 lastTick += INTERVAL;
 second++;
}

This makes sure that every second is INTERVAL microseconds long. Otherwise indeed you may get a little drift, as depending on the rest of your code you could be a few milliseconds late ticking the second, meaning you start counting the next tick late if you would use micros() there again.

wvmarle:
That's interesting.

Did you remember to update this line as well, when calibrating?

if (micros() - lastTick >= 1001500) {

Actually better code would be:

#define INTERVAL 1001500   // the actual length of a second in micros() as measured by the Arduino,

// change this to adjust the clock speed.
static unsigned long lastTick = 0;
if (micros() - lastTick >= INTERVAL) {
lastTick += INTERVAL;
second++;
}




This makes sure that every second is INTERVAL microseconds long. Otherwise indeed you may get a little drift, as depending on the rest of your code you could be a few milliseconds late ticking the second, meaning you start counting the next tick late if you would use micros() there again.

Thanks. I appreciate. The INTERVAL feature makes sense. But on the other hand, if I use that in my code (which I have included below) I find it harder to find the sweet spot in order to get the most accuracy possible out of arduino. Regardless of how fine I calibrate the INTERVAL value, it either gains too much or drifts behind. I am using values between 1001200-1001300. Whereas if I use only the micros(), I can get an accuracy of + or - 30 seconds for 24 hours. Any ideas? Perhaps, something to do with my code? Here is my code:

/*
An open-source binary clock for Arduino. 
Based on the code from by Rob Faludi (http://www.faludi.com)
Code under (cc) by Daniel Spillere Andrade, www.danielandrade.net
http://creativecommons.org/license/cc-gpl
*/

int second=0, minute=0, hour=0; //start the time on 00:00:00
int munit,hunit,valm=0,valh=0,ledstats,i;

void setup() { //set outputs and inputs
pinMode(1, OUTPUT);pinMode(2, OUTPUT);pinMode(3, OUTPUT);pinMode(4, OUTPUT);pinMode(5, OUTPUT);
pinMode(6, OUTPUT);pinMode(7, OUTPUT);pinMode(8, OUTPUT);pinMode(9, OUTPUT);pinMode(10, OUTPUT);
pinMode(11, OUTPUT);pinMode(12, OUTPUT);pinMode(13, OUTPUT);

pinMode(0, INPUT);
}

void loop() {

#define INTERVAL 1001300

static unsigned long lastTick = 0;
if (micros() - lastTick >= INTERVAL) {
 lastTick += INTERVAL;
 second++;
}

// move forward one minute every 60 seconds
	if (second >= 60) {
	minute++;
	second = 0; // reset seconds to zero
}

// move forward one hour every 60 minutes
if (minute >=60) {
	hour++;
	minute = 0; // reset minutes to zero
}

if (hour >=24) {
	hour=0;
	minute = 0; // reset minutes to zero
}

	munit = minute%10; //sets the variable munit and hunit for the unit digits
	hunit = hour%10;


	ledstats = digitalRead(0);  // read input value, for setting leds off, but keeping count
	if (ledstats == LOW) {
    
	for(i=1;i<=13;i++){
	digitalWrite(i, LOW);}
  
	} else  {

	//minutes units
	if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {  digitalWrite(1, HIGH);} else {  digitalWrite(1,LOW);}
	if(munit == 2 || munit == 3 || munit == 6 || munit == 7) {digitalWrite(2, HIGH);} else {digitalWrite(2,LOW);}
	if(munit == 4 || munit == 5 || munit == 6 || munit == 7) {digitalWrite(3, HIGH);} else {digitalWrite(3,LOW);}
	if(munit == 8 || munit == 9) {digitalWrite(4, HIGH);} else {digitalWrite(4,LOW);}

	//minutes 
	if((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60))  {digitalWrite(5, HIGH);} else {digitalWrite(5,LOW);}
	if(minute >= 20 && minute < 40)  {digitalWrite(6, HIGH);} else {digitalWrite(6,LOW);}
	if(minute >= 40 && minute < 60) {digitalWrite(7, HIGH);} else {digitalWrite(7,LOW);}

	//hour units
	if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {digitalWrite(8, HIGH);} else {digitalWrite(8,LOW);}
	if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {digitalWrite(9, HIGH);} else {digitalWrite(9,LOW);}
	if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {digitalWrite(10, HIGH);} else {digitalWrite(10,LOW);}
	if(hunit == 8 || hunit == 9) {digitalWrite(11, HIGH);} else {digitalWrite(11,LOW);}

	//hour
	if(hour >= 10 && hour < 20)  {digitalWrite(12, HIGH);} else {digitalWrite(12,LOW);}
	if(hour >= 20 && hour < 24)  {digitalWrite(13, HIGH);} else {digitalWrite(13,LOW);}

	}

	valm = analogRead(0);    // add one minute when pressed
	 if(valm<800) {
	 minute++;
	 second=0;
	 delay(250);
	}
  
	valh = analogRead(5);    // add one hour when pressed
	 if(valh<800) {
	 hour++;
	 second=0;
	 delay(250);
	}


}

30 seconds in 24 hours that's 347 microseconds per second. You adjust 100 us steps, that should give you an 8.6 second per 24 hours difference.