Arduino Basics 102: Control Structures, Variables, Interrupts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
During the basic 101 video I taught you with the five fundamental code commands of the Arduino software and what they can achieve once uploaded to Arduino development board and even though it is definitely fun to let an LED blink or determine the voltage on a digital pin we are still pretty limited on what we can do because we are missing so-called control structures most popular or if for wile and switch structures by utilizing if we can perform an action like pulling high digitalPin if a specific condition is met, in this case we double equal sign basically it means equal but there also exists the not equal, less than, greater than less than or equal than and greater than or equal than comparison operators but getting back to topic now if Pin10 connects to ground the led on Pin7 lights up just like we program it to do but after removing the ground potential from the pin the LED is still lits, what we forgot was to add an else structure which pull LED pin to ground when the if condition was not fulfilled and after uploading the new codes and adding a push button to connect ground to the input pin we successfully created our first microcontroller circuits with control structures but at this point you might say we don't need a microcontroller for that and you would be absolutely correct but if we go one step further and include boolean operators it gets a bit more complex in this case the && represents the logical 'and' there also exists '||' for the logical 'or' and '!' for not knots so this time after uploading the codes the LED only lights up if pin 10 and 11 is connected to ground but once again you could say well I'll just take two switching parts and serious to build a circuit without my controller and once again you would be absolutely correct so to prove the usefulness of micro controllers let's remove the simple if condition and replace it with the counter condition in this case the variable I must be lower than the number three in order to LED blink once and at the end of the action we increase the value of I by one which can also be written as 'i++' but while combining the sketch and error message popped up saying that the variable I was not declared yet to fix that we can define global so for the whole sketch above the setup porch hear the word represents a specific variable type known as integer which can hold a 16-bit number other popular types are bool or boolean which can either be 1 / 0 or true / false the long type which can hold a 32-bit number and the flow type which can hold a 32-bit number as well but with a decimal point as an example of the millis function of the Arduino which counts the milliseconds after the start of the codes is stored in a long time variable enters the maximum 32-bit size which is a pretty big number to begin with will be exceeded aka overflow after 49.7 days but getting back to our example by declaring the I variable with the start value of zero in the syrup section d code compiles and upload properly and let the LED blink for three times and then did nothing else perfect and instead of the function and this and additional variable increment line we could simply use the for loop which combines the initialization condition and iteration right at the beginning as expected this sketch does its job just fine as well but as another example for the full loop let's say we want pin 5 7 and 9 as an output now of course we could simply define each individual pin in the setup section but we could also create an array of integers variables which contains the number of the individual pins while defining the rear? The content of the square bracket describes the amount of variables in the array and the numeration of the individual variables always thoughts with a zero so by inserting the number of the required valuable in the square brackets we can select them individually this way by using a for loop which count from 0 to 2 we can define them all as an output without repeating the same line over and over again and while we're at it and don't want to change deep in variables later on we can change the integer type to a constant integer type next we got the while structure which in comparison to if and for structure loop the defined action endlessly until a certain condition is met and then continues with the rest of the sketch as usual and lastly we got the switch structure which can perform different actions dependent on the value of a variable this way we could light up three different LEDs dependent on which pin was pulled to ground through a push-button now let's move away from those learning circuits for seconds and let's have a look at an arduino FM radio letter? builds in a previous project video if we take a look at the codes we can see that there's quite a bit going on so let's imagine we turn the rotary encoder one increments by doing so the two so-called clock and data pins get pulled down to ground one offer the other for brief moments that only lasts around 11.5ms only problem is that your microcontroller was currently sending data to detect LCD and did not notice that the pins were pulled down to ground that is where we use an external interrupts which performs a function when other defined interrupt Pin, so 0 for pin 2 and 1 for pin3 of the Arduino Nano is either low high changes false the ground or rises to the supply voltage doesn't matter whether the code is doing something out at the moment the interrupt always has the highest priority to create an executable function though we can use the void variable type since our function will not return any variables then we simply fill it with what the interrupt is supposed to do and activate the interrupt through the attached interrupt function in the setup section now when other the Arduino pin 2 changes States the led it changes its as well even though there's a long delay happening in the code and with that being said you are now ready to try out your first Arduino projects if you like this video and want to see the final Arduino basics 103 video which we'll discuss libraries weird lines of code in the setup section and more advanced tips then don't forget to leave a thumbs up as always stay creative and I will see you next time
Info
Channel: GreatScott!
Views: 422,173
Rating: 4.9560118 out of 5
Keywords: arduino, basics, basic, 101, 102, introduction, tutorial, how to, diy, make, project, nano, atmega, 328p, control, structure, if, for, while, switch, coding, code, sketch, beginner, guide, variable, type, int, integer, long, float, boolean, interrupt, explain, comparison, operator, logical, electronics, greatscott!, beginners
Id: YT3birSKLLU
Channel Id: undefined
Length: 7min 38sec (458 seconds)
Published: Sun Mar 12 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.