I got a arduino mega hoping to make a solution to do some serial data conversion. I'm polling one device every 300ms (over serial at 57600), doing some math, and updating another device (over serial at 19200) every 100ms. At least that's what I was hoping to do.
When I look at how often the 100ms updates are going out, it's rarely less than ~350ms. I'm using the following in my loop() for my 100ms updates, yet they are not happening that frequently:
nextOut = lastOut + 100;
if (nextOut < millis())
{
sendOut();
lastOut = millis();
}
This comes after the logic for the 300ms poll, which uses a similar if statement as above but with a 300ms interval.
Am I doing something wrong? Is there a better way to do this? Or am I just asking too much from the arduino?
AWOL - Yes, lots of serial writes. print()'s actually. The 300ms loop is polling and serial.read()'ing the data, the 100ms loop is doing serial.print()'s. Is that part of the problem? Any way to fix it?
At 19200 baud, every character you print takes nearly 500us.
"print" doesn't work in background (i.e. it isn't buffered), so your program slows by 500us for every character.
You could try a higher baud rate.
In the other (300ms) loop, I'm sending/receiving quite a bit more.
It sounds like something (a print?) in your 300ms loop is causing it to block (for ~300ms) rather than letting the 100ms loop run... Hard to say, without seeing more of the code.
westfw - you were right! thank you so much! i had a bunch of serial.prints at 9600 for debugging. changed it to 115200 and i'm blazing along now! wonderful!