aboutsummaryrefslogtreecommitdiffstats
path: root/docs/source/arduino/booleanvariables.rst
blob: 772db0f253dd8d00162ca30638b5da12781a0730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
.. highlight:: cpp

.. _arduino-booleanvariables:

Booleans
========

A **boolean** holds one of two values, :ref:`true
<arduino-constants-true>` or :ref:`false <arduino-constants-false>`.
On a Maple, each boolean variable occupies one byte of memory, and has
type ``bool``.

.. warning::

   On an Arduino, the type ``boolean`` is also provided.  While the
   Maple also has this type for compatibility, **its use is strongly
   discouraged**.  The ``bool`` type is a standard part of C++, while
   ``boolean`` is a non-standard extension that serves no purpose.

Example
-------

::

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

    // running is a boolean variable:
    bool 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
      }
    }

See also
--------


-  :ref:`Boolean constants <arduino-constants-bool>`
-  :ref:`Boolean operators <arduino-boolean>`
-  :ref:`Variables <arduino-variables>`