08 Multitasking in Arduino using millis | Servo Motor and 7-segment Display | Embedded Systems

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi again and welcome to embedded systems course prototyping with arduino uno in today's topic you are going to learn how to work with servo motor by controlling its speed and direction also you will learn how to create a program to manipulate a seven segment display and understand the main difference between a common anode and a common cathode design more importantly you will learn how to control several components almost simultaneously or should i say multitasking by using the millis function instead of a delay at the end of this lecture you should be able to create your own prototype that controls a seven segment display a servo motor leds and they switch simultaneously without using the delay function so let's begin in this activity we will be needing the following materials a seven segment display two leds nine 220 ohm resistors a tactile switch a 10 kilo ohm resistor a servo motor i'll be using an sg90 micro servo a couple of connecting wires breadboard and your arduino let's start with the seven segment display the seven segment display consists of seven leds arranged in a rectangular fashion each of the seven leds is called a segment because when illuminated the segment forms part of the numerical digit to be displayed an additional eighth led is sometimes used within the same package thus allowing the indication of a decimal point when two or more seven segment displays are connected together to display numbers greater than nine each one of the seven leds in the display is given a positional segment with one of its connection pins being brought straight out of the rectangular plastic package this individual led pins are labeled from a through g representing each individual led the other pins are connected together and wired to form a common pin so by supplying voltage to the appropriate pins of the led segments in a particular order some segments will be light and others will be dark allowing the desired character pattern of the number to be generated on the display these then allow us to display each of the 10 decimal digits 0 through 9 on the same 7 segment display the display's common pin is generally used to identify which type of the 7 segment display it is since each led has two connecting pins one called the anode and the other called the cathode there are therefore two types of led seven segment display called common cathode and common anode the difference between the two displays as their name suggests is that the common cathode has all the cathodes or the negative pins of the seven segments connected directly together and the common anode has all the anodes or positive pins of the seven segments connected together in a common cathode display all the cathode connections of the led segments are joined together to logic zero or ground and the individual segments are illuminated by applying logic 1 or high signal by a current limiting resistor to the anode of the particular segment a to g however in a common nano display all the anode connection of the leds are joined together to logic one the individual segments are illuminated by applying a logic zero or ground signal via suitable current limiting resistor to the cathode of the particular segment a to g depending upon the decimal digit to be displayed for instance to display the numerical digit 5 we will need to light up 5 of the led segments corresponding to a c d f and g and this is how the digits from 0 through 9 can be displayed using a 7 segment display thus we can produce a through table giving the individual segments that need to be eliminated in order to produce the required decimal digit from 0 through 9. for this illustration i will be using a common cathode 7 segment display so it means i will be connecting the common pins of the 7 segment display to the ground however if you are using a common anode 7 segment display the common pins must be connected to the positive 5 volt supply although a 7 segment display can be thought of as a single display it is still 7 individual leds within the single package and as such these leds need protection from over current so this current must be controlled and limited by a safe value by an external resistor to prevent damage to the led segments i'll be connecting a 220 ohm current limiting resistor to each individual segments now a huge question is that how do you know if what you have is a common anode or a common cathode seven segment display well unlike with a single led wherein you can easily spot the anode and the cathode pins in a 7 segment display we have to test it so for this example if i assume that this is a common anode the common pins must be connected to the positive 5 volts and to test each individual segments i am going to connect any of these segments to logic zero or ground if it emits light then we are correct if it doesn't it means what we have used is not a common cathode but instead a common anode 7 segment display and therefore we need to reverse the connection by placing the common pins to the ground and each individual segments now requires logic 1 to emit light in this illustration you can see that segment b emits light so if we want to let's say display digit 4 then segments b c f and g must be supplied with logic 1. [Music] likewise to display decimal number 7 segments a b and c must be supplied with logic 1. of course it is not practical that we have to manually connect each segment every time we want to change the display well commonly 7 segment displays are driven by a special type of integrated circuit also known as seven segment decoder there are many binary coded decimal or bcd27 segment decoder ics available including the very popular ttl 7447 and 7448 to drive both the common anode and the common cathode respectively but for this demonstration we are going to use seven of the arduino's digital pins and write a program that acts as a decoder circuit to drive the 7 segment display for this i will be connecting pin 13 to segment a pin 12 to segment b pin 11 to segment c pin 10 to segment d pin 9 to segment e pin 8 to segment f and pin 7 to segment g let me replace these wires in the common pins with this small connecting wire so it doesn't block the view of our display [Music] in the setup function i'll create a for loop starting from i is equal to 13 down to i is equal to 7 then i'll use this variable i to set the pin mode of pin 13 to pin 7 as output in the void loop if i want to display a decimal number 4 i can simply do this [Music] as you have noticed segments b c f and g are supplied with logic 1. these are the only segments that should emit light and will cause digit 4 to be displayed let's upload the code and see what happens although it works but if we are going to display digits 0 through 9 with this kind of coding approach it will be very inefficient and hard to manage let me modify this code and i will declare an array and give it a name digit4 i'll set its content to 0 1 1 0 0 1 1 this should correspond to segments a b c d e f and g for index 0 to 6 and now in my void loop i'll replace this entire bunch of code with a single for loop similar to the void setup on how we set the pin mode of pin 13 to pin 7. the main difference is that i'll be using digitalwrite function to write to each pin is starting from pin 13 which is segment a the array content that corresponds to this segment so for pin 13 i need to access index 0 for pin 12 i need to access index 1. for pin 11 i need to access index 2 and so on and with that i'll use the array digit 4 and access its element pointed by index 13 minus i it only means that as it loops through the values of i from 13 down to 7 this code is trying to access index 0 to 6 instead let's try uploading this one to see if this new code will still accomplish the same thing of displaying digit 4. and there you have it [Music] let me copy paste this array and change it to digit 5. i'll modify the corresponding segments c d f and g to emit light after displaying digit 4 i'll pause for a second and then copy paste this entire code block and change the array being accessed to digit 5. let's upload it again and as you can see digits 4 and 5 are being displayed in a loop at one second interval but even though we have used an array for this you will notice that the similar lines of code will still be repeated at least 10 times if we are going to display digits 0 to 9. so for that i am going to modify our data structure one more time and use a two-dimensional array instead i will call it digits with 10 rows and 7 columns this represents 10 digits for the rows each digit has seven columns that represents the segments for each digit so for digit 0 only segment g is turned off for digit 1 only segment b and c are turned on for digit 2 only segments c and f are turned off for digit 3 only segments e and f are turned off and if we refer to the previous truth table we can complete this 10 by 7 two-dimensional array of digits i no longer need this single dimensional arrays similarly i'll remove this duplicate for loop that displays digit 5 and then to access a 2-dimensional array i'm going to create an outer for loop i'll use variable d that loops from 0 to 9. this represents the row so for each digit starting from digit 0 i need to loop through the 7 columns that represents the segments so to access column 0 to 6 i'll use the array digits at index d as the row and 13 minus i as the column and that's all that we need to do after displaying digit 0 i'll pause for 1 second before it loops back and increment d by 1 and repeat the same process to display digit 1. this nested for loop will finish once d reaches 10. but since this code is inside the void loop it will be repeated again and again and again let's upload the code and check the output [Music] arduino is a simple microcontroller based platform without a concept of operating system this means that only one program can run in arduino at a time but even though there is no operating system to manage multiple tasks we can still achieve the concept of multitasking which means we can implement complex projects involving several components like 7-segment display switches sensors motors leds servo motors etc to perform multiple tasks simultaneously in order to handle multiple tasks in arduino you need to understand the concept of millis to understand melees consider the following illustration let me add a push button switch to our existing circuit i'll use a 10 kilo ohm pull up resistor here and use the digital pin 2 of my arduino suppose that i want to reset our digit counter to 0 when i press this button switch at any time using our current program structure you might do something like this remove the local variable declaration d inside the for loop and place it outside making it global [Music] and then after this nested loop i'll check if my digital pin 2 is pushed to the ground and if so i'll reset d to 0. let's upload the code and see what happens [Music] notice that even if i press the switch nothing happens i intentionally place this if statement after this nested for loop for you to see that this if statement will not be executed until such time this nested for loop is done executing the main culprit here is not actually the looping statement but the delay function when you use the delay function in arduino you are actually putting the processor in a base state or on hold for the time period specified in the delay parameter during this time the arduino processor cannot do any other tasks like read signal from a push button for example and if you want to achieve multitasking and arduino you have to get rid of the delay function and start using millis instead to illustrate it i'll remove this outer for loop and use this void loop instead i'll declare two unsigned long variables named start time display with initial value of 0 and interval display set to 1000. inside the void setup i'll configure my serial monitor then inside void loop i'll create a local variable current time and store the millis return value whenever we call the millis function in your program it returns the time in milliseconds from the moment the program started running nilis is a time keeping function that starts when the arduino is powered on or reset for you to see this i'll use the serial.print line to display the current millis in the serial monitor i'll comment this code first and then let's upload the sketch then i'll open my serial monitor and as you can see it started counting upwards returning the current time in milliseconds since the program started running so how do you make use of this current time in milliseconds i'll uncomment this code and then [Music] i'll check whether the current time when subtracted with the starting time which is initially started at zero is it already greater than or equal to the time interval this is actually the elapsed time that will act as our delay in other words this statement simply asks if it is already time to perform the task by which in this one to display a digit to our seven segment display and this will only happen after every 1000 counts or 1000 milliseconds is specified in our interval variable now since i remove the outer for loop here i will manually increment this variable d so that it can access the next digit after every loop and then the next important part of this code is to set the new starting time which is the current time during the time that this code block is being executed this ensures that every time you attempt to subtract the current time with the starting time this if statement will be true when it is already equal or greater than the interval which is 1000 milliseconds now let me just add a logical operator here and check whether variable d is already 10 and then it can be reset to 0. let's upload and validate the output as you can see we still achieve the same digit counter at 1000 millisecond interval for each digit without using the delay function more importantly without using the delay function we can perform multitasking you will notice now that when i press the push button switch at any time our code responded to reset the content of variable d to zero so to further illustrate the concept of multitasking i will be adding another component to control simultaneously i'll take this opportunity to discuss how to work with a servo motor servo motors are self-contained electric devices that rotate or push parts of a machine with great precision servos are found in many places like from toys to home electronics to cars robotics and airplanes if you have an rc model car drone or helicopter then you are definitely using at least a few servos so how do servo motors work servo motors are controlled by sending a pwm or pulse width modulation signal to the signal line of the servo the width of the pulses letter means the position of the output shaft when you send the servo a signal with a pulse width of 1.5 milliseconds the servo will move to the neutral position which is 90 degrees the minimum which is 0 degree and the maximum which is 180 degrees this typically corresponds to a pulse width of 1 millisecond and 2 milliseconds respectively note that this can vary slightly with different types and brands of servo motors many servos only rotate through about 170 degrees or even only 90 degrees for some but the middle position is almost always at 1.5 milliseconds servo motors generally expect a pulse every 20 milliseconds or 50 hertz but many remote control servos work fine in a range of 40 to 200 hertz although most rc servos are from 180 degree variety which means they can only rotate in a range of zero to 180 degrees however continuous rotation or also known as 360 degree servo motors are also available for this demonstration i am going to use the sg90 micro servo which can rotate from 0 to 180 degrees so how do we connect a servo motor to the arduino wiring a servo motor is very easy because you only need to connect three wires the power ground and the signal the power wire is typically red and needs to be connected to the positive 5 volts a micro servo like this sg90 consumes around 10 milli ampere when it is idle and 100 to 250 milliampere when rotating so you can power it directly with the 5 volt output of the arduino the ground wire is typically black or brown and should be connected to the ground pin of the arduino the signal wire is typically yellow orange or white and it can be connected to any of the digital pins of the arduino in this case i'll connect it to the digital pin 6. to control the servo motor we will be using the servo.h library which comes pre-installed with arduino ide then i'll create a variable of type servo i call servo1 in the void setup we need to tell which pin is assigned to this servo1 variable by calling the attach function in this case pin 6 then to position the servo to a specific angle just call the right function and pass in the desired position in this case i'll pass 180 degrees let's upload the sketch and see what happens and there it is your servo moves to 180 degree angle let's try changing the angle to 90 degrees upload the code again and there you have it one last try let's try changing the angle to zero degree and upload the code again [Music] and that's it now let me write this three different positions 0 90 and 180 degrees sequentially but for the meantime i'll use the delay function of 1000 millisecond waiting time to set each position of the motor i'll upload the code again and now what have you noticed even though the servo motor and the digit counter work but they don't execute simultaneously based on their expected time of execution after one digit count the motor must finish the position from 0 to 90 to 180 degrees consuming 3000 milliseconds first before looping back to perform the displaying of the new decimal digit so in effect our digit counter is no longer counting up at an interval of 1000 milliseconds but rather its execution is put on hold until the 3000 millisecond delay is finished before i integrate the concept of midis here let me modify the code for servo positioning instead of jumping from 0 to 90 to 180 degrees i'll create a for loop to change the position of the servo motor starting from 0 degree to 180 degrees incrementing 1 degree every 10 milliseconds [Music] and then i'll copy this entire for loop and change the servo position starting from 180 degrees down to zero degree decrementing one degree for every 10 milliseconds i'll upload the code and as you can see a smooth sweeping effect of the servo motor from 0 to 180 degrees and vice versa but then again this doesn't change the problem that we faced before when dealing with multitasking you will notice that the program must first finish one complete sweep of the servo motor before it loops back to increment the digit counter i'll change the servo motor delay and make it 20 milliseconds so that you can see that this greatly affect the digit counter even though this one uses millis so now if we want to accomplish multitasking meaning the servo motor the switch in the seven segment display can execute its operation independent of the time from each other we must not use the delay function but instead we'll use millis i'll declare two additional variables that hold the start time and the interval time for the servo motor i'll set the interval to 20 milliseconds i'll also declare another variable servo angle and set it to zero i'll remove this for loop that controls the position of the servo motor and then i'll replace it with a condition similar to the one that we use in the digit counter i'll check if the current time when subtracted with the servo start time is it greater than or equal to the servo interval which is 20 milliseconds because if it is it is time to move the servo to the position is specified by the servo angle variable i'll use the servo angle variable which i have initialized to zero and increment it by one after then i'll check when it reaches 180 degrees i'll reset it to zero now don't forget to set the new value for the servo start time which is the current time so that this code block gets executed after every 20 minute seconds let's upload the program and check the result and based from what we have observed the servo motor and the seven segment display execute simultaneously and independently the servo rotates at an interval of 20 milliseconds for every degree and the digit counter counts from 0 to 9 at an interval of 1000 milliseconds and the switch can be pushed at any time to interrupt the digit counter and reset its value to 0 without affecting the servo motor's operation we can try reducing the speed of the motors sweeping to 50 milliseconds i'll upload it again and as you can see the servo is a lot slower but the digit counter still counts every one second [Music] let's try changing the servo's interval to 100 milliseconds and test it again together with the [Music] switch [Music] now let's try changing the seven segments time interval and set it to 300 milliseconds i'll upload it one more time [Music] and there you have it the digit counter counts every 300 milliseconds while the servo motor still rotates at a slow rate of 100 milliseconds per degree although working with nilis is a lot to take in at first in comparison with the delay function which is a lot easier but you'll get used to it if you can understand this concept very well you will no longer be using the delay function for most of your tasks that require simultaneous execution and now for your code and circuit connection challenge i challenge you to create a similar prototype consisting of a 7 segment display a switch a servo motor and an additional 2 leds green and red the digit counter counts down from digit 9 to digit 0 while the servo motor rotates synchronously from 0 to 180 degrees as the digit counter counts down and the servo rotates the green led blinks at 500 millisecond interval when the operation resets either by pressing the switch or reaching the count to zero the red led turns on and repeat the process again and again thanks for watching if you learned something of value here please click the like and subscribe button for more programming and circuit tutorials this is joey go and hope to see you in the next video lecture
Info
Channel: Education is Life
Views: 7,523
Rating: 4.9747634 out of 5
Keywords: Servo, 7-segment, seven segment, common anode, common cathode, PWM, multi-tasking, multi tasking, motors, millis, delay, sg90, micro servo
Id: 0l4yJwmXvUA
Channel Id: undefined
Length: 28min 40sec (1720 seconds)
Published: Mon Nov 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.