Timing with using millis()

Dear All!

I have encountered a phenomenon what I do not really understand because I have never ever seen this before.
I started a new project and start to use timinig with millis() instead delay() and experienced a behaviour of timing like this:
If I write down this lines then the output of the millis() will be greater with 10ms.
for example (I have just write down the interested lines without the normal initializations)

unsigned long timeNow;

timeNow = millis() + 1000; // this line tells me that the time now gets the value of the millis() plus 1000ms (1 second), right?
for(int i=0;i<=100;i++)
{
print(i,DEC); /print out the value of i (anyway not matter where do you want to print out… iti s just an example)
while(millis()<=timeNow); /if 1 second passed the while break out and the for step to the next cycle
}

So my expereince that the time difference between printed values wont be 1second but 10 seconds.

Do anyone saw this kind of behaviour before?

THX
Peter

Please post a complete sketch that illustrates what you are seeing

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code

while(millis()<=timeNow); /if 1 second passed the while break out and the for step to the next cycle
}

Using while() in this way, is simply blocking in the same way as delay()
It doesn’t answer your question but it defeats the reason to use millis() in the first instance.

Hello
Take a look into the BWOD IDE-example to see how does the millis() function works.

For more details see Using millis() for timing. A beginners guide and Several things at the same time

Thanks all of you for the helping will but honestly I have not found the solution for my problem.
Again, my problem is that the millis() which should be counting in ms not works in this way because it is counting in 1000*10 which means that if I build a cycle and print out the output of the millis() then the result will be higher than 1000.
for example
if I would like to print out every 1000ms then the result will be 10000ms.

I tought that it happens beacuse of the clock of the micro but I have not found solution.

As previously requested

Hello
Do you have knowledge about datatypes ?

Hello

surely

:slight_smile: Now I am just rewriting the code just trying something.

Now this is the new code but it is work the same.

unsigned long currentMillis,timing_SP;
int y,i;
boolean mS,mD;

setup()
{
pinMode(speaker,OUTPUT);
}
loop()
{
// speaker test
currentMillis= millis();
if(mS != true)
{
if(i < 3)
{
if(timing_SP == 0)
timing_SP= currentMillis + 300;

		if(currentMillis <= (timing_SP - 100))
			digitalWrite(speaker,HIGH);
		else if(currentMillis > (timing_SP-100) && currentMillis <= timing_SP)
			digitalWrite(speaker,LOW);
		
		if(timing_SP <= currentMillis)
		{
			i++;
			timing_SP= 0;
		}
	}
	else if(i == 2)
	{
		mS= true;
		i= 0;
		timing_SP= 0;
	}
}

}

What happened to the type specifiers for the functions ?

Where is speaker declared ?

Please post you code with all of it in code tags

Sorry about it I tought it would be enough. But this is the enite code.

#include “CUU_Interface.h”
#include “CUU_Parallel_I80.h”
#include “CUU_Parallel_M68.h”
#include “CUU_Serial.h”
#include “Noritake_VFD_CUU.h”
#include “util/delay.h”
#include “Wire.h”
/*
Name: folia_teszt.ino
Created: 5/6/2021 8:02:23 AM
Author: HPETER\Péter
*/

#define HghPrLvL 2 // Felső nyomás határ gomb
#define trigPresLvL 3 // Trigger nyomás határ gomb
#define LedAgreen 32 // “A” led Zöld vezérlés
#define LedBgreen 36 // “B” led piros vezérlés
#define LedAred 33 // “A” led piros vezérlés
#define LedBred 35 // “B” led zöld vezérlés
#define speaker 12 // hangszóró
#define disp ‘\x00’ // teli karakter

unsigned long timing,previousTime,currentMillis,timing_SP,timing_D;
int y,i;
boolean mS,mD;

CUU_Parallel_M68_4bit interface(8, 9, 10, 4, 5, 6, 7); //RS,WR,RD,D4-D7
Noritake_VFD_CUU vfd;

byte teli[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};

void setup()
{
vfd.begin( 20,4 ); // 20x4 karakteres modul
vfd.interface(interface); // interface választás
vfd.CUU_init(); // VFD inicializálás
vfd.CUU_createChar( disp, teli );

pinMode(HghPrLvL,INPUT_PULLUP);	// high pressure button bemenet
pinMode(trigPresLvL,INPUT_PULLUP);	// trigger button bemenet
pinMode(LedAgreen,OUTPUT); // A LED green kimenet
pinMode(LedBgreen,OUTPUT);	// B LED green kimenet
pinMode(LedAred,OUTPUT);	// A LED red kimenet
pinMode(LedBred,OUTPUT);	// B LED red kimenet
pinMode(speaker,OUTPUT);	// hangszóró kimenet

}

void loop()
{
// speaker test
currentMillis= millis();
if(mS != true)
{
if(i < 3)
{
if(timing_SP == 0)
timing_SP= currentMillis + 300;

		if(currentMillis <= (timing_SP - 100))
			digitalWrite(speaker,HIGH);
		else if(currentMillis > (timing_SP-100) && currentMillis <= timing_SP)
			digitalWrite(speaker,LOW);
		
		if(timing_SP <= currentMillis)
		{
			i++;
			timing_SP= 0;
		}
	}
	else if(i == 2)
	{
		mS= true;
		i= 0;
		timing_SP= 0;
	}
}

// display test
if(mD != true)
{
	if(i <= 79)	
	{
		if(timing_D == 0)
			timing_D= currentMillis + 1000;
		
		if (currentMillis >= timing_D);
		{
			vfd.CUU_setCursor(i,y);
			vfd.print(disp);
			i++;
			timing_D= 0;
		}
		if(i==19)
		{
			i=0;
			y++;
		}
	}
	else if(y == 4)
	{
		i= 0;
		y= 0;
		timing_D= 0;
		mD= true;
	}
}

if (digitalRead(HghPrLvL)==LOW) // IF HPB ON
{
	delay(100);
	if (digitalRead(HghPrLvL)==LOW) // IF HPB really ON
	{
		if(digitalRead(LedBgreen)==HIGH)	// IF B LED ON
			digitalWrite(LedBgreen,LOW);	// B green LED OFF
		if(digitalRead(LedAred)==HIGH)
			digitalWrite(LedAred,LOW);
		digitalWrite(LedAgreen,HIGH);	// A green LED ON
		digitalWrite(LedBred,HIGH);
	}
}
if (digitalRead(trigPresLvL) == LOW) // IF TRB ON
{
	delay(100);
	if (digitalRead(trigPresLvL) == LOW) // IF TRB really ON
	{
		if(digitalRead(LedAgreen)==HIGH)
			digitalWrite(LedAgreen,LOW);
		if(digitalRead(LedBred)==HIGH)
			digitalWrite(LedBred,LOW);
		digitalWrite(LedBgreen,HIGH);
		digitalWrite(LedAred,HIGH);
	}
}

}

The first thing that I suggest you do is to change the logic of your program so that your tests for elapsed periods does not rely on adding periods to the current value of millis() and instead tests the time elapsed between the time the period started and the time now as illustrated in the links posted earlier in the topic. Something like

if (currentTime - startTime >= period)
{
  //period has ended.  Time to do something
}

not a fast learner with code tags…

You could also use a library for software timers to make things super easy

Hi,

Thank you very much for your very kind help and I am sorry for late-late reply.
I had to rethink my logic about timing and I have rewrite my code with different way like this.
Now it is working.

Thank you again.
Peter

// speaker test
currentMillis= millis();
if(mS != true)
{
	if(z <= 2)
	{
		if(current_now_ST == 0)
			current_now_ST= currentMillis;
		if(currentMillis <= (current_now_ST + ST_ON_delay))
			digitalWrite(speaker,HIGH);
		if(currentMillis >= (current_now_ST + ST_ON_delay) && currentMillis <= (current_now_ST + (ST_ON_delay + ST_OFF_delay)))
			digitalWrite(speaker,LOW);
		if(currentMillis >= current_now_ST + (ST_ON_delay + ST_OFF_delay))
		{
			z++;
			current_now_ST= 0;
		}
	}
	else if(z == 3)
	{
		mS= true;
		digitalWrite(speaker,LOW);
	}
}