Subroutine

Hello,

For an engineering project, I need to create many programms in only one. ( subroutine)

I had many ideas :

-> insert instruction in libraries and write them in the main programm
-> a function who starts a subroutine

But I don't know how to do it.
Thanks for help (a french beginner)

charpi_:
I need to create many programms in only one.

Well you can only have one, but judging by the thread title, you know that. In this world, subroutines are called functions, read all about them here.

So make each of your sub-programs a function and a write a main program that calls the functions based on some conditions. The Arduino can have only one program installed (loaded).

If you have many ideas, start with the simplest, but better is to start with the examples which are part of Arduino SW.

Have look at how the code is organised in Planning and Implementing a Program

...R

In Arduino Platform, we have learnt to develop a program/sketch based on the following strategies:

1. Identify that there are four separate spaces in an Arduino Sketch/Program.

(1) Global Space : It hold variables that can be accessed by all functions and sub-functions (subroutines) of the program.

(2) Setup Space: It holds variables that will only be used here. It also includes codes (functions/methods) that will be executed only once or for a definite number of times.

(3) Loop Space: It holds variables that will only be used here. It also includes codes that will be executed repeatedly (again and again).

(4) User Space: This space holds sub-programs or subroutines written by the users. These subroutines are called upon from setup() and loop() functions as and when needed.

2. Example of a typical Arduino Sketch

//---------Global Space-------------------
unsigned long presentMillis = 0;

//----------Setup Space-------------------
void setup()
{
    Serial.begin(9600);
    pinMode(13, OUTPUT);
    digitalWrite(13, LOW);
}

//---------Loop Space------------------------
void loop()
{
    digitalWrite(13, HIGH);
    timeDelay();                     //calling subroutine
    digitalWrite(13, LOW);
    timeDelay();;
}

//------------User Space---------------------
void timeDelay()
{
    presentMillis = millis();
    while(millis() - presentMillis != 1000)
    {
         ;                   //wait here
    }
}
//---------------------------------------------

GolamMostafa:
1. Identify that there are four separate spaces in an Arduino Sketch/Program.

There are no "spaces" in Arduino sketches, they are just normal C++ code,
with two special functions being called from main.

And please don't use that "we have learned" stuff, that sound like the hospital "how do we feel today?"
and at least I did not learn about your strange "spaces".

Whandall:
There are no “spaces” in Arduino sketches, they are just normal C++ code,
with two special functions being called from main.

And please don’t use that “we have learned” stuff, that sound like the hospital “how do we feel today?”
and at least I did not learn about your strange “spaces”.

There are spaces everywhere - Memory Space, Register Space, Corner Space, Arduino Space, and the like; if we were looking for technical/literal meanings for everything everywhere, there is no end of it; we need to be pragmatic to accept things conceptually.

I remember very clearly that my tutor, at the very first Arduino class, draw 4 rectangular blocks and labelled them as spaces of which two are (usually) unseen to the users – the Global Space and the User Space. There were literate Arduinians in the class; but, they did not react the way you have reacted.

I don't think there's anything wrong with thinking of the spaces as, well, spaces :wink: but I would shy clear of labeling them as Space with a capital-S which makes it seem like some official C/Arduino naming convention.

Is it just me, or does that timeDealy() function use millis() to while()-away a second just like delay()?

It's certainly not an..

GolamMostafa:
Example of a typical Arduino Sketch

... if it does that.

Sort of blink without delay but with delay?

elvon_blunden:
I don't think there's anything wrong with thinking of the spaces as, well, spaces :wink: but I would shy clear of labeling them as Space with a capital-S which makes it seem like some official C/Arduino naming convention.

There is a style/convention for the creation of a Title: Use Capital letters for all the meaningful words of the title; hence the names: Global Space and not Global space; Setup Space and not Setup space...!

GolamMostafa:
There is a style/convention for the creation of a Title: Use Capital letters for all the meaningful words of the title; hence the names: Global Space and not Global space; Setup Space and not Setup space…!

That sort of pedantry is not helpful for the OP. Please start your own Thread if you wish to discuss it.

Neither (IMHO) is introducing a new range of buzz-words, whether they start with an upper-case or a lower-case letter.

Whatever about drawing attention to global and local scope and to the standard functions setup() and main() there is definitely no other “space” that might justify the name “user space”. There are just more functions.

…R

PS … if one must be pedantic then both setup() and loop() use lower-case first letters - and the C/++ programming language is case sensitive.

GolamMostafa:
I remember very clearly that my tutor, at the very first Arduino class, draw 4 rectangular blocks and labelled them as spaces
of which two are (usually) unseen to the users -- the Global Space and the User Space.
There were literate Arduinians in the class; but, they did not react the way you have reacted.

So what?

Does that justify misusing terminology pimped up in a fancy legit looking enumeration?

User space has a common meaning that shares nothing with your new meaning.

Whandall:
So what?

Does that justify misusing terminology pimped up in a fancy legit looking enumeration?

User space has a common meaning that shares nothing with your new meaning.

Never Mind! The Arduino/C/C++ language will never accept my new terminologies; it is not going to cause any loss/harm to anybody except those who are reluctant to hear new vocabulary.

GolamMostafa:
I remember very clearly that my tutor, at the very first Arduino class, draw 4 rectangular blocks and labelled them as spaces of which two are (usually) unseen to the users -- the Global Space and the User Space.

Perhaps the guy just edited his COBOL:101 notes.

(I don't like the use of capitals to label these "spaces" either)