modifying the millis functions

so im controling a motor and using an as5600 mag encoder

I need to create a history of the last 256 readings

I want each record 1 ms apart

so I'm basically doing buffer[millis()&B11111111]

but readings were insane and spiking.. it took me a while to figure out that there is a "missing millisecond" every 42 ms due to FRACT_INC. so there are empty records in my buffer

I want to remove the FRACT counter

I realize this will make millis drift compared to real time.. but i dont care

my questions are 1)will this affect anything else (I am not using any libraries.. just serial and analogRead.. which i don't think should care about the millis timer.. right?) 2)is there proper way to do this (i can just modify the source code in the arduino library.. but i would prefer to change it in a way that only affects this project)

any help greatly appreciated

I realize i could do buffer[(millis()>>1)&B11111111] to get a working 2ms interval buffer.. and i might be willing to accept this.. but i would much prefer to be able to do a smaller interval

follow up

if i also change the overflow to make millis do say .. half a millisecond.. will that affect anything else?

**and fyi.. yes i have modified the adc settings to read high freq

Not only that, but about 7uS is lost in the loop() overhead.

If you want to do rock-solid timing, turn off the millis background with noInterrupts, and put a while(1) inside the loop().

Time with a software for or while.

I know it's clumsy, but it works

Allan

i want interrupts

and i dont need it rock solid down to micos

just a steady non skips millis.. does nto need to be perfectly accurate

I know what i need to do.. i need to remove the FRACT part of the timers for millis

but is there a proper way to do this in one project without me just modifying the Arduino source code so that my changes only affect this project?

Have you tried doing your timing using micros() rather than millis() - something like this

if (micros() - prevSampleMicros >= 1000) {
  prevSampleMicros += 1000;
  // take sample
}

...R

Robin2:
Have you tried doing your timing using micros() rather than millis() - something like this

if (micros() - prevSampleMicros >= 1000) {

prevSampleMicros += 1000;
  // take sample
}




...R

what about micros overflow?
that would screw it up…

but i guess buffer[((micros()>>10)&1)] would work

it is an extra mask

and and i wouldnt mind removing some logic from the millis interrupt

I really just want to remove the FRACT. it is the most elegant and efficient solution as far as i can see

is there a reason why this cant be done?
do i just need to modify my Arduino library?

Don't cross-post. Thread locked.

(This post moderated from the middle of the Bay of Mont St Michel!)