Arduino IDE - loop() What happens?

Status
Not open for further replies.

irakandji

Member
I am looking for some guidance on where to find detailed information on how an Arduino application works.

I wrote a very simple loop() that toggled a digital pin on/off. I then ran it at different clock speeds on the Teensy 3.6. I used an oscilloscope to look at the output. An noticed some jitter and other unexpected results.

I concluded that something was happening "in between" each execution of the loop. (conclusion might be wrong)

I tried to find some detail on how this worked, but can't seem to find the correct "google search" criteria to find a detailed explanation of what happens or any options to configure what happens.

Some pointers to a good source would really be appreciated.

Thanks in advance
 
Simplest thing to do is to look at the actual code!

That is if you look at where you installed Teensyduino. Example on my machine I have 1.8.2 installed at the root of D: drive.

So looking at the file: D:\arduino-1.8.2\hardware\teensy\avr\cores\teensy3\main.cpp
You see:
Code:
#include "WProgram.h"

extern "C" int main(void)
{
#ifdef USING_MAKEFILE

	// To use Teensy 3.0 without Arduino, simply put your code here.
	// For example:

	pinMode(13, OUTPUT);
	while (1) {
		digitalWriteFast(13, HIGH);
		delay(500);
		digitalWriteFast(13, LOW);
		delay(500);
	}


#else
	// Arduino's main() function just calls setup() and loop()....
	setup();
	while (1) {
		loop();
		yield();
	}
#endif
}
So the thing that is called between your loop calls. is yield:
The yield function looks at every possible Serial class you have and checks to see if it has any Serial data on it and if so calls the Serial Event...
Code for yield is in the same directory in yield.cpp

Hopefully we can remove some of the default overhead of it, but you can easily do it by adding a dummy yield function to your code.
Something like:
void yield() {};
 
Fantastic!!

I see how this works. So is the core code specific to teensy? I am trying to understand how to make changes properly so not to run into version control problems down the road when pjrc updates the core.

Time to dig some more .. thank you
 
The code KurtE linked to is part of and unique to each install of TeensyDuino / TeensyInstaller - and yes a CORE file - overwritten on each new release/install. That part shown doesn't typically change and is similar in net effect for other Arduino systems. This is true of any file in the "D:\arduino-1.8.2\hardware\teensy\avr\cores" directory.

Making your own project local "void yield() {};" will always work to replace the default yield code when you compile locally as the CORE copy has a 'weak' attribute which tells the linker to use your copy when provided without complaint. This is fine to do if you never use the serialEvent() feature of Arduino. If you process Serial data you can always add that to your own loop() as needed - or even to you local copy of yield() as you see fit.
 
Status
Not open for further replies.
Back
Top