Hi, welcome to an Arduikno tutorial from Robojax. my name is Ahmad Shamshiri.
I'm presenting this tutorial from Canada. in this tutorial we are going to learn
how to use the Millis or a millisecond in Arduino programming. let's get started
with this.
so what is millas and why we have to use it? first missil() is a internal
function from Arduino that calculates the time in millisecond from the
starting point when Arduino program runs. as soon as you power up the Arduino
the missli() will start with zero and it goes up in millisecond for one second
you will read 1000 and for 5 second you will read 5,000 and so forth.
but it can be accessed throughout the code any point for example you can
record a start of a point and then stop stop of a point at any moment for
example let's say somebody presses a push button or temperature increases or
something is detected you can say I'm counting from this point until some
other event happens or until you want it. for example I push button is pressed and
in 5 seconds you want to do something from the time the push button is pressed
you can save the millis() in a variable and then you can check it continuously
and then you see when it is 5,000 millisecond or 5 seconds then you can
take an action. many times people will need to use delay the function delay
with 5000 millisecond and it will be 5 second delay but the problem with the
delays that it will halt all program. everything that you have everything will
be waited until the delay passes then they will continue. but with millis() it
will not interfere with any other feature that you have and you can
calculate the time. and the rest of the program or components that are connected
to Arduino they all function normally. now let me show you practically on
Arduino. and here I've defined the serial monitor with 9600 baud so it can display
the value on the screen for me and this is just a text and I'm waiting for 2
second here using the delay. to use millis() we type millis M I L L I S and open and
close parentheses (). this means it's a function with an Arduino. but this is
just a value at the moment that our dinner starts and it will increment
every millisecond now it will be printed for you first
I've written this code like this using serial.println println mean new line so
it will print the millis value and we'll create a new line. the speed of how fast
this is printed that now depends on a serial monitor because it has to read
the value from Arduino and bring it back on the screen for that reason this will
delay it but we will see some value and I did not put any delay here so you will
constantly see the values and I'm gonna stop it so we can review it I'm opening
the serial monitor from here but you can click on tools->serial monitor or you
can use a control shift + m to open it.
this is 2 seconds and that's it it will start automatically let me stop it and
here as you can see the scrollbar is going crazy it's continuously printing
let's see here automatically it was two seconds here
remember I put that 2 seconds delay the program start running and after 2
seconds prints 1 9 9 9 because 0 is also millisecond the first millisecond is 0
and then 1 & 2 and after that it prints it very quickly because the serial
monitor printed very fast and slowly you see here 38 and this has been printed a
little late for millisecond late late and then from 4 and this is a longer and
from here to here 6 millisecond so you got the idea that this is the time from
the beginning now it's 52 seconds and it continuously will print if I leave the scroll on the
scroll on you will see that the 60 seconds 1 minute so continuously prints
the value you got the idea.
Example 1
the value for the millis() is unsigned long (data type) that's very important because if you try
to compare it with an integer you will get error according to the Arduino
website so I'm defining an event let's say you want to take some action after
five seconds we are defining it as a type long and we put unsigned which
means it doesn't have negative or positive so it has no sign. unsigned long
event this is our variable and the value for that is five thousand five thousand
millisecond or five seconds and here we check from the start at zero and it
increases until it reaches equal or greater than "EVENT" if an it is five
bigger than five thousand this will be printed and let me open and as you can
see these are millisecond one second two and as you can see it shows event
occurred I'm disconnecting it from USB and let's see at five thousand four
thousand nine hundred and ninety five and this occurred when it was five
thousand and you can see next is five thousand and sixteen and forever it will
be. so this is one way that you can remember and the beauty of this one is
that you can do any other tasks that are within the loop they will continuously
operate and this will not interfere with them and yet you can detect that time. if
we do not use the missli() then our option would be to have delay for
example you want to take some action you can say delay is five thousand and then
set your print like this and you don't have this one this will also will be
printed only when five thousand millisecond passes but the problem for
this is and tell this time passes the program will stop here and stuck here.
all the code whatever action that you have below this and this area will
not be executed. so delay is not a good option if you're working with time and
there are other tasks involved. if you don't have anything else to do and the
whole code is waiting for this then would be very simple and it will be
reasonable to use delay but in many applications this will not work and will
be problematic so we will use millis() this was one example
Example 2
and here I've connected this push-button which has two pens like this the left
pen is connected to the ground of Arduino here we have a ground and the right
pin is connected to pin 2. and here is an example for the push button first I've
defined using pen mode and 2 as an input and I also put underscore pull up this
will eliminate usage of a resistor a separate video explaining that the link
will be provided below the video. and here this pin is now ready without the
resistor it accepts when you push the push button that will be detected. and
now the rest of the code is the same I have added this line digitalRead(2)
this is reading pin 2's status either pressed or not pressed. remember this input
pull up means it is pulling up the pin 2 this pin to which that is pin 2 with a resistor
so it positive and it will have 5 volts from this pin 2 the Arduino so doing is
reading always high and we read it and we store the value of whatever is a
result into the PB or push-button as an integer so if it is HIGH it will be 1
if air is LOW it will be zero when the push button is pressed it will be zero.
now we check if PB is LOW, LOW means zero this ==
equal means compare this what this so we are comparing it if it is true which
means if push button is equal LOW then take the action whatever is between
these two parentis so this action will take place this is exactly the same as
before but here this one occurs only as push
button is pressed. what it means is if at the beginning somebody pushes the button
it will not take action until 5 second passes. after 5 seconds this event
that we have defined as before 5 second is here at the beginning until 5
second his past, then we will see this action. otherwise this
push button if it is not pressed the code will not come to execute this now
let me run the code I will just leave it you see this is one second two second
three seconds now if I push the push button you will see the action after
four seven now if I close the serial mental monitor and reopen it again it
will reset the time so I'm pushing the push button I see two seconds three
seconds nothing for and suddenly you see take action when I remove my hand it
stops so let me stop it or maybe disconnect Arduino so the scrolling
stops automatically so pay attention here this is 900 millisecond one second
and as we can see we are at two seconds here three seconds and here four
thousand 4995 milliseconds so it's
almost five-second when it's five second it says take action action takes place
when push button is pressed and the time is more than five seconds. and whatever
other tasks that you have will continuously will be executed it will
not be interrupted.
I have separate LED blink with millis() the link is below this video in the description
Example 3
in this example we are checking the status of a push-button if it has been
pressed twice or more once it is pushed for the first time we record the time in
the button pushed variable and then continuously monitor to see if it has
been pushed after five seconds for example a two three second four second
if it is pressed doesn't matter four point nine second doesn't matter as soon
if it is pressed after five seconds or longer then we can take an action. of
course you can make it more complicated but this is just simple task so we will
check the status of push button if it is in pressed twice or more and here is a
third example what we do is in this one that we push the button and we make sure
the second push occurs after five second when it is after five second then we
take some action so now at this case doesn't matter when maybe after one hour
somebody pushes the first and then the second this push can be signal from a
sensor from something I just made this simple but you can do other tasks. so
first we have defined a variable called buttonpushed this is the time when the
push button is pressed what we do is we set it at zero initially and then after
that as soon as program starts it will never be zero so this is just for our
reference I will show you. then the rest is the same and here after this line
what we check is we check if(pb) button is equal LOW which means if push button is
pressed and and these two ampersand means AND this AND this must be TRUE
both of them. so we check if buttonPushed if equal zero
which means at it it is at the initial state then we save the current
Mellis millisecond at a buttonPushed variable so now zero will be replaced
with this. and then we say first push at this time and we print that time that is
it. so before if I go for the second section
remember that when the loop continues next time yes but the button is pressed
AND if the button is not equal zero this this will not be executed so this will
run only once when we save the push-button value. then we check here
that code will check every button is pressed well AND buttonPushed is larger
than zero which means it has some value then we check here we have a variable
called "difference" what we do is we get the current millisecond which is always
greater because when the time passes it increases subtract the buttonPushed
value from it and we have a difference between the time that was
pushed and the current time and we check if the difference is greater or equal
the event, event is the value that we have is five second then we take some
action which we print okay after five seconds this have been pressed. here this in
this case I am NOT checking if it has been pressed after 6 second seven second.
but for us it's important that it has to be pressed after five second at any time.
of course you can make it very precise for example if it is pressed between
five to seven seconds some actions should take place but for simplicity I'm
just saying every is press after five second this action will take place and
you can put whatever code you want in this area. let me upload the code.
I'm opening the serial monitors as you can see the time is running absolutely
no difference when I press it it will be very quick that you saw that it says
button pushed if I press it nothing happens you see now if it was if it was
5 seconds let me disconnect Arduino so that scream stops let's go and check the
time when I pressed it first these are the millisecond 1.6 seconds so first
push at 5019 millisecond five seconds and 19 millisecond so this was when I
pushed it so if you take five five plus five it should be 10019 after 10019 it
should have effect otherwise a push button should not have any effect and I
press this so many times so this is seven eight thousand nine thousand you
see it in powder and something I have not rested at 10168
millisecond I pressed it and it shows after five seconds of the first push. and
as long as I kept a button pushed you will see these values and after that I
release it.
that it. I hope you've learned this and I have a lot of other videos that use
this in many instances the link will be provided for example I have a timer and
here in this video Robojax Arduino Relay Timer and
V 1.0 I've used it to control and an AC bulb or something with a really
and here is the code maximum time minimum time and let me see if you hear
me less remember time I've used it so it runs and you can press the push button
at any time to reset it or a starter relay the link will be provided for this.
I would like to sincerely thank the following people who have supported me
where patreon
Subtitle typed and corrected by Ahmad Shamshiri on July 10, 2020 in Canada.
I did my part to help you learn. Now it's your turn.
You can subscribe to my channel, thumb up 👍 the video and say "thank you" in the comment section.
The link to my PayPal account is below this video if you want to send me chase donation. Thank you.