![]() ![]() When going through the “if … then …” statements, we will notice something interesting – which causes a lot of bugs in programs. Since a working condition was found, the last “else” will be ignored. The 3rd condition however SUCCEEDS, so the “Whoohoo – we are rich!!” message will be displayed. In the example, AllMyMoney = 16, so the first 2 conditions FAIL. ” construct will be exited once a condition has been met.Īll following conditions will be IGNORED !!!! Going through Multiple “if … then … else if …”Ī few things to notice in this code – and you have to pay attention to this to understand why certain things work the way they work!Īn “ if … then. – in all other situations print “It’s all good … no worries.”. – “Woohoo – we are rich!!” when AllMoney is greater than 10, or – print “Oh oh,… we are low on cash!!” when AllMyMoney is less than 5, or Once one of the conditions has been met, the entire construct will be seen as “done and dealt with” … following “if …then …” (in the same construct) will be ignored. “if … then …” statements can be built with multiple “if … then …” constructs. Once it found a matching condition, it will execute the code block that goes with that condition and EXIT the entire “if … then …” stack. then … else if … then … else if … then” like this, your Arduino will go through the conditions one at a time. If AllMyMoney=0 then panic else if AllMyMoney<5 then get more money else stay cool. The “if … then …” statement however can be expanded with as many “if … then …”‘s as you’d like. If you’d like to omit the “else” part, and only want to see a warning when our funds are low, your code would look like this: The condition in line 25 is now being met, which means that the “if” code block will be executed and the “else” code block will be ignored. Our output will look like this: No change the code in line 12, and set our savings to zero and compile and upload again to your Arduino: Savings = 0 Because the condition in line 25 is not being met, your Arduino will ignore the “if” code block and execute the “else” code block and the response will be: If the condition has NOT been met, the message will be “ It’s all good … no worries.“.Ĭopy and paste the code in the Arduino IDE and compile and upload it to the Arduino. If the condition is met, a message will be displayed “ Oh oh,… we are low on cash!!“. ![]() The fun starts at line 25 where we check to see of the value of the variable “AllMyMoney” is less than (<) 5. print the values to the serial monitor The “if…then…” is a very common construct, which you will find in most other programming languages as well. Did you notice the “if … then …” in this text? If it is not dark then we need to switch the lights off. If it is dark then we need to run the code to switch the lights on. In our code we would first check if it’s actually dark. Let’s say we want to write a program that switches the lights on when it’s dark outside, and off when it’s not light outside. This is what is called “ control flow” or in other words: controlling how we go through the program and execute pieces that we need. ![]() Making decisions in our code is useful when we want to execute particular code, only when a certain situation occurs. ![]() Both of these are of great use in this section: Making Decisions in your code. After that we also looked at Boolean Operators, so we can see if certain conditions are met. In the previous chapter we have briefly looked at Comparison Operators, which allow us to compare two pieces of data. " ConditionsĪ complete overview of this course can be found here: Course Overview. Sending any other character will bring up a default message showing that the option chosen is invalid. Sending 3 will show the menu of options that the sketch operates on. Sending 1 from the serial monitor window to the Arduino will switch the on-board LED on and sending 2 will switch the LED off. Load the sketch to the Arduino and then start the Serial Monitor window. The following Arduino sketch shows the switch statement being used in conjunction with the break statement. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |