Bool flag reset. Is it possible to set it to false after a true?

I am working on a project where I am using a Pixy Cam and a TfMini range finder to trigger a cannon. I have two cannons that I am trying to fire at different times. I am able to get the first cannon to fire when the "2 stage" trigger is met with the Pixy tracking and the range finder showing within range.

The problem that I am having is that it looks like the bool I am using for the rangefinder looks to be returning the true flag and no matter what the distance is for the second cannon the will fire regardless.

I am not sure how to correct this, I had thought about possibly re-initializing the range finder after the first shot but that didn't work and I am unsure how to reset the bool flag so that it will run though it again and only fire when the second cannon is in range.

I have included the two spots I believe would be needed to spot the bug. Thank you!

bool LIDAR::inRange(uint16_t targetDist, uint16_t targetStrength) {
  uint16_t dist = tfmini.getDistance();
  uint16_t strength = tfmini.getRecentSignalStrength();

  if ((dist <= targetDist) && (strength >= targetStrength)) {
    return true;

  }
  else {
    return false;
  }
}
void loop() {

  // Find object and center camera on it 
  if (camera.updatePos()==true) {                 // Returns true if object with Signature 1 is found
    if (scope.inRange(50, 100)) {                 // within 100cm and signal strength at least 100
      scope.printDistance();
      Serial.println("Target in sight. Fire!");
      if (cannon1.fired==false) {
        cannon1.fire();
        delay(10000);                             // Wait 10 seconds after firing the first cannon
      }
      else if (cannon2.fired==false) {
             cannon2.fire();
      }
    }
    Serial.println("Next");
  }

We can't debug code we can't see. Please post your entire sketch and also some sort of diagram of how it is wired up.

To answer the question in the thread title.

Yes, you can freely change the value of a boolean variable between true and false and vice versa

Have you checked that the values for tfmini.getDistance() and tfmini.getRecentSignalStrength() are what you expect for a 'true' value? Just print them out.

I apologize for overlooking this. Here is the code I am using.

// Pin Connections
#define SOLENOID1_PIN 9
#define SOLENOID2_PIN 10
#define LIDAR_TX_PIN  18
#define LIDAR_RX_PIN  19

// PD parameters for pan servo
#define PROPORTIONAL_P  350
#define DERIVATIVE_P    500

// PD parameters for tilt servo
#define PROPORTIONAL_T  450
#define DERIVATIVE_T    550

#define PAN_SPEED       25     // Servo panning speed (0-100, higher=faster)

#include "Solenoid.h"
#include "LIDAR.h"
#include "Pixy.h"

LIDAR scope;        // Declare LIDAR
Solenoid cannon1;   // Declare turret cannon #1
Solenoid cannon2;   // Declare turret cannon #2
Pixy camera;        // Declare Pixy2 camera

void setup()
{
  Serial.begin(115200);
  scope.begin();                 // Initialize LIDAR
//  return;
  cannon1.begin(SOLENOID1_PIN);  // Initialize cannon #1
  cannon2.begin(SOLENOID2_PIN);  // Initialize cannon #2
  camera.begin(PAN_SPEED);       // Initialize Pixy2 camera 
}
//void loop() {
//scope.printDistance();
//}

void loop() {

  // Find object and center camera on it 
  if (camera.updatePos()==true) {                 // Returns true if object with Signature 1 is found
    if (scope.inRange(50, 100)) {                 // within 100cm and signal strength at least 100
      scope.printDistance();
      Serial.println("Target in sight. Fire!");
      if (cannon1.fired==false) {
        cannon1.fire();
        delay(10000);                             // Wait 10 seconds after firing the first cannon
      }
      else if (cannon2.fired==false) {
             cannon2.fire();
      }
    }
    Serial.println("Next");
  }
}

and the Lidar.h is

#include <SoftwareSerial.h>
#include "TFMini.h"

SoftwareSerial mySerial(LIDAR_TX_PIN, LIDAR_RX_PIN);
TFMini tfmini;

class LIDAR {
  public:
    LIDAR(void);
    void begin(void);
    void printDistance(void);
    bool inRange(uint16_t dist, uint16_t strength);
};

LIDAR::LIDAR(void) {
}

void LIDAR::begin(void) {
  mySerial.begin(TFMINI_BAUDRATE);
  tfmini.begin(&mySerial); 
}

/* Prints the measured distance and signal strength */
void LIDAR::printDistance(void) {
  uint16_t dist = tfmini.getDistance();
  uint16_t strength = tfmini.getRecentSignalStrength();

  Serial.print(dist);
  Serial.print(" cm\tsigstr: ");
  Serial.println(strength);
}

/* Returns true the following two conditions are true:
 * 1. Measured distance <= specified distance
 * 2. Measured signal strength >= specified signal strength
 * 
 * targetDist is in cm.
 * targetStrength is unitless.
 */
bool LIDAR::inRange(uint16_t targetDist, uint16_t targetStrength) {
  uint16_t dist = tfmini.getDistance();
  uint16_t strength = tfmini.getRecentSignalStrength();
//  Serial.print("dist: ");
//  Serial.print(dist);
//  Serial.print("\tstrength: ");
//  Serial.println(strength);

  if ((dist <= targetDist) && (strength >= targetStrength)) {
    return true;

  }
  else {
    return false;
  }
}

Connection wise the Pixy is connected via the ISCP to the Arduino NANO v3. and the pins for the range finder and solenoid (cannon) are wired to the pins accordingly

Also coming across the serial monitor this is what I was getting when I printed the distance.

36 cm sigstr: 189
Target in sight. Fire!
Cannon fired!
dist: 35 strength: 404
dist: 35 strength: 470
Next
dist: 35 strength: 503
111 cm sigstr: 252
Target in sight. Fire!
Cannon fired!
Next

That 111cm should not be triggering the cannon and that is where I am running into the problem.

This line either has the comment or the parameters wrong.

if (scope.inRange(50, 100)) {                 // within 100cm and signal strength at least 100

You should print out the values just before you use them. If possible assign them to a variable (as in your inRange method) and print out the actual values you are using in the comparison. It is possible that sensors bounce around and one read may be different from the next call. It is not clear where your debug output is from given the number of commented out sections with the same debug output.

I would simply print out the values in inRange (all the values, including the function parameters) to make sure they are correct. Either dist or targetDist are not what you think they are.

That 111cm should not be triggering the cannon and that is where I am running into the problem

Finding the problem is more than half the battle. So now it should be easy to fix.

The comment is wrong. I changed it to make it more friendly in my testing area (my desk). Since it is possible to change bool states manually would it be possible after

      if (cannon1.fired==false) {
        cannon1.fire();
        delay(10000);

to manually change the state of the bool of scope.Inrange back to false from here? For example:

      if (cannon1.fired==false) {
        cannon1.fire();
        delay(10000); 
        (chage state of scope.Inrange back to false)

Thanks for the help.

You could have tried it yourself faster than posting here.

What happens when you try it ?

Change it, print a message indicating that you have changed it then print its value as confirmation

I don't know the line to change it. I attempted it by putting

scope.inRange = !scope.inRange

But it would not compile. Im not sure if the number in scope.inRange(50,100) throw it off, or even what or where the flag for this is stored. I had help with writing the bool part of it, but the person that helped me is not available to further explain it. In looking it up I found and tried the ! command to make it a not but that didn't work.

scope.inRange() is a function in the LIDAR library that returns a boolean. It is not itself a boolean, hence the error when you try to change it.

What I think you are trying to do is to fire the cannon when the target becomes in range rather than when it is in range. If so, when the target becomes in range execute the required code and set a boolean to true. Make the execution of the code dependant on the boolean being false. When the target moves out of range set the boolean to false ready for the next time the target becomes in range