bool with a choice. [SOLVED]

bool with a choice as I would call it.
A while ago I saw this bit of code from someone on the internet, I have used it for one of my projects and had a basic understanding of how it worked.
It has worked since then.
But in the past week, I have started a new project and thought that I could reuse it.
And it failed on me. It will only return false, and I don’t understand why.
I only changed the variables.

numofscreens is a const int,
currentScreen is an int.
screens is a const int array.

the function:
It checks if the current screen is equal to one of the numbers in the screens array if so it will return true otherwise it returns false.

bool access()
{
  for (int k = 0; k < screens; k++) {
    if (currentScreen == screens[k]) {
      return true;
    }   
    else if (currentScreen != screens[k]) {
      return false;
    }
  }
}

Welcome,

k < screens

?

You probably want to do this instead:

k < sizeof(screens)/sizeof(screens[0])

Or

k < numofscreens

But there is a logic problem in your function, the loop will immediately return true or false after checking the first element of the array, other elements of the array are not checked. Maybe you want to do this:

bool access()
{
  for (int k = 0; k < numofscreens; k++) {
    if (currentScreen == screens[k]) {
      return true;
    }
  }
  return false;
}

Or a non-iterative solution:

bool access2(const int *screens, int numofscreens)
{
    return numofscreens > 0 && (currentScreen == screens[0] || access2(screens + 1, numofscreens - 1));
}

bool access()
{
    return access2(screens, numofscreens);
}

:frowning:

guix:
You probably want to do this instead:

k < sizeof(screens)/sizeof(screens[0])

Or

k < numofscreens

thanks a lot for the reply.
I made a typo in my new code there indeed need to be numofscreens there. but still, it doesn’t work the way I want it to work. I will have a further look later in the day what I could do.

guix:
But there is a logic problem in your function, the loop will immediately return true or false after checking the first element of the array, other elements of the array are not checked. Maybe you want to do this:

bool access()

{
  for (int k = 0; k < numofscreens; k++) {
    if (currentScreen == screens[k]) {
      return true;
    }
  }
  return false;
}

christop:
Or a non-iterative solution:

bool access2(const int *screens, int numofscreens)

{
    return numofscreens > 0 && (currentScreen == screens[0] || access2(screens + 1, numofscreens - 1));
}

bool access()
{
    return access2(screens, numofscreens);
}




>:(

thanks a lot for the help.

I will have a look later in the day which code could help me and then I will post an update.

In the past week, I have been trying to get it working and keep running into an issue.
The code from guix is correct but still, it doesn’t work.

guix:

bool access()

{
  for (int k = 0; k < numofscreens; k++) {
    if (currentScreen == screens[k]) {
      return true;
    }
  }
  return false;
}

The bool is stuck on returning true even when it needs to be false.
and I don’t know how or why it’s doing this
when I replace the return true to false, it stays true.

What the problem could be is that somehow the bool is declared as a const int.

Post a small program just enough to demonstrate the problem you have

sebasdt1:
The bool is stuck on returning true even when it needs to be false.

How do you know?

The function code as posted is correct, so it must be either...
a) Your data (values of one of the variables or in the array)
b) How you are calling it

guix:
Post a small program just enough to demonstrate the problem you have

so I made one and it gave me an error.
I used a bit of code from the denounce example.

const int numofscreens2 = 7;
const int numofscreens = 2;
const int screens[numofscreens] = {5, 6
                                  };
int currentScreen;
boolean access()
{
  for (int k = 0; k < numofscreens; k++) {
    if (currentScreen == screens[k]) {
      return true;
    }
  }
  return false;
}
const int buttonPin = 25;     // the number of the pushbutton pin
const int ledPin =  LED_BUILTIN;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    Serial.print("currentScreen= ");
    Serial.println(currentScreen);
    if (currentScreen == numofscreens2 - 1) {
      currentScreen = 0;
    }
    else {
      currentScreen++;

    }
    if (access) {
      // set the LED using the state of the button:
      Serial.print("Access= true");
      digitalWrite(ledPin, HIGH);

    }
    else {
      Serial.print("Access= false ");
      digitalWrite(ledPin, LOW);
    }

  }

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

I think that the program is asking the bool to fast what to do.

compiler error:
 
error: cannot resolve overloaded function 'access' based on conversion to type 'bool'

     if (access) {

               ^

exit status 1
cannot resolve overloaded function 'access' based on conversion to type 'bool'

There should be a pair of brackets after access.

bool not boolean

wildbill:
There should be a pair of brackets after access.

thanks for that bit of advice, now it’s working!!

gonna see if this will fix the problem in my main project!

here below is the correct code.
if you switch the false to true in the bool it will invert.
I was stuck on that for a half an hour :’(
I have simplified the code, now I change currentScreen from 0 to 7

const int numofscreens = 2;
  const int screens[numofscreens] = {1,  5
                                    };
int currentScreen = 2;
bool access()
{
  for (int k = 0; k < numofscreens; k++) {
    if (currentScreen == screens[k]) {
      return false;
    }
  }
  return true;
}
const int ledPin =  LED_BUILTIN;      // the number of the LED pin



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Begin");

  pinMode(ledPin, OUTPUT);
}

void loop() {
 Serial.println(currentScreen);
Serial.println(access());
    if (access()) {
      // set the LED using the state of the bool:
      Serial.println("Access= true");
      digitalWrite(ledPin, HIGH);

    }
    else {
      Serial.println("Access= false ");
      digitalWrite(ledPin, LOW);
    }

}

Thank y’all allot for helping me with this problem.
The problem has been solved!

here below is the correct final version:

const int numofscreens = 2;
const int screens[numofscreens] = {1,  5
                                    };
int currentScreen = 2;
bool access()
{
  for (int k = 0; k < numofscreens; k++) {
    if (currentScreen == screens[k]) {
      return false;
    }
  }
  return true;
}
const int ledPin =  LED_BUILTIN;      // the number of the LED pin



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Begin");

  pinMode(ledPin, OUTPUT);
}

void loop() {
 Serial.println(currentScreen);
Serial.println(access());
    if (access()) {
      // set the LED using the state of the bool:
      Serial.println("Access= true");
      digitalWrite(ledPin, HIGH);

    }
    else {
      Serial.println("Access= false ");
      digitalWrite(ledPin, LOW);
    }

}