Go Down

Topic: Boolean not changing (Read 74 times) previous topic - next topic

yuukyori

I am using a boolean to track if the user has taken their medication.

In my code, the medicationTaken variable was assigned a false value.

When I upload my code, my LDR sensor is in a dark container and logs the correct message in the log: " Medication not taken" and the medicationTaken variable is false.

However, once I open the container, it logs the correct message: " User taking medication", but the medicationTaken variable does not change into true.

So when I close the container again, it goes in a loop and says " Medication not taken" rather than the correct message: "  User already took medication".

What am I doing wrong?

Blackfin

You have a global:

Code: [Select]
// variable for medicationTracking
boolean medicationTaken = false;


and a local:

Code: [Select]
void printCurrentTime(int photoCellReading, boolean medicationTaken){...

Inside the function printCurrentTime, any operation on "medicationTaken" will be on the local version.

Since the variable is already global, there's no need to pass it as an argument to your function.

johnwasser

Code: [Select]
// print to the serial monitor
void printCurrentTime(int photoCellReading, boolean medicationTaken){



You have a function argument with the same name as a global variable.  Inside this function you are only setting the COPY of the value that was passed to the function.  It does not change the global.


The best way to fix it:
Code: [Select]
// print to the serial monitor
void printCurrentTime(int photoCellReading){  // This uses the global directly


An alternative way, of you had more than one "medicationTaken" flag to pass to 'printCurrentTime()':
Code: [Select]
// print to the serial monitor
void printCurrentTime(int photoCellReading, boolean & medicationTaken){  // This passes the argument "by reference" instead of "by value"
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up