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
: variable name.var
: the value to assign to that variable.val
Example Code
This code shows how to use the
bool
datatype.1int LEDpin = 5; // LED on pin 52 int switchPin = 13; // momentary switch on 13, other side connected to ground3
4 bool running = false;5
6 void setup() {7 pinMode(LEDpin, OUTPUT);8 pinMode(switchPin, INPUT);9 digitalWrite(switchPin, HIGH); // turn on pullup resistor10 }11
12 void loop() {13 if (digitalRead(switchPin) == LOW) {14 // switch is pressed - pullup keeps pin high normally15 delay(100); // delay to debounce switch16 running = !running; // toggle running variable17 digitalWrite(LEDpin, running); // indicate via LED18 }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.