4372Views11Replies

Author Options:

Arduino true and false statements Answered

So I'm having the worst trouble tying to figure out how to use true and false statements. Can anyone explain how to define something as true and how to use it! An example sketch maybe?

Thanks!

Discussions

0
None
HavocRCsteveastrouk

Reply 5 years ago

I was more talking about how to use true and false, not if statements. I can already construct if statements, but I don't know how to incorporate true and false with it.

0
None
caitlinsdadHavocRC

Reply 5 years ago

Think of true and false as yes and no. You are trying to figure out what the status or condition or value of a variable is. If an arduino pinvalue is HIGH, yes it is ON, if it is connected to a switch, then we can say yes, it is true that the switch was pressed. If the switch was pressed, then signal the motor to turn on or something. It can also happen that the pinvalue was NOT HIGH, or no reading and the signal or set to LOW. In that case the switch is not pressed so we interpret that as FALSE and you can decide what to do next. You can also combine the various checks - if 2nd button is pressed AND 3rd button is not, then put the robot in reverse. if 2nd button is pressed OR 3rd button is pressed then flash the warning LED. Hope that helps.

0
None
caitlinsdadcaitlinsdad

Reply 5 years ago

and also do that with numeric checks/equalities - is the counter greater than 10 then blink a LED or if the counter variable reaches a maximum number then reset it, etc...

0
None
HavocRCcaitlinsdad

Reply 5 years ago

Alright ok, lets see. Will this work? This code is from my lockduino i'ble.

int a = true
int b = false

if (buttonState == HIGH)
change it to
if (buttonState == a) ??
{
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
myservo.write(90);
delay(5000);
}

else
{
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
myservo.write(170);
}

0
None
caitlinsdadHavocRC

Reply 5 years ago

Your trouble may be seeing how computers think. It only knows 1 and 0 which is on and off. We can call it whatever we want when we program in code by labeling it with something we understand. The arduino code knows HIGH and LOW or just the values 1 and 0. If we want to substitute the words TRUE and FALSE we have to define the variable as Boolean. So you are getting there. The digitalRead(pin) example in arduinoo.cc reference sets a regular numeric placeholder value for the button state. You do a digitalRead to assign that value of whatever state the button is in, 1 or 0, HIGH or LOW which we understand as on or off. You then do the comparison or your check, has my placeholder variable been set to 1 or HIGH (code understands both without you having to declare anything special) then do what you have set out in the IF statement.

0
None
caitlinsdadHavocRC

Reply 5 years ago

IFstatements are basic syntax of all programming. Maybe you could just look up basic or beginner programming to see what is out there. Understand flowcharting. Only difference amongst programming languages, in this case the arduino, is how it is written in the code.

0
None
steveastrouk

5 years ago

TRUE and FALSE are the results of LOGICAL operations.
If A is true AND B is true then Answer is true else Answer is false;

So if A=digitalread(14) and digitalread(14) is low, then A is false

You can also just SAY A=FALSE, or A=TRUE and use them in your programs, if it suits that kind of variable.