loading

if/else Arduino code

So I need a little help building some if/else statements. I went to the Arduino website but there wasn't a whole lot of information there. I need some help with reading analog input and reacting to them. I'm not totally sure how to use the == either, I know it means do something if one thing is equal to, but. . . Also, with the if/else, I need some help with using >= => (i think??) as well as true and false. I also think you can like use two different if statements and only one else. Could I get a little assistance from you guys!?

Here's an example:

if(analog_in (is less than) 5)
{
 do something here 
}

else
{
 do another thing here
}


Here's another example:

if(analog_in (is equal to, I think ==) 5);
{
  analogWrite(LED, 100);
}
if(analog_in (is greater than) 5);
{
analogWrite(LED, 255);
}
if(analog_in (is less than 3, but greater than 1);
{
 analogWrite(LED, 50);
}

and if none of that is true, then do this

else
{
 digitalWrite(LED, LOW);
}

So you can see I need some help! 
Thanks!

sort by: active | newest | oldest
MattiV1 year ago

i try many day do code, but no understand, how make if analogRead value = >1010 then serial.println("relay1"); if value= 866-869 do serialprint relay2.

if value = 655-670 do serialprint relay3. and if value= <440 do serialprint relay4.

just test serial monitor input 12volt to 2volt,3volt,4volt,5volt input rotary switch and analogRead input value .but how can do somethink ewery switch position? switch/case system not can working because i no know how read analog value etc, 655-670 level and do somethink. because serailmonitor show input value noise little.

lovetronic3 years ago

Hi there. i'm using arduino uno and i found that the code below does not work. Seems like "if" do not work inside "if" statements. how do i solve this problem dear community.

if(ButtonStateAUTO==HIGH)

{

while(1)

{

Serial.println("auto");

delay(1);

if(ButtonStateON1==HIGH)

//while(1)

{

Serial.println("autoON1");

delay(1);}

}

}

else if(ButtonStateMAN==HIGH)

{

//lcd.clear();

while(1)

{

Serial.println("man");

delay(1);

if(ButtonStateON2==HIGH)

Serial.println("manuALon2");

delay(1);

}

}

HavocRC (author)  lovetronic3 years ago

Copy this into a new forum topic or you will never get this answered.

gmoon4 years ago
Don't put a semicolon immediately after an "if" statement. Here's the proper syntax:

if(analog_in == 5)
{
  analogWrite(LED, 100);
}

Two equals == is an "equate" test, which returns a boolean value (TRUE, FALSE).

One equal = is the "assignment" operator, as in: myVariable = 5; This assigns "myVariable" with the value of 5.

Conversely, myVariable == 5 would return TRUE if myVariable is equal to 5, FALSE if it is not. It does not change the value of myVariable.

>= is less than or equal, <= is greater than or equal. These are also test, or comparison operators, like <,>,!=, ==. Here's the Arduino lang ref. Read the Comparison Operator section..

----------------
The "I think" operator doesn't exist. ;-)

There also isn't a "but" operator. You're looking for Boolean logic here to combine comparisons. So:

if(analog_in (is less than 3, but greater than 1))

Should read:

if(analog_in < 3 || analog_in > 1)

Which translates to: If analog_in is less than 3 AND analog_in is greater than 1.

------------

Example 2:

You can use multiple If..else if statements here. Or use flags, like this

if(analog_in == 5);
{
  analogWrite(LED, 100);
  turnOff = FALSE;
}
........

if  (turnOff)
{digitalWrite(LED, LOW); }
gmoon gmoon4 years ago
Oops, I switched these (still pining for the day posts will be editable here...):

>= is less than or equal, <= is greater than or equal.

should be:

<= is less than or equal, >= is greater than or equal.

Also, I omitted initializing the flag:

turnOff = TRUE;

if(analog_in == 5);
{
  analogWrite(LED, 100);
  turnOff = FALSE;
}
........

if  (turnOff)
{digitalWrite(LED, LOW); }
HavocRC (author)  gmoon4 years ago
Wait, in the top, it says turnOff = true, then in the bottom, it says turn off = false?

Hey well thanks for all that! Are you serious, "I think" is not an operator? I use it all the time! digitalWrite(LED, I think);
:D

Ok, so I'm starting to understand what true and false are. But what about this?

if (analog_in == 5)

Then how would I set the words true and false to it?

if (analog_in ==5)
if TRUE, do this
if FALSE, do another thing
gmoon HavocRC4 years ago
Variable names can be anything. Your thought process might be different. But think it through for a moment:

-- at the start of every loop(), turnOff is initialized to TRUE. The default LED state is off.
-- any change of analog_in that meets the "LED on" criterion should also change turnOff to FALSE.
-- If turnOff has been reset to FALSE, the last if() statement will fail, and the LED remains ON. Get it? turnOff == FALSE, then don't turn off the LED.
-- If it falls through each test of analog_in and they all fail (the criterion is not met), then turnOff is still TRUE, and the last if() statement will turn off the LED.

You can change it around and rename the flag variable turnOn, but the function of the last if() statement is explicitly for turning off the LED, which is why when turnOff is TRUE, the statement does indeed turn off the LED.

There are dozens of ways of tackling this problem, I'm just offering one approach, mostly based on changing your original code as little as possible. And I'm trying to infer as much as I can based on your small sample.

I'd probably code it differently from scratch...
----------------------

TRUE and FALSE are conceptual, and their values can vary in each language and microprocessor. Usually FALSE = 0 and TRUE is any non-zero number. But the state tested as TRUE or FALSE is usually defined by certain microprocessor registers called "flag bits."

Those flags are set automatically during code execution. Simply loading a number into a register will set or unset flags. Arithmetic computation sets flags, bit shifting sets flags, etc.

Any conditional "test" statement like:
if (analog_in == 5)

Will alter those flags. In this case, if the statement is true (i.e., analog_in actually does equal 5), then the if() statement executes the code. If it's not true, the code is skipped.

TRUE and FALSE aren't just concepts, they are also defined constants guaranteed to set the conditional state.

Using comparison or math operators will automatically set a true/false state, if it's done inside a control structure like if().

Also (at least in "C") if you want to positively trigger an if() conditional, any non-zero value will be seen as TRUE. So the following if() statement will execute if analog_in is non-zero:

if (analog_in)
{
code here;
{
HavocRC (author)  gmoon4 years ago
Wow, that's a mouthful for me! Well thanks for explaining it to me!
HavocRC (author) 4 years ago
int LED = 11;

void setup()

{
pinMode(LED, OUTPUT);
Serial.begin(9600);
}

void loop()
{
int pot = analogRead(A0);
float xval = pot * (10.0 / 1023.0);
Serial.println(xval);

if(xval == 5)
{
analogWrite(LED, 255);
}


else
{
digitalWrite(LED, LOW);
}


}
HavocRC (author)  HavocRC4 years ago
Lol nevermind, I think what's happening is that it has many decimals and it's never exactly 5 for anytime long enough for me to actually see the LED on. Right?