When the value measured at the analog pin is above 400, we turn on the LED at pin 13, when the voltage is below 400 we turn the LED off. It happens more often than you might think – I recommend using constants when the variable will not change in the program.The integer constants we declare are pretty straightforward. If it is above the threshold the LED turns on, if it is below the threshold the LED turns off.We also want to see the actual values at the analog pin, so we print them to the serial monitor. The list of comparison operators on the Arduino Reference page is as follows:These operators offer a broad spectrum of comparisons to use. We do this with the delay() function.Let’s recap. It’s as easy as that.Make sure you read the sketch and try to figure out what is taking place before moving on.This program might look long to you – the previous ones were a bit shorter. They make it possible to test a variable against a value/compare a variable with another variable and make the program act in one way if the condition is met, and another if it isn’t. It protects you from unintentionally writing a new value to the variable. A constant is classified as a qualifier – it adjusts the behavior of the variable being declared. The first chunk in this sketch is the multi-line comments thatThis might seem like a lot of stuff – but I would recommend you do the same thing for your programs! The first line of code we encounter reads from the value at the analog pin A0 and assigns this value to an integer variable called To do this, we use the analogRead() function. The You have seen the if statement before, now we add an else statement. It is similar an adjective in a sentence – “The squishy material,” squishy qualifies what the material will behave.It might seem counterintuitive to the whole point of variables, but the constant qualifier will stop the variable from changing throughout your program. This value will change as we adjust the potentiometer. Arduino - If statement - It takes an expression in parenthesis and a statement or block of statements. The statements being evaluated inside the parentheses require the use of one or more operators shown below. As I have said previously, the if-else statement is a staple of programming – you will see it (or a similar form of it), in any programming language you encounter henceforth.Get instant access to the Arduino Crash Course, a 12 lesson video training curriculum that teaches the details of Arduino programming and electronics and doesn’t assume you have a PhD.You will receive email correspondence about Arduino programming, electronics, and special offers. Ltd. All rights reserved – This tutorial discusses what are conditional statements, and their different types in Arduino IDE, such as the if statement, if-else statement, and if-else-if statement.Try interchanging numbers and then see what happens.Try interchanging the marks and then see what happens.Make Flappy Bird Game Using Human Body Detection Extension in PictoBloxMake your Own Smart AI Chatbot Using Speech RecognitionMake Toy Recognizer Using Machine Learning Extension in PictoBlox switchCase - How to choose between a discrete number of values. Accidentally dropping an equal sign, or typing a > instead of a < is easy to do, and usually leaves you scratching your head when the program acts differently than you might have expected.Now that we have turned the LED on or off depending on the position of the potentiometer, let’s see exactly what values are being read. Auf den else -Teil kann wieder ein if folgen, sodass viele sich gegenseitig … By submitting this form you agree to the You will receive email correspondence about Arduino programming, electronics, and special offers.
The variables analogPin and ledPin are those pins the potentiometer and LED will be attached to on the Arduino board. The list of comparison operators on the Arduino Reference page is as follows: == (equal to)!= (not equal to) < (less than) > (greater than) <= (less than or equal to) >= (greater than or equal to) These operators offer a broad spectrum of comparisons to use. Here, we have to set the mode of a pin and set up serial communications.Recall that all pins are by default set to INPUT, so we do not have to explicitly set the pin mode for our analogPin A0 as an INPUT – though I would argue it is best to do so for clarity.You should have serial communication down pat. First we read the value at the analog pin and assign that value to a variable. We want to do this to ensure the program is doing what we think it should and also to make sure our potentiometer (sensor) is working properly.This line of code uses the println() function from the Serial library – which (as you know by now) sends the values from the Arduino to your computer.
Adjust your potentiometer and watch the values change in the Serial Monitor window. The else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time.