How to Use Switch Case in Arduino Programs – Ultimate Guide to the Arduino #12
That is perfect for anyone who wants to learn how to master the Arduino. The kit comes with an Arduino 22 different sensors and modules thread boards, jumper wires and everything else. You need to build a bunch of fun and interesting projects, learn about robotics by building a remote, controlled smart car that can be controlled with an infrared, remote controller or drive on its own and avoid obstacles or Follow The Line. Learn about the internet of things. With a project that lets you monitor the temperature humidity and light level of a room from an app on your smartphone Music and build a plant monitor that tracks, the temperature, humidity, light intensity and soil moisture and displays it on your smartphone. So you can keep your plants water, remotely its a super cool kit, and I had lots of fun building all the projects in it. So click the link in the description below to order the kit from some founder foreign Music were going to learn about. Another type of conditional statement called The Switch case statement when you have multiple conditions that need to be tested its better to use switch case rather than multiple. If statements, the code is simpler, faster and more optimized lets take a look at an example sketch say I want to take a reading from a temperature sensor and print on the serial monitor whether the temperature is low, medium or high. A thermistor is a type of analog temperature sensor.
Well, learn how to use them in an upcoming video, but for now all you need to know is that they output, an analog, read value between 0 and 10 23.. So first thing I need to do is declare the analog, pin variable Ill call it thermistor, pin in the setup section I need to initialize the serial monitor foreign since analog pins are assumed to be inputs. I dont need to use pen mode in here in the loop Im going to make a variable called temp to store the analog, read value from the thermistor pin, and then I want to divide the analog read value by 341., which will divide it into three integers. That represent the low medium and high temperature ranges, so this will take the analog, read value stored in temp divided by 341 and save it as a new value in temp to make the switch case statement, you start by writing switch followed by a pair of parentheses And a pair of open and closed curly braces the variable or value that will be compared to the cases goes inside the parentheses the sketch is going to compare the temperature values against the cases. So Im going to put my temp variable in here inside the curly brackets here is where the case statements go now. Well, have three possible cases, one two and three, so the first case will be one Ill type that here then in this line with a colon on the next line below that Ill put a line to serial print temperature is low, then Ill end the case with A break causes the program to exit the switch statement once it is found to match its not absolutely necessary, but it prevents the program from checking all the other cases against the switch condition even after it is found a match.
So the next case statement will be for when temp equals two now our case is 2.. When the temp range is 2, we want to print a line that says temperature is medium and when the temp is in the third range Ill print out, temperature is high. After writing the case statements we can use Whats called the default statement. The default statement is optional, but it can be useful. The default statement is executed if none of the case statements match the switch condition in the case of this temperature sensor. If none of the cases match theres, probably something wrong with a sensor, so Im going to print out sensor, error and thats it first, the Arduino is going to take a reading from the thermistor pin and store the value in the temp variable. The value will be between 0 and 1023, depending on the temperature it measures, then we convert that value to a set of three temperature ranges, one two or three by dividing it by 341.. The switch statement is going to look at the temp variable and check it against each one of these case statements. If temp is 2, this case statement will be run and temperature is medium will be printed to the serial monitor. After that, the break keyword will make the program exit the switch statement. If temp is three, this case statement will run and temperature is high will be printed say, temp is zero. None of the case statements are zero, so the default statement will be run after that.
The sketch will exit this switch statement block and continue on with the rest of the program switch case. Statements are really useful when you have lots of different inputs and need to do something different when each one occurs were going to be seeing more switch case statements when we get to the video on infrared, remotes and receivers, but in the next video well take a Closer look at loops and how we can use them to get even greater control over the flow of our sketches Music. Sun founder is my go to source for sensors modules and other parts for the Arduino and Raspberry Pi. They have a huge selection of stem Robotics and iot kits, and lots of useful sensors and modules. Every product has an online tutorial with wiring diagrams and example code. They also offer free shipping on all orders, with no minimum, give them a try at www.sunfounder.com. Next time.
Recent Comments