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
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;
}
}
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.
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
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;
}
}
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.
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
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?
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.