Confused about Boolean statement

Hello everyone,

I'm a bit confused about the boolean statements I have in my code. I am controlling a relay and an LED.

Basically when a button is pressed the relay is engaged, and at the same time I want the led to turn on.

This is working, but the following two statements are confusing me.

boolean relayOff = true; // The present state of the relay (on/off)
boolean ledOff = false; // The present state of the LED (on/off)

If relayOff = true, to me it means the relay is off, so why in the case of the ledOff = false the led of actually off. Shouldn't it be true like the relay is?

I've tried various combinations, but I just can't see it.

Hope someone can help and explain the problem.

Thanks,

Robert

/*
 * Modified by Robert Budnikas
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-relay
 */

const int BUTTON_PIN = 2;                             // Arduino pin connected to button's pin
const int RELAY_PIN = 3;                              // Arduino pin connected to relay's pin
const int LED_PIN = 9;                                // The LED is connected to pin 9

boolean lastButton = LOW;                             // Variable containing the previous button state
boolean currentButton = LOW;                          // Variable containing the current button state
boolean relayOff = true;                              // The present state of the relay (on/off)
boolean ledOff = false;                                // The present state of the LED (on/off)


void setup() {
  Serial.begin(9600);                                 // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP);                  // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);                         // set arduino pin to output mode
  pinMode(LED_PIN, OUTPUT);                           // Set the LED pin as an output
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(BUTTON_PIN);          // Read the button state
  if (last != current)                                // If it's different…
  {
    current = digitalRead(BUTTON_PIN);                // Read it again
  }
  return current;                                     // Return the current value
}

void loop() 
{
  currentButton = debounce(lastButton);               // Read debounced state
  if (lastButton == LOW && currentButton == HIGH)     // If it was pressed…
  {
    relayOff = !relayOff;                             // Toggle the RELAY value
    ledOff  = !ledOff;                                  // Toggle the LED value
  }
  lastButton = currentButton;                         // Reset button value
  
  digitalWrite(RELAY_PIN, relayOff);                  // Change the RELAY state
  digitalWrite(LED_PIN, ledOff);                       // Change the LED state
  
  int buttonState = digitalRead(BUTTON_PIN);          // read new state

  if (buttonState == LOW) {
    Serial.println("Power off");
    digitalWrite(RELAY_PIN, HIGH);                    // turn on
    digitalWrite(LED_PIN, HIGH);                      // turn on
  }
  else
  if (buttonState == HIGH) {
    Serial.println("Power on");
    digitalWrite(RELAY_PIN, LOW);                     // turn off
    digitalWrite(LED_PIN, LOW);                       // turn off
  }
}
boolean relayOff = true;                     // The present state of the relay (on/off)
boolean ledOff = false;                                // The present state of the LED (on/off)

Those declarations are just setting the initial values. Initially, it is 'true' that the relay is off and and 'false' that the LED is off. At least that is what the sketch implies.

Since those variables are passed directly to digitalWrite() it would make more sense to call them RelayPinState and LEDPinState and, perhaps, use the values LOW and HIGH instead of 'false' and 'true'.

If relayOff = true, to me it means the relay is off, so why in the case of the ledOff = false the led of actually off. Shouldn't it be true like the relay is?

Boolean variables are just like any other variable and can be defined as anything you want. There are no rules, they contain names and values that the programmer wants.

The name ledoff when false means the led is on. It is simply a matter of style what you use, it could have been called ledOn in which case then you might expect it to be true when the led is on and false when it is off.

But it doesn’t matter, even if it is counterintuitive the code will continue to function like it was programmed to do. It is just a matter of style.

If relayOff = true, to me it means the relay is off, so why in the case of the ledOff = false the led of actually off.

It also has to do with how the components are wired. The LED wired output-resistor-ground takes a HIGH (true in this case) to light the LED and a LOW to turn it off. The relay may be wired through a opto isolator. The wiring of the relay is Vcc-opto LED-resistor-output. So it takes a LOW to turn on the relay and HIGH to turn it off.
true false.jpg

true false.jpg

It's a good idea to choose the polarity that tells what's most logical to You.
Using the name xxxOff and then face the state "false" gives You double negations. Anybody can get lost.
I would recommend to use positive definitions and names like xxxOn. If a HIGH or a LOW in the digitalWrite is needed for the xxxOn state is a question for the I/O handling part of the code. Function names like relayOn resp. realyOff will make You free from remembering the electronical last piece. It's all handled by the two functions.

Part of the problem is that most relay modules sold for use with an Arduino are Active Low. That means the relay coil is ON when the input is LOW and OFF when the input is HIGH. Since the boolean variables are written directly to the pins, relayOff turns the relay off when true/HIGH but probably ledOff turns the ledON when HIGH/true. Bad choice of names, if you ask me. :slight_smile:

johnwasser:
Part of the problem is that most relay modules sold for use with an Arduino are Active Low. That means the relay coil is ON when the input is LOW and OFF when the input is HIGH. Since the boolean variables are written directly to the pins, relayOff turns the relay off when true/HIGH but probably ledOff turns the ledON when HIGH/true. Bad choice of names, if you ask me. :slight_smile:

Is that really good coding, using a boolean as the fysicly controlling item? Why not use thinking modell BIOS? Leave the hardware doings to output funcions and release the code from the fysics?
In order to save a few lines the entire code gets more tricky. Is it worth it?

I think reply #1 nailed it.

aarg:
I think reply #1 nailed it.

Reply #6?

Railroader:
Reply #6?

Is correct, but #1 was first. :slight_smile: