Boolean experiment

//Arduino and programming level: Beginner

Helllllo :)

So I did a little Boolean experiment where I wanted to see what would happen if I wrote a basic program replacing the "int" with a "boolean" to make the led (pin13) blink on and off.

I wasn't expecting much to happen however what did happen interested me enough tom ask about it on the forum.

Program: boolean led = 13; void setup() { pinMode(led, 1);

}

void loop() { digitalWrite(led, 1); delay (500); digitalWrite(led,0); delay(5000);

}

As soon as I loaded the program to my arduino (uno) the led light went on but the TRANSCEIVER (tx) led was was blinking according to what my "delay" parameters were set to.

Does anyone know why this affected the tx led?

Thanks :)

Because tx is pin 1. And 13 in binairy is 0b1101. And the bool can only store one bit and that would be the LSB (less significant bit aka most right bit) and that happens to be 1.

Very good experiment!

septillion: Because tx is pin 1. And 13 in binairy is 0b1101. And the bool can only store one bit and that would be the LSB (less significant bit aka most right bit) and that happens to be 1.

what if you used

boolean Board_mounted_led = 13;

I do not understand why the Boolean would care about the name or the pin, just the value.

I am missing something.

What

It doesn't care the name. But when you store a value in a boolean, if the value you are storing is NOT zero then it stores 1. If the value you are storing IS zero then it stores zero.

So when you write

boolean anything = 13;

what actually gets stored in anything is a 1.

So when you then use that as a pin number in a digitalWrite statement you are actually writing to pin 1 and not 13.

boolean is a 1 or a zero,
int is an 16 bit integer
int8_t is an 8 bit integer.
13 can not be boolean.

what actually gets stored in anything is a 1.

Actually anything stores 13. It is a 1 because it is not zero?

I am still learning C++ Arduino and am curious so I tried the following:

boolean led = 13;

void setup()
{
Serial.begin(9600);
Serial.print(led);  // prints 13
pinMode(led, led);  // (OUTPUT = 1)

}

void loop()
{
// led on and off 1300 mS.

digitalWrite(led, led);
delay(led * 100);
digitalWrite(led, !led);
delay(led * 100);
}

The pin 13 led flashes on and off. So led can be a boolean and byte both?

It depends on what version of the IDE you have. Things recently changed concerning what actually happens with boolean.

If you stick with the real C++ type bool, things will be a little more consistent across versions.

this is confusing.

you have a variable, you can name it anything you want. LED1 or front_panel_LED or whatever.

that variable can point to a pin. pins are fixed addresses both for analog and digital

the only discussion about boolean should be about the type of values the register accepts for the value

if you have a pin, it is not a boolean location. if you create a name for the pin, it is not a boolean name, after all, it is an address.

what values any of the digital pins can either input or output are boolean. you cannot change that, it is the nature of the whole digital thing.

I think I understand what the OP did. instead off initiating the pin, or defining a name for the pin, boolean was chosen. this was just an improper use in trying to set the values a digital pin can have.

kind of like saying your light switch is boolean. or boolean boolean.

I think I finally wrapped my head around that. now the whole boolean thing.....

you have a very fixed state. 0 this is zero, or boolean 0. we think of the other boolean state as one, but I think the true definition is anything not 0