Custom Function Issue

Hi all,

I’ve wrote a custom function, and its actual output varies predictably from what I intended it to do (the code and and expected vs actual “results” are included). Does anyone happen to know why this is the case and how, if at all, it could be fixed?

code:

int i;
bool 
gLED,
IRSensorState,
lickSensorState,
solenoidState,
rewardState;


void changeDetecRecorder (bool gLED, bool IRSensorState, bool lickSensorState, bool solenoidState, bool rewardState, int select) {
  if (select == 1) {
    if (gLED == 1) {
      gLED = 0;
    } else {
      gLED = 1;
    };
  };
  if (select == 2) {
    if (IRSensorState == 1) {
      IRSensorState == 0;
    } else {
      IRSensorState = 1;
    };
  };
  if (select == 3) {
    if (lickSensorState == 1) {
      lickSensorState == 0;
    } else {
      lickSensorState = 1;
    };
  };
  if (select == 4) {
    if (solenoidState == 1) {
      solenoidState = 0;
    } else {
      solenoidState = 1;
    };
  };
  if (select == 5) {
    if (rewardState == 1) {
      rewardState = 0;
    } else {
      rewardState = 1;
    };
  };
  //  String fileTitle = "";
  //  File myFile = SD.open (fileTitle, FILE_WRITE);
  //  myFile.print (millis ());
  //  myFile.print ("_");
  //  myFile.print (gLED);
  //  myFile.print (IRSensorState);
  //  myFile.print (lickSensorState);
  //  myFile.print (solenoidState);
  //  myFile.println (rewardState);
  //  myFile.close ();
  Serial.print (millis ());
  Serial.print ("_");
  Serial.print (gLED);
  Serial.print (IRSensorState);
  Serial.print (lickSensorState);
  Serial.print (solenoidState);
  Serial.println (rewardState);

}


void setup () {
  Serial.begin (9600);

}

void loop () {
  for (i = 1; i <= 5; i++) {
    changeDetecRecorder (gLED, IRSensorState, lickSensorState, solenoidState, rewardState, i);
    delay (2000);
  };
}

Expected results (as read in the serial monitor and assuming any arbitrary value for the “timestamp”):

timestamp_10000
timestamp_11000
timestamp_11100
timestamp_11110
timestamp_11111
timestamp_01111
timestamp_00111
timestamp_00011
timestamp_00001
timestamp_00000
timestamp_10000

and so on.

Actual results:

timestamp_10000
timestamp_10000
timestamp_01000
timestamp_00100
timestamp_00010
timestamp_00001

and so on.

Thank you again, and please let me know if I can provide additional information.

It’s printing the results I would expect.

It looks like you think that the variables passed by value are being passed by reference. They are not, they are passed by value, and the variables visible in that function are thus locally scoped - the changes you make to all those variables are not changing the global variables of the same names.

If you remove all the arguments from the function except the last one, I think it will do what you’re expecting. Either do that, using the globals directly. If you for some reason need to call that with different variables being used for each of the arguments (pretty sure you do not), you need to pass pointers, not the values.

One way to fix it is to use the global variables directly instead of passing copies of them as arguments to the function. Also, switch/case is good for comparing an integer variable against a list of constants and the boolean inverse operator (!) is good for toggling booleans.

int i;
bool
gLED,
IRSensorState,
lickSensorState,
solenoidState,
rewardState;

void changeDetecRecorder (int select) {
  switch (select)
  {
    case 1:
      gLED = !gLED;
      break;
    case 2:
      IRSensorState = !IRSensorState;
      break;
    case 3:
      lickSensorState = !lickSensorState;
      break;
    case 4:
      solenoidState = !solenoidState;
      break;
    case 5:
      rewardState = !rewardState;
      break;
  }


  //  String fileTitle = "";
  //  File myFile = SD.open (fileTitle, FILE_WRITE);
  //  myFile.print (millis ());
  //  myFile.print ("_");
  //  myFile.print (gLED);
  //  myFile.print (IRSensorState);
  //  myFile.print (lickSensorState);
  //  myFile.print (solenoidState);
  //  myFile.println (rewardState);
  //  myFile.close ();
  Serial.print (millis ());
  Serial.print ("_");
  Serial.print (gLED);
  Serial.print (IRSensorState);
  Serial.print (lickSensorState);
  Serial.print (solenoidState);
  Serial.println (rewardState);
}




void setup () {
  Serial.begin (9600);
}


void loop () {
  for (i = 1; i <= 5; i++) {
    changeDetecRecorder(i);
    delay (2000);
  }
}

Wow, thank you all so much! I learned so much from this. I'll report back once I get everything implemented.