Need Delay for 400 mirco seconds

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

I m trying to make simple energy meter that keeps track of kwh

At pakistan frequency : 50hz

I need large number of reading for current and voltage per cycle, to measure the instantaneous power, from which i can calculate average power.

so taking each reading after 400us for 50Hz means getting 50 reading per cycle (good accuracy)

Earlier i was using println command to show the time just to confirm, but then the baud rate issue came up.
But naturally my current, voltage or power reading will have few characters so printing current, voltage and power will not require much time (because few characters).

Usman93:
so taking each reading after 400us for 50Hz means getting 50 reading per cycle (good accuracy)

I presume you will be doing the averaging on the Arduino so you will only need to send data to the PC maybe 4 times per minute, maybe less.

You will need to ensure that whatever you send to the PC takes a lot less than 400us on any occasion if you wish to keep measuring while you send. You could send a full set of data as several small parts.

...R

I m trying to make simple energy meter that keeps track of kwh

As a suggestion, an energy meter IC can be had for a few $, then this could be interfaced to the Arduino. Many to choose from ... even Atmel makes them.

dlloyd can you plz provide the link for energy meter IC. (with details for interfacing with arduino)

Robin2 yeah obviously i wont send the power reading at each iteration.

like 50 readings made a cycle

if i take readings for like for 20 cycles i-e 1000 samples and add all the samples and then divide them by 1000, so this would give me the average power which i can send via println and i guess printing some bytes once in a 1000 iterations wouldnt make any error.

Robin2 your suggestions will be appreciated .

THANKS