Cracking the Code: Unveiling All Arduino Operators for Programming!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we'll do the comprehensive review of operators that can be used when Arduino programming quick Interruption this video may be the first video in a series covering basics of Arduino programming being a part of my larger plan to introduce as many people to Arduino as possible but you have to let me know whether you like it or not because in the past I created the video with a beginner's guide to Arduino and that video did not get a lot of traction so if you enjoy this content let me know and there will be more videos like this coming your way and now back to the video [Music] [Applause] [Music] before we start I have a quick update for you this month I got no one not two but three new patrons guys thank you so much and welcome on boards support like this means the world to me for all of you watching you can support the growth of my channel by joining these guys okay back to our topic today we'll do the comprehensive review of operators that can be used when Arduino programming there are several types of them arithmetic comparison Boolean compound and last but not least bitwise operators this video is meant for people who are starting their Journey with Arduino programming so some of the operator types are very straightforward and I would Breeze through them but some may not be as obvious so check this video till the end maybe you'll learn something new let's start with the easiest arithmetic operators first one is a sign operator this one is used to assign a value to a variable if you see this operator in between two variables it does not mean those variables are equal to each other it assigns the value from the variable on the right to the variable on the left then you have add subtract multiply and divide operators and finally you have operator that Returns the reminder from the integer division we'll look at this one more closely quick presentation in Arduino code let's introduce two variables A and B with subsequent values 2 and 4. we open serial monitor let's look at the assign operator first if I send the current value of variable a to serial monitor the value 2 would be output and then when we assign the value of variable B to variable a and run the print function again to Output variable a we get now value 4. now let's reset values of A and B back to 2 and 4 and run few print commands to Output results or formulas using arithmetic operators addition subtraction multiplication and division all results are as expected we have one arithmetic operator left to discuss reminder after Division if you need to be reminded what reminder operator is reminder is the amount left over after performing The Division so for example we cannot divide 10 by 3. the closest number you can divide by 3 is 9 so if you have 3 times 3 plus 1 it makes 10 and the one here is the reminder this one I actually used to optimize the code in one of my projects I had video in which I was showing how to control four digit 7 segment display in it I had an easy sketch that was counting from zero to 9999 so here you can see how this was working and here is the code behind it I created the for Loop that was changing the value of variable I from 0 to 9999 I created a fairly complex formulas to dissect each value of I variable into digits representing ones tens hundreds and thousands and using set digit function to Output those digits in the right places on the display we are not going to redo the prototype for this project we can alter the code to Output the results of the formulas into the serial monitor this way the Prototype would be simplified to just Arduino connected via USB to the PC simple let's glance quickly at how those formulas worked let's choose the sample num from the counter range EG 1549 let's see how the calculations would look like to come up with the digit representing thousands here is the calculation for the digit representing hundreds is it complicated you betcha I spend fair amount of time to come up with those formulas I will leave you to check if the other two formulas would work as well let's see how this code works great that is what we wanted but then I realized I can use the reminder after division operator here and I came up with this code let's look at it and dissect the same sample number starting with digit representing thousands the calculation for that digit would look like this now let's see how the calculation would work for a digit representing hundreds here I will also leave you to make calculations for the two remaining digits the code is more clear and much easier to understand but will it work let's give it a spin and as you can see it does now let's look at the comparison operators there is a handful of them we have equal to not equal to greater than less than greater than or equal to less than or equal to we mostly use those operators in conditional statements like if while for switch let's do a quick demonstration to be able to do this I build this simple Arduino circuit in it we are using Arduino Nano six LEDs I'm using those lilypad LEDs which have already built in current limiting resistors connecting minus of each led to ground and plus of each led to digital pins from D2 to D7 then I have potentiometer connected to one of the analog pins and finally I have a seven segment display to track the current position of the photoshometer I have a separate video where I show How I build this display please check it out having the circuit in place we can now write the code that will demonstrate how those comparison operators work we start by introducing variable P that will store the potentiometer reading in setup we need to set up all the Arduino digital pins to which the LEDs are connected as output in Loop we use analog read function to read potentiometer value that is in range from 0 to 1023 and use map function to map it to the range from 0 to 9 we save the mapped value into variable p and then we create a set of if statements each one controlling a single LED so turning it off or on depending on the condition being fulfilled or not so we will turn on the first led if the potentiometer is in position 5. we'll turn on the second LED if the potentiometer is not in a position 5 we'll turn on the third led the potentiometer is in position higher than 5 the 4 value D would lit if the position is lower than 5 the fifth would let if position is greater or equal to 5 and finally the sixth LED would turn on when it is smaller or equal to 5. the code is already loaded position is 0 and the LEDs 2 4 and 6 are on because position is not equal to 5 it is smaller than 5 and it is smaller or equal to 5. let's change potentiomet position so far no change but when we get to position 5 now LEDs 1 5 and 6 are lit as position is equal to 5 position is greater or equal to 5 and position is smaller or equal to 5. carrying on we change position to 6 and now three conditions are met position is not equal to five it is greater than 5 and greater or equal to five further increasing of the pot position does not result in any changes now when we return to position 0 you see the LEDs react accordingly now let's look at Boolean operators comparison operator allowed us to check a single condition Boolean operator lets us connect several conditions and build more complex ones we have three Boolean operators logical and operator here is the alternative notation for it if both the operands are true then the condition becomes true next one is logical or operator if any of the two operands is true then the condition becomes true the last one is logical not operator we also can use this notation used to reverse the logical state of its operand if the condition is true then logical not operator will make it false we can reverse a single condition but we can also apply this operator to more complex conditions as well let's look at the example here is the axis that shows all possible positions of the potashometer from our example so 0 to 9 if we have a single condition p is smaller or equal to 7 this condition will be met in the highlighted range let's look at the different single condition p is greater or equal to 2 that condition is fulfilled in this range now if we use logical and operator to build more complex condition we can single out positions that are both greater or equal to 2 and smaller or equal to 7. let's do the similar example for logical or operator this condition would help us to select all positions smaller or equal to 3 and that one will select all positions that are greater or equal to 7 and here's the complex logical statement which is using logical or operator this way we are selecting all positions smaller or equal to 3 or greater or equal to seven now that we have that complex logical statement let's introduce the third operator logical not operator it reverses the state of our condition so we get the set of positions that are greater than 3 and smaller than 7. please mind that positions 3 and 7 are excluded so let's create sample code that will have several conditions using both Boolean and comparison operators and see how this affects our six LEDs Behavior so the setup function would be just like in our last example in Loop we will create several conditions to control the six LEDs the first led would lit when P would be either 5 or 6. the second LED would be on when the p is larger than 3 and smaller than or equal to 6. the third condition is met when p is smaller than 3 or larger than 6. the last three LEDs would be controlled by the same three conditions we have just created but reversed with logical not operator so now let's load the code and see if it is controlling the LEDs in line with the programmed in conditions at the start at the position 0 we have LEDs 3 4 5 on as p is smaller than 3 or larger than 6 p is not equal to 5 and 6 and P is not larger than 3 and small smaller or equal to 6. I will slowly go through all the other positions and you can check if LEDs are behaving as expected please note that since we applied The Logical not operator to conditions 1 2 3 to create conditions four five six the first led always has a different state than led4 same with sets of LEDs 2 and 5 and 3 and 6. looks like it works just fine let's move now to bitwise operators here they are to present how they work I would use two sample variables here you can see they are decimal and binary representations first we have binary and operator it copies a bit to the result if it exists in both operands here is an example two ones make a one for first bit two zeros Mega 0 0 1 makes a zero and we carry on like this for all remaining bits the result is 65. next is a binary or operator it copies a bit if it exists in either of the two operands so here two ones make a one two zeros make a zero zero and one make a one and so on the result here is 93. next one is binary xor operator it copies the bit if it is set in one of the operands but not both so two ones make a zero two zeros make a zero but zero and one make a one we populate remaining bits to get the result of 28. moving on to Binary node operator which is flipping bits so one becomes 0 0 becomes one and so on applied to decimal 81 we get decimal number 46. the last two are probably the most interesting ones you have binary shift right operator it shifts all the bits of the operand to the right by the specified number of positions here we take the binary representation of number 81 and we shift all the bits by two positions to the right we are losing data from two far right bit positions this operation returns 20. then we have also a binary shift left operator it shifts bits of the operand the same way but to the left again let's apply it to number 81 and shift all the bits to positions to the left the result here is 324. are you wondering what those bitwise parameters can be possibly used for let me show you one example in my recent video I was controlling this led Matrix one of the things I did was to Output text on it it looked like this and here's the code behind it here is the table that holds the text to be displayed where each cell corresponds to One LED on the LED Matrix as you can see this is array of bytes in loop I had a nested for Loop that was going through all the cells of that table if Cell was holding one we were lighting up the corresponding LED if it was Zero LED was off here also we are not going to recreate this setup but rather alter code to Output the result to serial monitor so we get rid of the two lines that were interfacing with the LED Matrix and introduced the line that would output at sign in case of 1 and Dot in case of 0 to the serial monitor let's add a short delay so we can observe how the text is displayed one sign at the time after processing all 32 cells of each row we need to send the new line command to move to the next row let's load the code and observe the result in the serial monitor looks great let's take a closer look at the table this is not the most optimal way to store information to store info for a single LED or character displayed in the serial monitor we need just one bit and here we are using the whole byte so 8 Bits to do it the optimal way this table should look like is this unsigned car data type comprises of four bytes so 32 bits so perfect for what we want to do here at first glance you may not see the text we want to display hidden in that table but believe me it is there but how can we navigate within the bits of those 8 entries in the table here is our table we'll go row by Row in which row we'll try to extract bits starting from the most significant on the left and move to the least significant on the right but how here is where bitwise operators would come in handy so we have our row of bits we would introduce shift variable which holds binary value initially this variable will have one in the most significant bit position far to the left here we will be displaying the results of the bit extraction so the procedure would look like this we apply binary and operator to process draw and shift variable it carries only the far left bit in this case one all remaining bits of the results are set to zero then we shift the bits of the result 31 positions to the right and we end up with a single bit which was extracted from the far left bit in the processed row we can now use print command to print it if we want to extract next bit in sequence we have to shift bits in the shift variable one position to the right now if we again use binary end operator the second bit gets carried forward we have to now shift it by not 30 31 but 30 positions to the right the bit in this case 0 is extracted and ready to be printed we continue the same way to extract all remaining bits in that row let's make changes so that the procedure we just explained is reflected in the code we declare shift variable before we start processing each row it is set to initial value where far left bit is set to 1 we need to change if statement so instead of reading single value from the table it actually extracts the bit from the currently processed row I change the characters displayed in case of 1 and 0 detections so the output would look slightly different this time after extracting and printing each bit we are shifting all bits of the shift variable one bit to the right this way we'll be extracting next bit in sequence let's see if this code would work and it does so what have we achieved here by using bitwise operators we reduced memory used to hold data from 256 bytes to just 32 and in Arduino world this is massive but this just scratches the surface if you check this video of mine you'd see that bitwise operators are key elements for advanced way of controlling Arduino digital output and input pins this is next level stuff so this video that seemed too basic and too easy at first all of a sudden is not that basic anymore finally we can look at the compound operators the word compound means a composite of two or more things similarly the compound operators are also a composite of two operators here is the list increment operator it compounds assignment with addition so a statement where we are adding 1 to variable a can be replaced with the statement like this decrement operator works the same way but for the subtraction by one we also have compound for all types of arithmetic operators here's the one for addition so instead of assigning to variable a the result of adding variable a to variable B we can simply use this compound operator we have similar compound operators for subtraction multiplication and division then we have compound parameters for reminder after division logical or operator and logical end operator the compound operators allow you to condense the line of code it looks more slick but you are not really cutting on number of lines of codes maybe with one small exception let's look how we can improve our code using increment and decrement parameters first let's create super simple program without compound parameters we declare variable a we open serial Monitor and setup function in Loop we run command to increase a variable by one and when done we output it to serial monitor so since at the beginning a was equal to 0 and we increased it by 1 we should see 1 in the serial monitor Loop is running in a round robin fashion so each time we run it a is increased by 1 and we should see incremented values in the monitor let's copy the code and try to rework it using the increment operator in this case we can get rid of command and that is increasing the variable a by one instead we can use increment operator directly in the print function so the code is definitely optimized here let's mix it a bit and test these sketches in Arduino Tinker cut simulator some of you might not know this but you can actually start playing with Arduino without actually owing one or any other electronic part for that matter if this is news to you you definitely want to check out my video covering this topic so here we have the original code before we run it let's open the serial monitor pane we Press Start and we see one being displayed in the Monitor and then every second subsequent number incremented by 1 is displayed let's now alter the code and add the increment operator we start the code again ah this time the code works in the same way but the sequence starts with a zero this is because in the print function we first output the value and then increment this can be easily fixed by changing the initial value of variable a to 1. sheesh that was a lot of operators to cover in a single video which by the way took forever to make before I go I would like to give special thanks to Dave Moreno for generous donation via PayPal and also let you know that while I was working on this video I gained a new Patron so by the looks of it I cannot keep up with the support I am getting from you guys which I guess is a good thing I will see you in my next video over and out [Music] [Applause]
Info
Channel: Mario's Ideas
Views: 1,946
Rating: undefined out of 5
Keywords: arduino, nano, programming, c++, operators, bitwise, comparison, compound, and, or, &&, shift left, shift right, reminder, memory, tutorial, how to, instructable, easy project, binary, logical, arithmetic, breadboard, microcontroller, circuit board, hardware programmer
Id: XqOGBkifZrc
Channel Id: undefined
Length: 23min 23sec (1403 seconds)
Published: Wed Apr 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.