Boolean Variables

Hello, I have a few questions about variables. Please be understanding as I am not an experienced programmer.

I would like to read a digital input, then store it as a Boolean variable.
could I accomplish this by boolean variable = digitalRead(pin) or do I have to do something along the lines of

if(digitalRead(pin)==HIGH){
     boolean variable = true;
}

I thought that the first would work because HIGH and true would both equal 1, but I just wanted to make sure. Thanks!

could I accomplish this by

Yes, because the values returned by digitalRead(), HIGH and LOW, correspond, value-wise, to true and false.

If you are concerned with digitalRead() returning other values in the future, you could use:

boolean flag = (figitalRead(somePin) == HIGH);

to set flag to true when digitalRead() returns HIGH.

figitalRead -- returns TRUE if someone fidgets with the pin?

Delta_G:
figitalRead -- returns TRUE if someone fidgets with the pin?

Yep. For detecting those tentative presses.

PaulS:

boolean flag = (figitalRead(somePin) == HIGH);

No need for parentheses, assignment binds less tightly than other operators:

boolean flag = digitalRead(somePin) == HIGH ;

but just stick to

boolean flag = digitalRead(somePin) ;

but choose a meaningful variable name, not "flag"!!

No need for parentheses, assignment binds less tightly than other operators:

You might be one of the 8 people in the world that can remember that. I use parentheses so I KNOW the order of the operators I use less often.

but choose a meaningful variable name, not "flag"!!

flag just might BE meaningful. In general, I agree, but is was simply an example.

So was the "figitalread" a typeo, and you were being sarcastic? Because I looked it up on the playground and nothing showed up. Also, for future reference, could I use 1/0 in place of HIGH/LOW or true/false?

I am guessing that I cannot append Boolean variables to a string, so could I take the 1/0 from the digitalRead, store it as a character, then append it to a string? Or does digitalRead only return an integer? What I'm trying to do is to take the state (1/0) from 5 IR sensors and append them to a string to make a code, which I will use in other parts of my program.

So was the "figitalread" a typeo, and you were being sarcastic?

You try typing with carpal tunnel, and see how well you do.

Also, for future reference, could I use 1/0 in place of HIGH/LOW or true/false?

Yes. I don't recommend it. The digital pins return HIGH and LOW, since they represent states. If you are comparing results of a call to digitalRead(), you should use the names that the function is expected to return. Boolean variables are designed to represent true or false. Use those names when assigning values. Doing so makes your code clearer.

I am guessing that I cannot append Boolean variables to a string

There are no functions for doing so. On the other hand, a string is just an array of chars, so setting the element in the array isn't rocket science.

so could I take the 1/0 from the digitalRead, store it as a character, then append it to a string?

Yes. But, 0 and 1 are not printable characters. In fact, 0 is the NULL terminator. Adding '0' to the value would let you store '0' or '1' in the array.

Or does digitalRead only return an integer?

It returns an int. But the value in the int fits in a byte or char.

What I'm trying to do is to take the state (1/0) from 5 IR sensors and append them to a string to make a code,

Why a string?

I have realized that I need a lot more help than I thought I did.. Here is the link to my new thread
http://forum.arduino.cc/index.php?topic=220787.msg1605858#msg1605858