Skip to content
Advertisement

Boolean flags aren’t corresponding to the switch they are in

I’m trying to make a traffic simulator and I completely underestimated how hard it was going to be to get all the classes to work together. I’ve finally gotten all the functions to work quite well, and am now I’m trying to put them together. On my lights algorithm I’ve gotten the lights to change fine on a timer, but then I needed to tie in a boolean that was attached to the lights color and then use it outside that class to tell the car to move or not (Failed Miserably). So now I’ve brought the Car classes .move/.draw to the lights class instead, although for some reason the booleans won’t correspond to the switch their in although there’s a variable that changes the light so I don’t see why my boolean continually switches from true to false when the lights in that switch are staying the same I’ve put in println to show how the lights are changing fine but the flags arent. I’ve commented out a couple different places I’ve tried placing the car.draw sections.

I’ve edited the code to just resemble how the boolean flags aren’t corresponding with the lights changing.

Also if I can get the flags to work how can I call upon that flag in another class?

  int greenX, greenY = 50;   // Alphas of Green Lights (50 = OFF & 250 = ON)
  int redX, redY = 50;       // Alphas of Red Lights (50 = OFF & 250 = ON)
  int yellowX, yellowY = 50; // Alphas of Yellow Lights (50 = OFF & 250 = ON) 
  int time;                  // set to equal millis()
  int lastInterval;          // Used to update time since last light change then used to take away from "time" in order to get amount of time specified
  float interval = 5;        // Interval for Lights in Seconds
  float wait = 1000*interval; // turn interval from seconds to millis
  char lightX = 'G';         // Switch for Lights on X-Cords
  char lightY = 'R';         // Switch for Lights on Y-Cords
  boolean X, Y, Xy, Yy;  // Booleans for Lights to corespond to draw/make car (Xy & Yy = yellow lights)

void setup() {
}

  void draw() {
    time = millis();
    println("Last Interval Times is " + lastInterval/1000);

    if (time - lastInterval < wait) { //  // If the Light has been green for less than wait time
      if ((X == true) && (Y == false)) {
    }
      println("X-Light = " + X);
      println("Y-Light = " + Y);
    } 

    if (time - lastInterval >= wait) {  // If the Light has been green for more than wait time
        X = !X;
        Y = !Y;
      if (lightX == 'G' && lightY == 'R') {
        lightX = 'Y';
        lightY = 'R';
      } 
      else if (lightX == 'Y' && lightY == 'R') {
        lightX = 'R';
        lightY = 'G';
      } 
      else if (lightX == 'R' && lightY == 'G') {
        lightX = 'R';
        lightY = 'Y';
      }
      else if (lightX == 'R' && lightY == 'Y') {
        lightX = 'G';
        lightY = 'R';
      }
      lastInterval = time; // Update last Interval to time of light change
    }


    switch(lightX) {        //Switch For X-Cord Lights
    case 'G':              // Green Light
      println("X-Axis = Green Light");
      time = millis();
      greenX = 250;
      yellowX = 50;
      redX = 50;
      X = true;
      Xy = true;
      break;

    case 'Y':              // Yellow Light
      println("X-Axis = Yellow Light");
      time = millis();
      greenX = 50;
      yellowX = 250;
      redX = 50;
      Xy = true;
      X = !X;
      break;


    case 'R':             //Red Light
      println("X-Axis = Red Light");
      time = millis();
      greenX = 50;
      yellowX = 50;
      redX = 250;
      Xy = !X;
      X = !X;
      break;
    }

    switch(lightY) {       //Switch For Y-Cord Lights
    case 'G':              // Green Light
      println("Y-Axis = Green Light");
      println(time/1000);
      println();
      time = millis();
      greenY = 250;
      yellowY = 50;
      redY = 50;
      Y = true;
      Yy = true;
      break;

    case 'Y':              // Yellow Light
      println("Y-Axis = Yellow Light");
      println(time/1000);
      println();
      time = millis();
      greenY = 50;
      yellowY = 250;
      redY = 50;
      Yy = true;
      Y = !Y;
      break;


    case 'R':             //Red Light
      println("Y-Axis = Red Light");
      println(time/1000);
      println();
      time = millis();
      greenY = 50;
      yellowY = 50;
      redY = 250;
      Y = !Y;
      Yy = !Yy;
      break;
    }
  }

Advertisement

Answer

I was using !X to represent false. SOLVED. Revised class code posted below, for a light switch that includes green, yellow, and red lights.

class LightAlg {
  int greenX, greenY = 50;   // Alphas of Green Lights (50 = OFF & 250 = ON)
  int redX, redY = 50;       // Alphas of Red Lights (50 = OFF & 250 = ON)
  int yellowX, yellowY = 50; // Alphas of Yellow Lights (50 = OFF & 250 = ON) 
  int time;                  // set to equal millis()
  int lastInterval;          // Used to update time since last light change then used to take away from "time" in order to get amount of time specified
  float interval = 5;        // Interval for Lights in Seconds
  float wait = 1000;         // turn interval from seconds to millis
  char lightX = 'G';         // Switch for Lights on X-Cords
  char lightY = 'R';         // Switch for Lights on Y-Cords
  boolean X, Y, Xy, Yy;      // Booleans for Lights to corespond to draw/make car (Xy & Yy = whether intersection should be crossed if already entered by cars)

  LightAlg (float interval_) { //Constructor and specify light change time wanted
    interval = interval_;
  }

  void run() {
    time = millis(); //Sets time to time since program was executed
    println("Last Interval Times is " + lastInterval/1000); // Prints time since last time lights where changed
    println("X-Light = " + X); //Prints Boolean of X-Light 
    println("Xy-Light = " + Xy); //Prints Boolean of X-Light when it supposed to be yellow in order to creat a flag for whether cars should continue throught intersection
    println("Y-Light = " + Y); //Prints Boolean of Y-Light
    println("Yy-Light = " + Yy);//Prints Boolean of Y-Light when it supposed to be yellow in order to creat a flag for whether cars should continue throught intersection
    if (time - lastInterval < wait * interval) { //  // If the Light has been green for less than wait time by taking millis() - time since last light change < time wanted for light interval
      if (X == true && Xy == true) { //If light is green and intersection is ok to enter
        my_array.draw_cars();
        my_array.make_car();
        my_array.check_collision();
      }
    } 

    if (time - lastInterval >= wait * interval) {  // If the Light hasn't changed for longer then specified interval time

      if ((X == false && Xy == false) && (Y == false && Yy == true)) { //If X-Light is Red and Intersection is not safe to cross && Y-Light is yellow and Intersections is safe to cross
        lightX = 'G'; //X-Light Goes Green
        lightY = 'R'; //Y-Light Goes Red
      } 
      else if ((X == true && Xy == true) && (Y == false && Yy == false)) { //If X-Light is Green and Intersection is safe to cross && Y-Light is Red and Intersections is  not safe to cross
        lightX = 'Y'; //X-Light Goes Yellow
        lightY = 'R'; //Y-Light Goes Red
      } 
      else if ((X == false && Xy == true) && (Y == false && Yy == false)) { // If X-Light is Yellow and Intersection is safe to cross && Y-Light is Red and intersection is not safe to cross
        lightX = 'R'; //X-Light Goes Red
        lightY = 'G'; //Y-Light Goes Green
      }
      else if ((X == false && Xy == false) && (Y == true && Yy == true)) { // If X-Light is Red and Intersection is not safe to cross && Y-Light is Green and intersection is safe to cross
        lightX = 'R'; //X-Light Goes Red
        lightY = 'Y'; //Y-Light Goes Green
      }
      else if ((X == false && Xy == false) && (Y == false && Yy == true)) { // If X-Light is Red and Intersection is not safe to cross && Y-Light is Yellow and Intersection is safe to cross
        lightX = 'G'; //X-Light Goes Green
        lightY = 'R'; //Y-Light Goes Red
      }
      lastInterval = time; // Update last Interval to time of light change so that you can Minus it from time in order to set time back to length of interval desired
    }


    switch(lightX) {        //Switch For X-Cord Lights
    case 'G':              // Green Light
      println("X-Axis = Green Light");
      time = millis(); //Update time when light changes
      greenX = 250; //Alphas for trafficlight Object
      yellowX = 50; //Alphas for trafficlight Object
      redX = 50; //Alphas for trafficlight Object
      X = true; //Light is Green so boolean for cars on Xpos BEFORE intersection is true
      Xy = true;
      break;

    case 'Y':              // Yellow Light
      println("X-Axis = Yellow Light");
      time = millis(); //Update time when light changes
      greenX = 50; //Alphas for trafficlight Object
      yellowX = 250; //Alphas for trafficlight Object
      redX = 50; //Alphas for trafficlight Object
      Xy = true; //Light is Yellow so boolean for cars on Xpos INSIDE/AFTER intersection is true
      X = false; //Light is Yellow so boolean for cars on Xpos BEFORE intersection is false
      break;


    case 'R':             //Red Light
      println("X-Axis = Red Light");
      time = millis(); //Update time when light changes
      greenX = 50; //Alphas for trafficlight Object
      yellowX = 50; //Alphas for trafficlight Object
      redX = 250; //Alphas for trafficlight Object
      Xy = false; //Light is RED so boolean for cars on Xpos INSIDE/AFTER intersection is false
      X = false; //Light is Red so boolean for cars on Xpos BEFORE intersection is false
      break;
    }

    switch(lightY) {       //Switch For Y-Cord Lights
    case 'G':              // Green Light
      println("Y-Axis = Green Light");
      println(time/1000);
      println();
      time = millis(); //Update time when light changes
      greenY = 250; //Alphas for trafficlight Object
      yellowY = 50; //Alphas for trafficlight Object
      redY = 50; //Alphas for trafficlight Object
      Y = true; //Light is GREEN so boolean for cars on Ypos BEFORE intersection is true
      Yy = true; //Light is GREEN so boolean for cars on Ypos INSIDE/AFTER intersection is true
      break;

    case 'Y':              // Yellow Light
      println("Y-Axis = Yellow Light");
      println(time/1000);
      println();
      time = millis(); //Update time when light changes
      greenY = 50; //Alphas for trafficlight Object
      yellowY = 250; //Alphas for trafficlight Object
      redY = 50; //Alphas for trafficlight Object
      Yy = true; //Light is YELLOW so boolean for cars on Ypos INSIDE/AFTER intersection is true
      Y = false; //Light is YELLOW so boolean for cars on Ypos BEFORE intersection is false
      break;


    case 'R':             //Red Light
      println("Y-Axis = Red Light");
      println(time/1000);
      println();
      time = millis(); //Update time when light changes
      greenY = 50; //Alphas for trafficlight Object
      yellowY = 50; //Alphas for trafficlight Object
      redY = 250; //Alphas for trafficlight Object
      Y = false;  //Light is RED so boolean for cars on Ypos BEFORE intersection is false
      Yy = false; //Light is RED so boolean for cars on Ypos INSIDE/AFTER intersection is false
      break;
    }
  }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement