Page 60 - Programming the Photon Getting Started With the Internet of Things
P. 60
Boolean
Another common type of variable is a Boolean, which has logical values—that is, the
value is either true or false. The best example of using Boolean logic is using the if
statement. The condition set in an if statement can only be true or false.
int LEDpin = 5; // LED on pin 5
int switchPin = 13; // momentary switch on 13, other side connected to gr
ound
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
}
}
In addition, you can manipulate values using Boolean operators. These are similar to
performing arithmetic calculations. The most commonly used Boolean operators are and,
which is written as &&; and or, which is written as ||. In addition to these operators there
is not, which is written using !. This value can either be “not true” or “not false.” Table
3.1 shows all the data types that are available and which format of values should be used.