So in this lecture, I'm going to be talking about a little bit of an advanced topic for Dwina programming
in general and its goal structures, I'm not here to talk about what is structure in the first place.
It is used to combine multiple variables in one structure.
So is it an array then, remember, an array can hold multiple data in one structure and we call it
an array.
Is it the same?
Actually, it's not, because an array could be an integer array, a character array, a floating rate.
There is a specific type for the array.
However, for a structure, it can hold multiple data types at the same time, so struct can have integers,
floats, characters.
It can also have arrays inside it and it could have a structure inside the structures at the same time.
So it is a structure that could contain multiple stuff at the same time packed into one structure.
OK, so this is how it looks like.
This is how you define it.
Let's say at the top of your sketch, you need to say typedef struct the name of the structures and
then you put a bunch of variables that you need here.
Now where do we use this?
Before I go further in this, I'm just going to give you an illustration on where we use this.
Let's say that you have a sensor and you want to combine all the information about the sensor in one
variable, which is this one.
OK, so first you say typedef struct, you give the name here as a sensor, let's say, and you put
all the variables that are related to the sensor here.
And any time you want to access the sensor, you just type the name of the structure and you can get
access to everything inside all at once.
So first you write typedef struct, then the name of the structure, any name you want, and then you
put your valuables.
Let me give you a more detailed example.
Let's say that I want to create a structure that can hold any sense or type so it could hold ultrasonic,
it could hold proximity sensors, it could all temperature, humidity, encoders, decoders, it can
hold anything.
So you create a general structure and you call it, let's say, sensor in it or sensor initialize.
And you put information like, what is the sensor name?
Let's say here it's ultrasonic.
What is the sensor type?
It's also a character.
Let's say it is an analog.
What is the sensory data?
You can then link this variable to your analog input in order to read all the data of the sensor and
store it in this larger structure.
Then what is the scale unit?
How can I convert the voltage to, let's say, distance or to any tenderized unit?
You can do that also with this valuable so you just type ultrasonic and you can access all the information
about it all at once.
So it's a great way.
In order to organize your code, you don't have to write valuables right and left over there.
You just combine everything you need in one structure and then you pass it in between your code.
So it is very crucial in any real life application that you could program.
I mean, go to any company that works embedded systems, and you're going to see that structures is
taking a huge portion and data organization into code that they are working on.
Let me tell you how you can initialize it.
So first, we have typed all of this type of struct at the top of our sketch.
Then let's say in our loop, we create an instance of the structure.
We call this an instance.
I mean, this is a sensor in it, it could be, as I said, ultrasonic, it could be temperature, it
could be anything.
So here I am creating an instance of sensor in it, and I'm calling it ultrasonic.
Then I'm saying ultrasonic, which is this valuable or instant that they have defined their data element
name here, let's say since her name is Ultrasonic Dee, we are now using the sensor name, then we
are using ultrasonic dot sensor type.
So we are accessing the sensor type and we are initializing it to analog.
Then we have sensor data here.
We I think it could add value here.
We could simply just say another grid, a zero so that we are reading a zero and we are just giving
it to this center of data and it's updating all the time.
Next is the sense of scale unit.
And you are saying it's cool, it's equal to four point two.
Now you can create another sensor in it and this time it could be approximately denser and you can fill
its information than it could be temperature.
So you can put this block multiple times, but for multiple sensors and you just initialize it so you
don't have to say here, ultrasonic type, ultrasonic name, ultrasonic data sensor.
Then you say temperature named Richard Di Tombouctou data.
You don't have to do this.
You just say ultrasonic and you put all the variables that are inside the struct.
Then let's say you put proximity here instead of ultrasonic and you put all the information there.
So here in this case, you have only one valuable, which is the ultrasonic, instead of having four
variables and you can access this ultrasonic as you wish.
Now, we call these data here members of the structure or elements of the structure.
Now, there is another way to initialize this, which is by doing the following.
You could say sensor in it and you're giving it a name that say I'm creating an ultrasonic sensor this
time and you can just open curly brackets like that and just fill the fields as they are ordered right
here.
So name ultrasonic type analog data is 48 and scale unit is whole point to now, depending on how you
want to use it in your code, you could be using this form that is line by line or you could be using
this for sometimes you initialize it using this form, then you modify one of the variables using this
form, like the sensor data here you say it equals to another grid and all of these are fixed and you
initialize them and you initialize them using this form right here.
So, again, you can take this line, copy, paste it multiple times for multiple sensors that say you
have five ultrasonics, you could say sensor in it, ultrasonic one, then two, then three, then four
within five.
You would just rename the name here and you link it to different analog inputs and that's it.