Arduino Processor Loading and Baud Calculation

Hello

I am just trying to determine what the loading on my arduino processor currently is, as I want to know if I have enough 'cpu' time to do some more stuff.

Basically I have the following bit of code at the start of my loop, to capture the current time and compare it against the time of the last loop, so I can see the time per loop effectively.

unsigned long Time;
unsigned long PrevTime;
unsigned int ScanTime;

void loop() 
{
  PrevTime = Time;
  Time = micros();
  ScanTime = Time - PrevTime;
  */blah blah/*
}

I also have some LCD code, and some Keypad code, and also sending some data (Sine wave values at the moment) to the serial port at 9600 baud, every loop. The Sine Wave values are just for the sake of having data to send to the serial, this will be replaced with the data I talk about at the end of this speech!

What the scan time is telling me is it is taking approximately 30ms per loop.

I just wanted to run a few calculations I made by you to see if I am correct...

30 ms/loop = 0.03 seconds/loop
1 / 0.03 = 33.3 loops/second

16Mhz CPU = 16,000,000 'cycles'/second
16,000,000 / 33.3 = 480480.48 'cycles'/loop

That is quite a few...

The next calculation is how much data I am sending over the serial link.
I have it set on 9600 baud, which is 9600 bits/second.

Each loop of my sketch, I am writing up to 20 characters onto the serial port. Whether or not this is correct or not, im not sure... but 20 characters is 20 bytes, which is 160 bits.
I am currently running 33.3 loops/second, and therefore:
160 bits * 33.3 times second = 5328 bits/second

So in theory I am within my limits of 9600 baud. Is this correct?

If I was to halve my scan time however, by say taking out my LCD and the print statements to it, then my loops/second would increase... so say 15ms/loop now, which is 0.015 seconds/loop, which is 66.6 loops/second. So therefore 160 bits * 66.6 = 10666 bits/second.
So does that mean that 9600 baud will no longer cope with the amount of data I am sending it?

Is this how you calculate this all out?

What I want to add onto the project is 2 high speed pulse trains into the external interrupts. One of the pulse trains is going to be coming in at about 340 pulses per second.

Is this going to cope..?

Sorry for the long post, but hopefully someone will spend the time to read and point me in the right direction.

Thanks

Is this how you calculate this all out?

Close but I wouldn't break down the serial into bits, I
don't think it adds anything.

That is quite a few...

Most of them are spent hanging around waiting for the serial buffer to have some space in it.

So in theory I am within my limits of 9600 baud.

Not sure what you mean by "within limits"

Is this going to cope..?

It depends on what the ISR is going to do.

The biggest drag is the serial output, find a way to cut down on that and things will improve. However the arduino is only a 16MHz processor with limited memory so any audio processing you can do will not be very good.

Audio processing?
Im not doing anything with audio frequencies - merely counting pulses to determine RPM, Km/h etc.

The ISR is only incrementing a count, which is then processed in the main loop.

RE the baud rate, if I have 20 character bytes to send each loop, then how is it I calculate which baud rate I ideally need to handle all the data, without overflowing the buffer etc..

Thanks

Audio processing?

Sorry misread the title.

You won't overflow the buffer because you are sending stuff. You will just extend the time it takes to send them if the data rate is higher than the baud rate will give you. That is once you reach the optimum serial speed making the baud rate even faster will not decrease the time your loop takes.

Ok, I understand.

The Serial.print statement, is it possible to group a bunch of data all together and then send it using one print statement, rather than having to have say 10 print statements, one after each other?

ie in Bascom AVR, you can do the following:
Print Command1 ; Command2 ; "," ; Value ; "," ; Value2 ; "," ; Index

whereas in Arduino IDE it seems I have to do the following:
Serial.print(Command1);
Serial.print(Command2);
Serial.print(",");
Serial.print(Value1);
Serial.print(",");
Serial.print(Value2);
Serial.print(",');
Serial.print(Index);

I dont know though if multiple print commands is slowing the processing down, or if its purely the fact that I am actually sending more data?

I am just trying to figure out how I should do this, as I need to sample these pulses as they come in, then perform a calculation on them, and then send the result out to the serial port. But I just dont know if I am doing it the most efficient way or if there is a better way to do it...

hmm.

is it possible to group a bunch of data all together and then send it using one print statement

Basically no, and even if you could it wouldn't save you any time as the serial transmit time for a byte far outweighs any slight time saving in the overheads of calling a routine.

or if its purely the fact that I am actually sending more data

That's the one.