Problem, Function returning true even thou the condition inside is false

Hi

Everybody. I’m new to Arduino and need some help. I’m writing a program to control my interior light in my old car. It has 2 door switches and one resilient switch to control the light when already inside the car.

When it detects a door opening it turn on the light and holds it so. When the door has clsed a timer will check if both doors are shut and if they has been opened, if so the timer starts and keeping the light on for 10 seconds.

During this time I wants to check if someone opens the doors again or pressing the switch for the interior light always ON. If so the timer must break and return to whatever is happening.

Bu here is my problem, when the timer start it runs a do…while loop and inside that I have put the if condition for calling a function to return TRUE if some of the switches has been active again.

But it always breaks the doo.while loop even thou none of the switches has activated, I’ve checked the output from this with Serial monitor and it never reaches the subfunction, if I changes the if condition to “if (!SwitchBreaker())” it will go inside but not breaking as expected…

Can anyone see what I have done wrong?

// Function LightsOnTimer, this function is keeping the light on for a specified amount of time
// the timer will stop and restart if another door is open during the countdown.
void LOT(){
  // Call the CSS() function Read all switches and store there state in the SWstate array
  CSS();
  // Check the door switches and the flag for once opened and now closed door to start the timer for interiorlight ON timer
  if (PrevDoorState && !SWstate[0] && !SWstate[1]){
    PrevMillis = millis();
    do{
      // Check if one or more switches has been activated during the TimerOn, if so break the timer
      if (SwitchBreaker()){
        break;
      }
    } while (millis() - PrevMillis < OnTimer);    // This condition will run the loop until it reaches the OnTimer value
    digitalWrite(OutPin, false);
    PrevDoorState = false;                                // Set the OpenDoor flag back so it will sense and start timer again if a door opened ad closed
  }
}

// Function SwitchBreaker, this function returns true if a door or the interiorlight switch is pressed
bool SwitchBreaker(){
  // Call the CSS() function Read all switches and store there state in the SWstate array
  CSS();
  // Check the SWstate array for active switches
  for (int i = 0; i < 3; i++){
    if (SWstate[i]){
      return true;      
    }
  }
}

I don't do keyhole debugging.
Please post your code.

Ok. Here’s the full code. Btw I do appreciate if can have smart tips on how to make this code far better and less lines of code. :slight_smile:

I forgott to mention that the code isn’t done yet, some parts are missing such as resetting the timer when interrupted in the do…while loop and fading of the light function after OnTimer has finished is also missing, but as I got stuck on that just anoying thing so I asked for help even thou…

// Include external libraries
#include <Bounce2.h>

// Define ports to be used with Bounce class
#define NumButtons 3                                              // Set the number of buttons connected
const uint8_t BtnPins[NumButtons] = {2, 3, 4};        // Set the number of buttons and pins connected

// Create class objects
Bounce * debo = new Bounce[NumButtons];           // Create a Bounce object for all buttons

// Set variables used in the program
const int OutPin = 9;                                                 // The number of the Output pin used to activate the lights
const int OnTimer = 10000;
unsigned long PrevMillis = 0;
bool PrevDoorState = false;                                      // Set PrevDoorState to false to avoid start ontimer
bool SWstate[3] = {false, false, false};                     // This will store the previous state of the button


// Initialize setup
void setup() {
  pinMode(OutPin, OUTPUT);                                    // Set pin 9 as output:
  for (int i = 0; i < NumButtons; i++){
    debo[i].attach(BtnPins[i]);                                   // Set the debounce instance for each one of the buttons
    debo[i].interval(25);                                              // Set the debounce time interval to 25 ms
  }
  Serial.begin(9600);
}

// Initialize functions
// Function CheckSwitchState, checks the door and interior light switches, it store the result in an array
void CSS(){
  debo[0].update();                                                 // Update the bounce instance for RightDoor
  debo[1].update();                                                 // Update the bounce instance for LeftDoor
  debo[2].update();                                                 // Update the bounce instance for InteriorSwitch
  SWstate[0] = !debo[0].read();                             // Read the RightDoor status and store it in the array
  SWstate[1] = !debo[1].read();                             // Read the LeftDoor status and stire it in the array
  if (debo[2].fell()){
    SWstate[2] = !SWstate[2];                                 // Read the InteriorLight and toggle if activated
  }
}

// Function TurnOnInteriorLight, this will check if any or none switch is ative at turn on or off the light
void TOIL(){
  // Call the CSS() function Read all switches and store there state in the SWstate array
  CSS();
  // Check the resilient switch and turn on the always ON light function
  if (SWstate[2]){
    digitalWrite(OutPin, SWstate[2]);
    PrevDoorState = false;
  } else {
    if (!SWstate[0] && !SWstate[1]){
      digitalWrite(OutPin, SWstate[2]);
    }
    // Check if one of the doors has been opened, if so. Turn on the light and hold it lit. Set flag for Timer PrevDoorState
    if (SWstate [0] || SWstate[1]){
      digitalWrite(OutPin, HIGH);
      PrevDoorState = true;
    }
    }
}

// Function LightsOnTimer, this function is keeping the light on for a specified amount of time
// the timer will stop and restart if another door is open during the countdown.
void LOT(){
  // Call the CSS() function Read all switches and store there state in the SWstate array
  CSS();
  // Check the door switches and the flag for once opened and now closed door to start the timer for interiorlight ON timer
  if (PrevDoorState && !SWstate[0] && !SWstate[1]){
    PrevMillis = millis();
    do{
      // Check if one or more switches has been activated during the TimerOn, if so break the timer
      if (SwitchBreaker()){
        break;
      }
    } while (millis() - PrevMillis < OnTimer);    // This condition will run the loop until it reaches the OnTimer value
    digitalWrite(OutPin, false);
    PrevDoorState = false;                                // Set the OpenDoor flag back so it will sense and start timer again if a door opened ad closed
  }
}

// Function SwitchBreaker, this function returns true if a door or the interiorlight switch is pressed
bool SwitchBreaker(){
  // Call the CSS() function Read all switches and store there state in the SWstate array
  CSS();
  // Check the SWstate array for active switches
  for (int i = 0; i < 3; i++){
    if (SWstate[i]){
      return true;      
    }
  }
}

// Start main code, here you will run all your code for the program
void loop(){
  TOIL();
  LOT();
}

What happens inside your SwitchBreaker() function if none of the switches are active? You just finish up the routine and return some random value (which may be true). You need to specifically return false

bool SwitchBreaker() {
  // Call the CSS() function Read all switches and store there state in the SWstate array
  CSS();
  // Check the SWstate array for active switches
  for (int i = 0; i < 3; i++) {
    if (SWstate[i]) {
      return true;
    }
  }
  return false;
}

A gold star to you. I've totally missed that. Yes of course the function needs a false return as well if none of the switches are active...Sometimes it just takes a fresh pair of eyes to solve something :slight_smile:

I'm really grateful. I works now and now it just has some minor bugs to straighten out before field tests.

Thanks //ZerQ