Hello and welcome back to the blog Waiting for the Bus.
I always get requests for help on programming aspects related to union more sketch that control different modules such as LCD, LED display , Bluetooth, Wifi, modules with relay, etc ..
Obviously the individual sketch alone work fine but, together, are incompatible because they use the same PIN Input / Output or simply were not designed to work with other modules.
Normally a programmer encapsulates the code in libraries or functions, but this is not always easy.
Today I'll show you how to integrate the seven sketches for the module with 14 LEDs in one sketch.
We start with the project for the control of 14 LEDs that are here.
Well, let's see what changes we need to make the sketch to operate the various sketches in sequence one after the other. First let me remind you to take a look at the first lessons of programming that I published on the blog and you find here. Then very quietly we reason about changing a sketch to add more games.
The first idea, which is also the simplest, is to add all games to the Calc spreadsheet and paste them as data in one sketch. Easy and fast.
In doing so we went around the problem quickly but we have not learned anything.
We return to our sketch: everyone has a common part and a part that is linked to the game that you want to do.
We return to our sketch: everyone has a common part and a part that is linked to the game that you want to do.
The first step is to better encapsulate or enclose the variable in a function or procedure.
But what are the functions or procedures?
A few examples: the mother asks his son to buy a liter of milk and bring the rest. Mom is sure if everything went well because the child bring in the rest will also tell you if everything went well (there's the rest) or anything else (milk finished). This is a typicalfunction .
Another example: The mother has to go out and asks his son to buy milk. Mom does not expect her son to communicate something on the purchase of milk and discover only when you return if there is or not the milk house. This is a typical procedure .
How to recognize the functions and procedures in C ++?
Easy, if before the name of the function VOID then there is a procedure, or a function.
But what does it come back? Just see the return command () at the end of the function.
In our case this is how you will present the sketch enclosed in a procedure ( notice the void):
![]() |
Fig. 2 - Sketch B put in a procedure of Paul Luongo |
As you will see there is the variable part of the sketch of the base with the numerical values of the game that we want to achieve and there is also a new procedure LanciaICodici show that deals with the individual combinations that make up the game.
![]() |
Fig. 3 - LanciaICodici () - How the new sketch of Paul Luongo |
A note for the uninitiated: notation values [] refers to a collection of values or arrays that are stored in sequence ( in the jargon is called vector ). The number that is put in brackets [] indicates what value we want. In our case we used a variable i that will assume all values between 0 and nro minus one because it is controlled by the for.
I forgot ... in C ++ carriers start with the place 0 (zero), unless otherwise indicated.
After repeating the job done in Fig.2 for all the sketches, it's time to change the main program:
![]() |
Figure 4 - Changes in the main program of the sketch of Paul Luongo |
At this point all that remains is to load the new skecth Arduino UNO R3 and admire the result of our efforts.
Post a Comment