bool

Description

A

bool
holds one of two values,
true
or
false
. (Each
bool
variable occupies one byte of memory.)

Syntax

bool var = val;

Parameters

  • var
    : variable name.
  • val
    : the value to assign to that variable.

Example Code

This code shows how to use the

bool
datatype.

1int LEDpin = 5; // LED on pin 5
2 int switchPin = 13; // momentary switch on 13, other side connected to ground
3
4 bool running = false;
5
6 void setup() {
7 pinMode(LEDpin, OUTPUT);
8 pinMode(switchPin, INPUT);
9 digitalWrite(switchPin, HIGH); // turn on pullup resistor
10 }
11
12 void loop() {
13 if (digitalRead(switchPin) == LOW) {
14 // switch is pressed - pullup keeps pin high normally
15 delay(100); // delay to debounce switch
16 running = !running; // toggle running variable
17 digitalWrite(LEDpin, running); // indicate via LED
18 }
19 }

See also

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.