Modifying Millis() function

Hi,

I have some problem modifying Millis() while refering to the BlinkWithoutDelay example. I understand that the example uses the Millis() function. Millis() a command that returns the number of milliseconds since the board started running its current sketch.

However, i would like to modify Millis() and would want a command that return the number of millisecond since i press a button.

How do i do it? Is there any in-built Arduino function that can help?

Sorry, i am still new to programming.

Why would you want to modify a function upon which so much else depends?
Don't you want to build upon it instead?

Remember when you pressed the button. Find the time now. Subtract one from the other.

calai:
Hi,

I have some problem modifying Millis() while refering to the BlinkWithoutDelay example. I understand that the example uses the Millis() function. Millis() a command that returns the number of milliseconds since the board started running its current sketch.

However, i would like to modify Millis() and would want a command that return the number of millisecond since i press a button.

How do i do it? Is there any in-built Arduino function that can help?

You are meaning a "math function for "subtraction"?
Yes, it is included in Arduino!

If you need the milliseconds since you pressed a button, that's the programming logic:

  1. In the moment the button is pressed, store thecurrent millis() in an unsigned long variable, like
 unsigned long buttonPressTime=millis();

Then, at any later time, you can find out the time since by doing a subtraction:

unsigned long tmillisecondsSinceButtonPress= millis()-buttonPressTime;

This will work for periods up to a bit more than 49 days.

BTW: That's not 'modifying' the millis () function, it is 'calculating' a time difference using the millis() function.

later_time minus earlier_time = time_passed since.

This is not good advice....

jurs:

 unsigned long buttonPressTime=millis();

That creates a local variable inside whatever if() statement determined that the button was pressed. That value is not available to be used outside that particular block. (A block is defined by the {} braces.)

One of the correct ways to do this is to make it a global variable, so that it is accessible from everywhere in your program.

unsigned long buttonPressTime;

Then inside the if() block, put this...

    buttonPressTime = millis();

A better way to do this is to make the variable local to whatever function or class is actually doing the button press detection and timing. That way you can have a module that's easy to use in multiple programs or multiple places in one program. In that case, the original declaration needs to be prefixed with static which tells the compiler to keep the value stored even when that function is not running. Globals are automatically static.

Thanks guys for your help, especially MorganS and jurs. After some understanding on the codes, i managed to clear my doubts and solve the problem. :slight_smile: