Welcome back, guys.
So right now, what we're going to do is we're going to start publishing these analog data but but within
certain intervals.
So we're going to be publishing, let's say, every three seconds.
Now, first of all, I just want to define a variable that is from type unsigned long.
And I'm going to call this last publish.
Or last book.
And it's going to be equal to zero.
Why now?
Long is actually an integer, but it can handle more data, but it has more bits.
So if we go now and Google, let me Google here.
Or do we know unsigned long, I'm going to show you how does this data type looks like?
It is a 32 bit for bytes, and since it is unsigned, it does not store any negative numbers.
Now we can say here, unsigned, laundresses, integer.
This is unsigned.
Let us check the unsigned integer.
Well, the unsigned integer is only two byte, so it is actually 16 bits, so the long is actually 32
bits and we use it because we want to store the counter value.
And since this is a timer type of calendar, it's going to be counting for long periods of time.
This is why this counter needs to be holding large numbers.
So we have defined this valuable and now I'm going to show you how we can use it.
I'm going to find another valuable here.
And it is called unsigned.
Long.
And it's called now.
And this valuable stock in the Middle East.
So what I'm doing here, whenever I call this function here, it means that I am reading the internal
timer.
OK, now this M.D. is actually a timer that starts at the beginning of the sketch.
So when you uploaded a sketch and it's running on or doing at the moment, it's a start.
It's going to count in this M.D..
OK, so here what I'm doing is I am reading this timer and assigning it to now.
OK, so the count the process of the timer starts the moment we applaud the sketch and after initialization,
of course.
So there is no need to start the timer on our own.
This is why we only read it.
So by doing this, we are only reading maybe at the moment of reading this we are getting five milliseconds.
Maybe then after we loop a little bit, we will be reading ten milliseconds.
So we are always reading the status of the timer.
Now here is how we are going to do this interval publishing thing.
We're going to do F now minus last pop.
Is larger than.
Something called bubblish time.
Now, we did not define published time yet.
I'm going to define it here.
Let's say publish time is equal to three thousand.
This means three thousand millisecond.
So.
Now, minus last sub is larger than bubblish time, I'm going to say last.
Publish equals.
Do now, and I'm going to open a curly bracket here and I'm going to close it right here.
So what the hell is going on here?
We have already talked about here we are reading and studying the current time or time in now.
Now, if I subtract now, let's say we are in the first iteration, I am subtracting now from the last
bubblish.
OK.
Now, let's publish at this moment is still zero.
Let's say that now is now holding five milliseconds, so five milliseconds, minus zero.
Well, is it larger than published time?
Well, published time right now is three thousand.
And here we have five.
No, it's not satisfied.
It means that I'm not going to be executing this chunk of code at all.
Right.
Because now is still five milliseconds.
And here I have three seconds.
It's not larger than it.
So we are reading the timer again.
Let's say now the timer is 2000, meaning two seconds is 2000, minus zero is larger than published.
I know.
Again, this chunk of code won't be executing and we will keep looping like that until now is larger
than three thousand.
Maybe we can make it larger or equal to.
So this is three thousand minus zero.
Is it equal to three thousand.
Yes, it is.
Now last bubblish is going to equal to now.
What does this mean.
Well now here it is.
Three thousand.
Right.
So the new value of the last published, which is when is the last time I have published a message.
It was at three seconds.
Three thousand.
It's important to do this.
Next, I'm going to be reading this value and I'm going to publish it, and then I'm going to go back
to the beginning of the loop.
Now, this one is reading maybe three thousand and five milliseconds, meaning three seconds and five
milliseconds is three seconds and five milliseconds minus three seconds larger than this?
No.
Well, it's like saying it's a three thousand five minus three thousand larger than three thousand.
It's not.
So this won't be executing.
And then we are going to loop again until this one is six thousand.
Now is six thousand minus three thousand equal to publish time, which is a thousand.
Yes, it is.
It means that less bubblish will take the now and now is equal to six thousand.
Right then we are going to read again publish.
And here we will continue now with this six thousand and five milliseconds.
So see we are only publishing every three seconds.
Now how about we test this really quick.
Let's click on Upload.
We have yeah, we did not define here, it's integer.
It was a plot against.
Again, we have another issue.
Oh, this should be a closing, girlie Prickett.
OK, so for that and we are applauding because of the serial monitor, we are connecting.
If I connected and it is connected now take a look here, let's say I change the potential meter.
See, the change is not immediate.
It takes around three seconds to change the value.
See now.
And this is how we do an interval based publishing.