How to store a number in a variable every 40 micro seconds?

Hi everyone,
I am using Arduino Mega 2560 and would you like store some numbers in to a variable, but every 40-20 micro seconds. It is possible do it?
Below here is the code that i am using:

float actual[4] = {0, 0, 0, 0};

float var_table[] = {
  0.88 ,
  0.77 ,
  0.66 ,
  0.55 ,

void setup() {
  while (!Serial);
int i = 0;

void loop() {
  for (i = 0; i < 4; i + 1)
    actual[i] = var_table[i];

    Serial.println(actual[i], 7);
    i = i + 1;


How can i do it?

Please clarify....

Where would the number come from? Are you sampling something (using what) ? Where do you want to store the number? An array? What type of data? Byte?

Ok, before, i just have to put some numbers in to variable called : var_table. Then i want put this numbers in to other variable called:actual. I'm not sampling anything for now but i would you like store then every 20-40 micro seconds. Do not worry to the serial monitor command for now. In future I will make a comparison algorithm between these values every 20-40 micro seconds and control an output

I hope I was clear

I am sorry if my English is not very good, i am Italian

I am sorry if my English is not very good, i am Italian

So, why are you not posting in the Italian section, where there are people that can understand your native language.

It is very easy to store a value in a variable every 20 microseconds, IF you are not doing much else between storing new values. If are, like reading from an analog pin, then it becomes more challenging.

A more capable board might be called for -

Digilent - chipKIT™ Max32, $49.99
512KB Flash
80 MHz operating speed
Four SPI and five I2C connectors
16 channel 10-bit ADC, five compare/PWM outputs
Dual CAN controllers
83 available I/O pins
3.3V operating voltage
16 analog inputs
Connects to a PC using a USB A to mini-B cable (not included)
USB 2.0 OTG controller
10/100 Ethernet MAC
User LED

int number = 1;
int variable;

unsigned long previousMillis = 0;

void setup() {


void loop() {

  unsigned long currentMillis = millis();

  const unsigned long microSeconds_40 = 40;
  if (currentMillis - previousMillis >= microSeconds_40) {
    previousMillis = currentMillis;
    variable = number; 

Doing ANYTHING every 20uSec means you won't be doing much of anything else, most especially things like Serial I/O.

Regards, Ray L.

I'm not sure i get what you are trying to do. If the numbers are already in memory (not sampling) then why do you need to store them every 40 microsecond - they are already in memory...


Need to use microseconds not millis()

Reply #5 has mixed up millis() and micros(). It should be

const unsigned long microSeconds_40 = 40;
  if (micros() - previousMicros >= microSeconds_40) {
    previousMicros  += microSeconds_40;
    variable = number;

For accuracy previousMicros += microSeconds_40; is better. For an explanation see Several Things at a Time

Keep in mind that micros() updates in steps of 4.