Arduino Programming Tutorials for beginners – 61 | If condition – 20 | Desktop
Okay, so im not going to explain anything because i have explained enough these two effects scenarios very clearly, okay, so we will just go to the demo and see whether this is working the way we expect it or not. That means we initially print the x value is one and then we simply change it to two okay and then this will be skipped, so we dont care worry and we will with that x. Value is two: you will come here, we will skip it and we will print this and again we will come here. This is false, so we check the elsif, since this is false. Only we check the elsif and we make it one and we come with x. Is equal to one and we do this and we skip the rest and theres another if condition because the second, then we check check the second, if condition – and we make it two so this chain switching chain will continue. Okay guys, so i will demonstrate it. Lets have a look, so the outcome should be x, values, one actually is two again x. Value is one x. Value is two one two each next, each other should be printed okay, so i have, i will copy it and i will simply do a verification. Its done compiling right, so i will have my tinkercad and ill go here, so i want you to do with the real arduino on real serial monitor guys.
I agree, im saying it again and again right. Okay, so, as i explained lets hope it will work, as i explain guys as we expect. Okay ill just simulation and see, okay, see one and two are coming next to each other. Okay, you see. So this is not readable right, because this is very fast. So we will slow it down a little bit okay, so what i will do is: okay lets have a delay. Okay, so where can we have a delay? Um lets see at the end of this code. Okay, we print one, okay, um and then well. At the end of the code – and we will switch the value and after that we will have a delay, have a delay and we will print the next one. Okay, so that you can see this one two which is printing uh next to each other right. So then, again at every iteration we will have a delay. Okay, so once it is printed switch it and then we wait for a second or second or 500 millisecond, then in the next iteration. This is printed and we wait for another. Second – and this is printer – we wait for another second, after switching okay, so i will have a delay. So this is very easy right. I go 500, so this will run anyway because this is out of any of this if else chain. So this will be any way this will be running right.
The delay will be there. Whatever is printed, the delay will be coming after every printing or after every iteration. There will be a delay. Okay, so nothing to work so verify it. Okay, compiling is done so copy; it ill delete the current one and quickly paste. It lets start the simulation so now see. You can clearly see that right. So that is the need of the delay, guys you cannotice it. Okay, so the adding the place so the place where the delay should be added. I decided it has to be in the last summer, so there is no harm, but depending on your purpose, you have to decide whether i mean where the delay should be coming. Okay, you have to logically think using your common sense. So now the outcome is totally fine right, one, two two, so you will understand this value is being switched in a very smart manner right one two one two is coming next to each other right. So i want you to understand this kind of tactics, tactics or programming techniques, so that you can take advantage of them: okay, switching the values so that switching the outcomes – okay, guys, so that is fine, so uh uh. So before moving into the next scenario, lets do a small refactoring guys, okay, so stop the simulation. Lets come here, okay, so what i do you see? I am doing a thing two things when if x is equal to one okay, so here i am doing this thing x, value is one displaying when x is equal to one okay also, i am reassigning the value for the same condition.
So what if i take this value and adding here in the next line, because if x is equal to 1, both should be happening right, okay, and also this also, if x, is equal to two in two places. I am checking the same condition: okay, so what? If i add these both conditions together in one place, why should i write uh code unnecessarily? Okay, so guys, if you understand what i have taught up to now, then only proceed with that. Okay, if you didnt understand you had to re watch it and you can even skip it. Okay, because this is some code refactoring scenario. Okay, this thing, if you dont understand you, had a gain by your experience, guys: okay, so just lets check whether it will work or not okay. So what will happen if i have this to be? This x is equal to here, and this switching should be here here. Okay, so lets simply think if this is not here, what will happen? Okay, okay, guys so lets say at the initial stage x is equal to one. Okay, so well, come here and well check. If x is equal to one, no, not this is true. Okay, so what we will do is we will display. This value x is x, y is one then what will happen? We dont do anything else. We will come here and we will do. The second thing x is equal to two and again we dont do anything else and we only have the delay so c.
We do this and this when, when x, is equal to 1. So why do we have to do these two things together? Why dont? We have to write two, these two lines together. Why do i have another one x if x is equal to one check, so what i will do is, i will write. This x is equal to 2 here, and if i remove this okay, then what you will realize, if x is equal to 1, i will see you bring the value and i change the x value too. The same thing i i earlier also these two things were happening, but in different lines, thats all here and here, but i will remove it here. No, i want to remove this. Not this. This the same way x when x is become two normally what we do. We do this and we do this. So what only these two things i am doing anyway, delay will be running okay, so why do i have written right in two places? Ill write it here, and i will remove this so here also if x, is equal to first. This will be happening and this will be happening and the rest will be anyway skipped. What is it okay, so i will remove it guys, so this should be happening from my common sense or with the practice and experience for the moment. I am telling this as a refactoring okay, so that you can consider it guys.
Okay, so this shouldnt be affecting any functionality lets see whether the functionalities are vectored or not guys. Okay, so lets see if x is equal to 1 ill x, equal to 1 ill come here in the first iteration x is equal to 1. I will print this value and i will change x value to 2. Okay, since this is true, the rest of this code will be skipped and only the delay will be there. Okay, so because this is out of aprils chain and now what is x value 2 right. So now we will come here. We will check. This is false. Okay and we will check x2 or not true, so we will print the x value is 2 and we will reassign x to be 1 and well skip the rest. Only the delay will be running so now. What is actually 1 again? Well. Do the same thing? Okay, so now tell me this flow. Is there any different from the flow i explained when there was the other reference condition, also same flow right, theres, no single difference, okay, so the same floor, i explained when we write, wrote a separate. If else condition for switching the same flow now also we have so this is a kind of code, refactoring lights. Okay, you understand what im telling so what im doing is im printing and switching and the next iteration okay. This will be true because i know because i have switched it okay in the next iteration.
This will be true, im, printing and switching and again, i know when we come here. I have to switch it because x is equal to one, so i have to switch it. So i do the work first and then i switch okay guys so try to understand what im telling guys okay. So i will do a small demonstration and see whether its working or not, if you dont understand this, just ignore it, you will get it with the experience guys. Okay, so im just doing a small refactoring so that my code looks very neat and clear. Okay, with only one if and else condition, okay, i dont need write extra codes.