Sequencing events in a loop-Blink once/Fade/repeat

Stefan currently going through your code line by line and commenting when I understand what it does.

Functions can appear in any order. Things that might be distractions or otherwise in the way can be placed "below the fold".

setup() is usually the first thing I read, and needs to be read, usually, only once. I don't really need to care where it is, it's never so hard to find that it matters. It may be conventional to adhere to the structure you get with a new bare minimum sketch, but by no means required.

And I hope everyone is able to spot this purest of nonsense from what is promoted as a reputable learning site:

It is important to have the loop() function in the sketch, even if it is empty, because without it the microcontroller on the Arduino board will try to execute whatever it finds next in memory after the statements in the setup() function have been executed. The microcontroller will try to execute whatever it finds in memory as an instruction, but the loop() function prevents it from doing this by keeping program execution in the loop.

No need to worry that with the setup() at the bottom, the microcontroller on the Arduino board will fall off into whatever it finds next in memory.

Functions return when they reach the end, or when they execute an explicit return statement. Return to from where they were called, no matter where in the source code all those things are.

a7

Surely, the code in main() would be subject to revision if a return from loop() simply began executing 'the next memory location'. Even on this forum, that just might occasionally ellicit a post or three.
Then again, don't keep us waiting - just what does happen when loop() returns to main()?

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

And so there we go. Hazardous? Not so much. Thanks.

Wow.

Leaves ya...breathless.

I had to google for it:

Arduino Sketch: Structure and Flow - STEMpedia Education.

This bit sucks too:

If the loop() function contains two statements, the first statement will be executed first, then the second statement, then the first statement again and so on. Hence, the statements in the main loop will be executed continuously until the Arduino is switched off or reset.

Hello cassellimdc

It seems to be a school assigment, isn´t it?

Or what is current task of the sketch?

No, I am trying to teach myself so I can teach my students.Need a set of tutorials that people agree are indepth and correct-seems some of the things you read are not true. Making two lighting events happen one after the other is way more complicated than I first thought and I want to go through every step and understand what is happening. Many tutorials start you out with delays but then you find a community that says never use them and then the frustration starts to grow.

deleted

complicated means composed of many pieces, and I think that's a good thing -- to break down what you're trying to do into pieces and solve each piece independently.

i think many people (programmers) try to solve everything at once, which may not be bad if it can be done quickly, but i think that approach is often too time consuming and unnecessary (unless the code needs to require less memory and/or possibly time)

i think the key concept in this case was a state machine driven by the timer and completion of each sequences. this approach can of course handle many different sequences of different length.

one of the best books i read to learn to program is The Elements of Programming Style which reviewed small programs in college text books and journal articles that had errors which they corrected and then went on to improve the programs

after participating in the forum for a few years now, there are common problems newbies seem to have which a good set of tutorials could help with

i'm curious what's on your list?

Using delay()s put newcomers on a certain track.
That - as soon as they want responsive code -
repsonsive immidiately to button-presses
having learned how to use delays makes it harder to switch over to non-blocking timing.

Using delays means to consequently think sequential low-speed-looping on low-levels
which is diametral opposite to
thinking high-speed looping on the top-level

And as soon as you want responsive code the delays must be replaced by nonblocking timing.

You would be a coding hero if you teach your students right from the start non-blocking timing high-speed looping on the top-level.

This term "high-speed looping on the top-level" is not a common term in programming.
It is created by me. But it nails to the point what non-blocking code is doing.

It will take you additionally 5 to 20 hours to learn this basic principle of high-speed looping on the top-level.

I haven't found a tutorial that would concentrate on this aspect with everyday analogons to make it easy to understand.

I have some ideas about it but haven't written a tutorial myself about this yet.

A very important part is non-blocking timing based on function millis() as described in this tutorial

This non-blocking timing is then used in this tutorialabout state-machines.

If you want easier to apply responsive code you could take a look into the micro:bit-controller which offers a graphical orientated online-programming-IDE with simulator in one place.

This graphical oriented programming takes away a lot of syntax-details from the user and it enables using "parallised" code-execution. (it is not really parallel. The thing behind it is:
from the user hidden away high-speed top-level-looping)

This is a screenshot how it looks like

https://makecode.microbit.org/#editor

So depending on what you want to teach your students the micro:bit might be a better choice