This shows you the differences between two versions of the page.
arduino:data_types:booleanvariables [2013/02/15 20:45] |
arduino:data_types:booleanvariables [2024/02/16 01:04] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== boolean ====== | ||
+ | |||
+ | A **boolean** holds one of two values, [[arduino:variable scope and qualifiers:Constants|true]] or [[arduino:variable scope and qualifiers:Constants|false]]. (Each boolean variable occupies one byte of memory.) | ||
+ | |||
+ | ==== Example ==== | ||
+ | <code arduino> | ||
+ | int LEDpin = 5; // LED on pin 5 | ||
+ | int switchPin = 13; // momentary switch on 13, other side connected to ground | ||
+ | |||
+ | boolean 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 | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | </code> | ||
+ | |||
+ | ==== See also ==== | ||
+ | * [[arduino:variable scope and qualifiers:Constants]] | ||
+ | * [[arduino:boolean operators:Boolean | boolean operators]] | ||
+ | * [[arduino:variable scope and qualifiers:VariableDeclaration|Variable Declaration]] | ||
+ | Source: arduino.cc | ||