Arduino true and false statements

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?


sort by: active | newest | oldest
1-10 of 11Next »
caitlinsdad4 years ago
The arduino site tutorials are pretty good with examples. Look at the "if statement" http://arduino.cc/en/Tutorial/IfStatement
HavocRC (author)  caitlinsdad4 years ago
Other than the arduino website?
Why not that ??
HavocRC (author)  steveastrouk4 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.
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.
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...
HavocRC (author)  caitlinsdad4 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);

digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
Yes, it will work.
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.
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.
1-10 of 11Next »