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. This example code is in the public domain. * 10K resistor from analog in 0 to ground Room, open the serial monitor, and and move your hand gradually To see this sketch in action, but the board and sensor in a well-lit Statement allows you to choose from among a set of discrete values The analogRead() function returns a range of about 0 to 600 from this circuit in a reasonably lit indoor space.ĭemonstrates the use of a switch statement. A 10Kilohm resistor makes up the other side of the voltage divider, running from analog in 0 to ground. The photoresistor is connected to analog in pin 0 using a voltage divider circuit. Finally, it uses the switch() statement to print one of four messages back to the computer depending on which of the four values is returned. Then it uses the map() function to map its output to one of four values: 0, 1, 2, or 3. This program first reads the photoresistor. This tutorial shows you how to use it to switch between four desired states of a photo resistor: really dark, dim, medium, and bright. Switch allows you to choose between several discrete options. When there are more than two options, you can use multiple if statements, or you can use the switch statement. Switch (case) Statement, used with sensor inputĪn if statement allows you to choose between two discrete options, TRUE or FALSE. Learning Examples | Foundations | Hacking | Links
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |