4051Views11Replies

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!

11 Replies

user
caitlinsdad (author)2013-03-28

The arduino site tutorials are pretty good with examples. Look at the "if statement" http://arduino.cc/en/Tutorial/IfStatement

Select as Best AnswerUndo Best Answer

user
HavocRC (author)caitlinsdad2013-03-28

Other than the arduino website?

Select as Best AnswerUndo Best Answer

user
HavocRC (author)steveastrouk2013-03-28

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.

Select as Best AnswerUndo Best Answer

user
caitlinsdad (author)HavocRC2013-03-28

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.

Select as Best AnswerUndo Best Answer

user
caitlinsdad (author)caitlinsdad2013-03-28

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...

Select as Best AnswerUndo Best Answer

user
HavocRC (author)caitlinsdad2013-03-29

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);
}

Select as Best AnswerUndo Best Answer

user
caitlinsdad (author)HavocRC2013-03-29

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.

Select as Best AnswerUndo Best Answer

user
caitlinsdad (author)HavocRC2013-03-28

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.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)2013-03-28

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.

Select as Best AnswerUndo Best Answer