Hello,
How do I get access to millis tick counter from my own .cpp? When one function in the module calls another in the same module with millis() in it, millis() never returns.
Oleg.
Hello,
How do I get access to millis tick counter from my own .cpp? When one function in the module calls another in the same module with millis() in it, millis() never returns.
Oleg.
My guess is that you're barking up the wrong tree.
I tested what you've described and it works fine. Here are the three files I used for the test...
CallMillis.h ...
#ifndef __CallMillis_h__
#define __CallMillis_h__
unsigned long OneFunction( void );
#endif
CallMillis.cpp ...
#include <wiring.h>
unsigned long AnotherFunction( void )
{
return( millis() );
}
unsigned long OneFunction( void )
{
return( AnotherFunction() );
}
Sketch ...
#include "CallMillis.h"
void setup( void )
{
Serial.begin( 38400 );
}
void loop( void )
{
unsigned long t;
t = OneFunction();
Serial.println( t, DEC );
delay( 1000 );
}
Without more details, I'm afraid I can't be more helpful.
I am sorry. I was trying to oversimplify. It actually happens inside a class when call is made from a constructor. This is how it looks:
myclass.h
#ifndef _myclass_h_
#define _myclass_h_
#include "WProgram.h"
class myclass {
public:
myclass(void);
void function();
};
#endif //_myclass_h_
myclass.cpp
include "myclass.h"
myclass::myclass(void)
{
Serial.begin( 9600 );
Serial.print("One");
function();
Serial.print("Five");
};
void myclass::function()
{
unsigned long dly;
Serial.print("Two");
dly = millis() + 10;
Serial.print("Three");
//while( dly > millis());
//delay(10);
Serial.print("Four");
}
sketch:
#include <myclass.h>
myclass create; //run constructor
void setup(){}
void loop(){}
This compiles and works, printing "One" to "Five". If you uncomment either one of commented out lines in function(), the thing stops after printing "Three".
Thanks! It makes sense now. However, the question remains - why does it work past
dly = millis() + 10;
?
It's not that millis() hangs when called from the constructor of a global object instance, it's that it doesn't work correctly. It returns 0. So an expression like
dly = millis() + 10;
sort of works, but not
while (dly > millis());
which loops forever.
Mikal
Thanks for explanation!