How do you get a frequency from that?
By looking at the micros() times?
I bet you get different frequencies just by changing the
Serial.begin(speed);
to a higher number - but then you flood the serial interface and bog down the PC (in my experience).
Do you have an oscilloscope? That's a better way.
This will yield a quick frequency:
void setup(){
pinMode (2, OUTPUT);
}
void loop(){
PIND = 0b00000100; // toggle PORTD-bit2 by writing to input register
}
My code in "long stuff" is not relevant. The important is that I just notice a difference between no load and complex calculation.
CrossRoads, I will borrow an oscilloscope to have a better precision, but I just want hove now an idea of the value.
Otherwire, what exactly does your code ? It "anologWrite" without using analogWrite?
I don't know about 150Hz, depends on what your code is doing.
Drop the analogWrite and Serial.prints and just use a PIND = 0b000000100;
for whatever free pin have and monitor that with a scope. I think you'll see some improvement.
150Hz = 6667uS, 6.667mS.
I am writing 9x9x9 LED cube software now, it uses Blink Without Delay style time checking to update the next layer of the cube every 3000uS.
You can easily figure out how long a piece of code takes to run without an oscilloscope if you record the value of micros before and after you run the piece of code for 100 or 1000 iterations.
Don't include Serial.print() within the test unless it needs to be part of the test. Serial.print() is slow.
Anduriel:
My code in "long stuff" is not relevant. The important is that I just notice a difference between no load and complex calculation.
Well...yeah, complex calculation takes longer than nothing. That's pretty obvious. Also, Serial prints on every pass through loop are going to bog down the processor. You can only clock data out so fast on Serial, and once the buffer fills up (and it will), the print statements block until there's room.
tl;dr, Serial prints take a loooooonnnnnggggg time, relatively speaking.
This simple code:
uint32_t previous = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600 );
}
void loop() {
// put your main code here, to run repeatedly:
uint32_t start = micros();
Serial.println(start - previous );
previous = start;
}
Runs loop at 160 Hz, and it's all because of the Serial statements. Get rid of them, and your code will speed way up.
Oh, and as for actually solving your problem, I wanted to do the exact same thing one time, and come up with this solution. Paste this code to the end of your loop():
Every second, it will print to Serial how many times loop() ran in the previous second. Because the Serial calls are much rarer, they shouldn't block and won't significantly affect the code's timing.