66549Views9Replies

Author Options:

How to use a boolean in Arduino? Answered

I need to learn how to use a boolean in arduino. How to set it to true or false, how  to read it, and an example of using a boolean to set a led on.

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
steveastrouk
steveastrouk

Best Answer 8 years ago


From


As http://arduino.cc/en/Reference/BooleanVariables

int LEDpin = 5; // LED on pin 5
int switchPin = 13; // momentary switch on 13, other side connected to ground

boolean running = false;

void setup()
{
pinMode(LEDpin, OUTPUT);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // turn on pullup resistor
}

void loop()
{
if (digitalRead(switchPin) == LOW)
{ // switch is pressed - pullup keeps pin high normally
delay(100); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(LEDpin, running) // indicate via LED
}
}

0
Kurt Gerhard
Kurt Gerhard

Answer 8 years ago

I had tried that example but it didn't work, I need another one and a more specific example of how to set a Boolean true or false, and how to read it.
Thanks

0
frollard
frollard

Answer 8 years ago

Then your wiring is wrong. That example works.

Boolean is a value just like an integer that can take a number, but instead of 0-65535 it can take a number 0 or 1.

That's it. You can compare a boolean to true, false, 1, 0, another integer, another boolean, or use the variable itself as a condition.

You can say things like
if (running) == TRUE;
or
if (running) == 1;
or
even just
if (running); // (works with boolean operators)

0
steveastrouk
steveastrouk

Answer 8 years ago

Personally, I've got VERY careful with those brackets.

0
Kurt Gerhard
Kurt Gerhard

Answer 8 years ago

I have this code but it isnt working and I dont know why, can you help me figure out the problem??

boolean mySet = false;
const int LED = 13;
const int BUTTON = 7;


void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}

void loop() {
if (BUTTON, HIGH) { // if switch is on
boolean (mySet == true); // set bolean to true
}
if (mySet == true) { // if boolean is true
digitalWrite(LED, HIGH); // the led will turn on
}
}


0
steveastrouk
steveastrouk

Answer 8 years ago

The syntax of your loop statement is all over the place. ;-)

You have confused the COMPARISON operator '==' with the ASSIGNMENT operator '=' The boolean values "true" and "false" are synonymous with "HIGH" and "LOW"

Your line boolean (mySet == true redefines mySet  Just because it compiles doesn't make it right. 'C' is a truly, truly horrible language.

Professionally, I avoid it like the plague and always write in Pascal or Delphi.

Steve

You have confused the PIN BUTTON, with the VALUE contained on that button, which is accessed with the digitalRead statement I have added.
boolean mySet = false;
constint LED = 13;
constint BUTTON = 7;
boolean myButton;


voidsetup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}

voidloop() {
myButton=digitalRead(BUTTON);
if (myButton==true) mySet = true; // set bolean to true else 
   mySet=false;

 digitalWrite(LED, mySet);
}

0
steveastrouk
steveastrouk

Answer 8 years ago

running=TRUE; //Watch the caps.

if (running==true) {digitalWrite(apin,FALSE);} //watch those caps.

0
amisusanta2011
amisusanta2011

1 year ago

Dual channel relay is on and off fingerprint sensor when I place my register finger then relay1 is on and second time when I place the relay2 is on for a few seconds and then third time when I place my finger and the relay1 is off so how to make it in
Arduino uno..
Anybody please help me about this program.. I cannot make it I am totally confused what should I do please please help me about this problem..

0
kardoalpha
kardoalpha

5 years ago

All that is wrong is a missing semicolon as below

digitalWrite(LEDpin, running);

Good programme works well.