Use millis() to increase a number

Hello I would like to know if this cose is correct, I want to increase a number each time millis() counts, I am doing this beacuse I want to make a Ton timer

Post arduino:

Hello everybody I would like to know if this is correct:

unsigned long count=0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
If(millis())
{
Serial.println(count);
count++;
}
}

Can you not run it, to test it? Have you ever seen any code in which millis() is used that way? Have you read the documentation for millis()?

parksplit: I want to make a Ton timer

What is a Ton timer?

Hello everybody I would like to know if this is correct:

It's not.

This link may help Using millis() for timing. A beginners guide

...R

PS ... always test a program before asking for help because then you can tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

Thanks for your reply, I have tested it with my arduino my value encreases as spected but I wasn't sure if it is increasing with millis time or if iis encreasing with loop

parksplit: Thanks for your reply, I have tested it with my arduino my value encreases as spected but I wasn't sure if it is increasing with millis time or if iis encreasing with loop

Time it with a watch or clock.

millis() will always be true, except for a very, very, very tiny window every seven weeks

Thanks for your reply, fast answer given to me "my code is not correct" , I was going to test it with proteus timer clock but I think it is useless, I will read mullis() documento again

parksplit:
Hello I would like to know if this cose is correct, I want to increase a number each time millis() counts…

Once finished reading, consider how you will tell the processor that millis() has counted.

Robin2: What is a Ton timer?

Unless I miss my guess, https://www.theautomationstore.com/rslogix-500-training-timers-ton-tof-and-rto/

If you need a to increment a counter every milli-second you'd be better off using one of the timer counters and setting it up so it interrupts every milli-second. In your interrupt handler you would just increment the counter variable.

noweare: If you need a to increment a counter every milli-second you'd be better off using one of the timer counters and setting it up so it interrupts every milli-second. In your interrupt handler you would just increment the counter variable.

Wouldn't it be much simpler to use micros() ?

...R

millis() is a counter that increases in millisecond steps.

Robin2: Wouldn't it be much simpler to use micros() ?

...R

Yeah, that works

Whandall: millis() is a counter that increases in millisecond steps.

But he wants to know when it increases. I guess he could just do something like this but looks a little skechy to me.

start=millis();

if(millis() > start)
{ count++;
  start=millis();
}

noweare: But he wants to know when it increases. I guess he could just do something like this

start=millis()

if(millis() > start) { count++;}

That won't do it.

It needs to be

if (millis() - previousMillis >= 1) {
  previousMillis ++;
  count ++;
}

Using micros() in the same way might give more precision

if (micros() - previousMicros >= 1000) {
  previousMicros = micros();
  count ++;
}

But, as @Whandall has said, if the OP just needs to know the number of millisecs between two events that can be done directly by saving the value of millis() at the first event and again at the second event and subtracting the first value from the second value

...R

But, as @Whandall has said, if the OP just needs to know the number of millisecs between two events that can be done directly by saving the value of millis() at the first event and again at the second event and subtracting the first value from the second value

...R

The OP wanted to increase the count on every tick of millis()

Hello I would like to know if this cose is correct, I want to increase a number each time millis() counts,

noweare: The OP wanted to increase the count on every tick of millis()

That's exactly what millis() trys to do and it starts with zero. So millis() will have the same value as an uint32_t that is incremented every millisecond.

Whandall: That's exactly what millis() trys to do.

You need something with a better resolution than millis() in order to know WHEN millis() changes so you know when to increment the counter.

I don't know what the end game is as far as why the OP would want to do that or if it even makes sense.

I normally use the difference in millis() to track elapsed time.

Nope, millis() increments automatically.

So how do you increment a variable each time millis() increments ?