Need Delay for 400 mirco seconds

Plz frndz its urgent, i need to show time via serial.println command each after 400 mirco seconds
I wrote a program at start the values dont have much error but after few seconds the error increases

Plz tell me what is the prob in this code

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

unsigned long prevMicros = micros() - 400 ;

void loop()
{
    if (micros() - prevMicros  >= 400)
    {     
      prevMicros =prevMicros+400;
    Serial.println(micros());  
  }
}

These are the readings i got

64
408
808
1216
1608
2020
2420
2808
3224
3616
4004
4404
4812
upto here readings are good but after this i got
9576
15816
23096

See at first the readings were very accurate but then the difference is not close to 400us at all

At 9600 bps, printing 5 characters plus CR and LF will take about 7ms!

Increase the serial bit rate in your program and the serial monitor.

unsigned long prevMicros = micros() - 400 ; This line is wrong and gives you the silly 64 at the start of your out put

      prevMicros =prevMicros+400; and this line is also in correct and accounts for the rest of the drifft.

Mark

See if this performs any better
(I don't have access to an arduino at the moment so no totally untested)

void setup()
{//use the same in serial monitor
  Serial.begin(57600);
}

unsigned long prevMicros = 0;

void loop()
{//we only want to read micros once per loop
  unsigned long t=micros(); 

  if (t - prevMicros  >= 400)
  {     
    Serial.println(t);
    prevMicros=t;  
  }
}

Reading micros() once per loop() is best.

However updating prevMicros with prevMicros += 400; is better than prevMicros = t; which can lead to accumulated errors. The explanation for this in in the long discussion in the Thread several things at a time.

…R

KenF your code perform well at first 3 to 4 seconds but after 5, 6 seconds the difference in readings goes to 1200 micro seconds and then the difference becomes almost consistent

bt the question is according to the code which KenF metioned the difference in reading should be approx 400 and it is at the beginning it is almost 400 bt after few seconds the difference changes to somthng above 1000

void setup()
{//use the same in serial monitor
  Serial.begin(57600);
}

unsigned long prevMicros = 0;

void loop()
{//we only want to read micros once per loop
  unsigned long t=micros(); 

  if (t - prevMicros  >= 400)
  {     
    Serial.println(t);
    prevMicros=t;  
  }
}

Why the code tends to work at beginning and not after few seconds

Try Robin2's suggestion ...

However updating prevMicros with prevMicros += 400; is better than prevMicros = t; which can lead to accumulated errors. The explanation for this in in the long discussion in the Thread several things at a time.

Also try increasing the bit rate to 115,200.

By the way, what is the application for this? Why are you trying to print out the data at such a high rate?

Usman93:
bt after few seconds the difference changes to somthng above 1000

That's quite a staggering difference.

Have you tried the suggestions put forward by Robin2 and Hackscribble

void setup()
{//use the same in serial monitor
  Serial.begin(115200);
}

unsigned long prevMicros = 0;

void loop()
{//we only want to read micros once per loop
  unsigned long t=micros(); 

  if (t - prevMicros  >= 400)
  {     
    Serial.println(t);
     prevMicros += 400;
  }
}

Thinking about the rate you are trying to print out ...

Printing a value of 6 seconds means printing "6000000" microseconds, which needs 7 characters plus CR and LF, a total of 9 characters.

At 10 bits per character, that is 90 bits.

90 bits in 400us is 4.4us per bit, which means you need a bit rate of at least 228 kbit/s.

Hackscribble:
Thinking about the rate you are trying to print out …

Printing a value of 6 seconds means printing “6000000” microseconds, which needs 7 characters plus CR and LF, a total of 9 characters.

At 10 bits per character, that is 90 bits.

90 bits in 400us is 4.4us per bit, which means you need a bit rate of at least 228 kbit/s.

Interesting.
I’ve used a baud rate of 1,000,000 on the mega2560 without any problem. Unfortunately I can’t get Serial Monitor to accept it as a valid baud rate.

Using something like RealTerm, it’s no problem.

OK i read Robin2 post and i came up with the following code,at 9600 baud rate the error was increasing but i increased the baud rate so the difference in reading was 764 almost constant

void setup()

{
Serial.begin(115200);
}

unsigned long present;
unsigned long prev=0;

void loop()
{
present = micros();
time();
}
void time()
{
if (present - prev >= 400)
{
prev+=400;
Serial.println(present);
}
}

I think the issue here is baud rate nthng else

KenF see this, i run ur code at arduino uno with 115200 baud rate at serial monitor
Plz see the attached picture

Do you have access to an alternative serial terminal emulator? The serial monitor that comes with the arduino ide can only go so far. If you use something else that will accept a baud rate of 1 MILLION. I'm sure it will iron out a lot of your issues.

The arduino is well capable of that baud rate. You just need a terminal emulator that will accept it. RealTerm does the job fine (and it's free) but I'm sure there are others out there.

Usman93:
I think the issue here is baud rate nthng else

It can't be done.

To display an 8 digit number, plus cr/lf is 10 bytes. At 115200 baud you can send one byte every 1/11520 seconds (every 86.8 µS). So ten bytes will take 868 µS.

So there is no way you can send something, which takes 868 µS to send, every 400 µS.

OK, here is the big question... Why do you want to do this?

X-Y problem

The reason for the sudden increase in times is because for the first several iterations of your loop, you are simply filling up the 64 byte output buffer, which is more-or-less instantaneous. Once the buffer is full, you have to wait for actual transmission time to send more data.

And even if you could use a baud rate of a million, so what? The Arduino would be spending all its time sending these numbers and doing nothing else. May as well just make a spreadsheet with the numbers in it, and leave the Arduino out of it altogether.

That would account for nearly every third question or so. :o

Guys thanks for your help
Well my job wasnt exactly to println each present time reading

I am actually making an energy meter for that i needed instantaneous current and voltage readings each after 400us

so i only needed the 400us delay

What are you measuring at 2500 Hz? (1/.0004)

The more this Thread extends the more I think that you are likely to be going the wrong way about whatever you are doing.

Tell us what you want to achieve and not how you think it should be done.

...R