Arduino Basics 102: Control Structures, Variables, Interrupts

Arduino Basics 101: Understanding Control Structures

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 an 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 popularly used if/while 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 use double equals basically it means equal but there also exists the not equal, less than, greater than, less than or equal to, and greater than or equal to comparison operators.

But getting back to topic now if Pin 10 connects to ground the LED on Pin 7 lights up just like we program it to do. But after removing the ground potential from the pin the LED is still lit, what we forgot was to add an else structure which pulls the LED pin to ground when the if condition was not fulfilled.

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.

WEBVTTKind: captionsLanguage: enDuring the basic 101 videoI taught you with the fivefundamental code commands of the Arduino softwareand what they can achieve once uploaded toArduino development boardand even though it is definitelyfun to let an LED blink or determine thevoltage on a digital pin we are stillpretty limited on what we can do becausewe are missing so-called controlstructures most popular or if for wileand switch structures by utilizing if wecan perform an action like pulling highdigitalPin if a specific condition ismet, in this case we double equal signbasically it means equal but there alsoexists the not equal, less than, greater thanless than or equal than and greaterthan or equal than comparison operatorsbut getting back to topic now if Pin10connects to ground the led on Pin7lights up just like we program it to dobut after removing the ground potentialfrom the pin the LED is still lits, what weforgot was to add an else structure whichpull LED pin to ground when theif condition was not fulfilled and afteruploading the new codes and adding apush button to connect ground to theinput pin we successfully created ourfirst microcontroller circuits withcontrol structures but at this point youmight say we don't need amicrocontroller for that and you wouldbe absolutely correct but if we go onestep further and include booleanoperators it gets a bit more complex inthis case the && represents thelogical 'and' there also exists'||' for the logical 'or'and '!' for not knots so this time afteruploading the codes the LED only lightsup if pin 10 and 11 is connected toground but once again you could say wellI'll just take two switching parts andseriousto build a circuit without my controllerand once again you would be absolutelycorrect so to prove the usefulness ofmicro controllers let's remove the simple ifcondition and replace it with thecounter condition in this case thevariable I must be lower than the numberthree in order to LED blink onceand at the end of the action we increasethe value of I by one which can also bewritten as 'i++' but whilecombining the sketch and error messagepopped up saying that the variable I wasnot declared yet to fix that we candefine global so for the whole sketchabove the setup porch hear the wordrepresents a specific variable typeknown as integer which can hold a 16-bitnumber other popular types are bool orboolean which can either be 1 / 0 ortrue / false the long type which canhold a 32-bit number and the flow typewhich can hold a 32-bit number as wellbut with a decimal point as an exampleof the millis function of theArduino which counts the millisecondsafter the start of the codes is storedin a long time variable enters themaximum 32-bit size which is a prettybig number to begin with will beexceeded aka overflow after 49.7 daysbut getting back to our example bydeclaring the I variable with the startvalue of zero in the syrup section dcode compiles and upload properly andlet the LED blink for three times andthen did nothing else perfect andinstead of the function and thisand additional variable increment line wecould simply use the for loop whichcombines the initialization conditionand iteration right at the beginning asexpected this sketch does its job justfine as well but as another example forthe full loop let's say we want pin 5 7and 9 as an output now of course wecould simply define each individual pinin the setup section but we could alsocreate an array of integersvariables which contains the number ofthe individual pins while defining therear? The content of the square bracketdescribes the amount of variables in thearray and the numeration of theindividual variables always thoughtswith a zero so by inserting the numberof the required valuable in the squarebrackets we can select them individuallythis way by using a for loop which countfrom 0 to 2 we can define them all as anoutput without repeating the same lineover and over again and while we're atit and don't want to change deep invariables later on we can change theinteger type to a constant integer typenext we got the while structure which incomparison to if and forstructure loop the defined actionendlessly until a certain condition ismet and then continues with the rest ofthe sketch as usual and lastly we gotthe switch structure which can performdifferent actions dependent on the valueof a variable this way we could light upthree different LEDs dependent onwhich pin was pulled to ground through apush-button now let's move away fromthose learning circuits for seconds andlet's have a look at an arduino FM radioletter? builds in a previous projectvideo if we take a look at the codes wecan see that there's quite a bit goingon so let's imagine we turn the rotaryencoder one increments by doing so thetwo so-called clock and data pins getpulled down to ground one offer theother for brief moments that only lastsaround 11.5ms only problemis that your microcontroller wascurrently sending data to detect LCD anddid not notice that the pins were pulleddown to ground that is where we use anexternal interrupts which performs afunction when other definedinterrupt Pin, so 0 for pin 2 and 1 for pin3of the Arduino Nano is either low highchanges false the ground or rises to thesupply voltage doesn't matter whetherthe code is doingsomething out at the moment theinterrupt always has the highestpriority to create an executablefunction though we can use the voidvariable type since our function willnot return any variables then we simplyfill it with what the interrupt issupposed to do and activate theinterrupt through the attached interruptfunction in the setup section now whenother the Arduino pin 2 changesStates the led it changes its as well eventhough there's a long delay happening inthe code and with that being said youare now ready to try out your firstArduino projects if you like this videoand want to see the final Arduino basics 103video which we'll discusslibraries weird lines of code in thesetup section and more advanced tipsthen don't forget to leave a thumbs upas always stay creative and I will seeyou next time