This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

for loop not executing

Good Day, i am writing a code for a density based traffic light project using for loops for the density part of the system, but my for loops wont execute. the code is attached below.

Parents
  • Making progress. Well done.

    // I've cut/pasted/auto-formatted the code for you to avoid further spurious waffle
    
    void main()
    {
      while(1)
      {
        v1=0;   //clear v1
        w1=0;   //clear w1
        x1=0;   //clear xl
        y1=0;   //clear y1
    
        if(in1_na==1)     //if sensor1 is blocked
        {
          v1=1;
        }
        if((in1_na==1)&&(in2_nb==1))  //if sensorl & sensor2 are blocked
        {
          v1=2;
        }
    
        if(in1_wa==1)   //if sensor3 is blocked
        {
          w1=1;
        }
    
        if((in1_wa==1)&&(in2_wb==1))   //if sensor3 and sensor4 are blocked
        {
          w1=2;
        }
        if(in1_sa==1)  //if sensor5 is blocked
        {
          x1=1;
        }
        if((in1_sa==1)&&(in2_sb==1))  //if sensor5 & sensor6 are blocked
        {
          x1=2;
        }
    
        if(in1_wa==1)   //if sensor7 is blocked
        {
          y1=1;
        }
        if((in1_wa==1)&&(in2_wb==1))            //if sensor7 and sensor8 are blocked
        {
          y1=2;
        }
    
        // You're still within the while loop
    
        // LED control here ? - First glimpse suggests that there should be no problem with above
      }
    
      // Assuming it falls through to the remainder of your main function here
    

    So the obvious question is where are the loops to control the LEDs?

Reply
  • Making progress. Well done.

    // I've cut/pasted/auto-formatted the code for you to avoid further spurious waffle
    
    void main()
    {
      while(1)
      {
        v1=0;   //clear v1
        w1=0;   //clear w1
        x1=0;   //clear xl
        y1=0;   //clear y1
    
        if(in1_na==1)     //if sensor1 is blocked
        {
          v1=1;
        }
        if((in1_na==1)&&(in2_nb==1))  //if sensorl & sensor2 are blocked
        {
          v1=2;
        }
    
        if(in1_wa==1)   //if sensor3 is blocked
        {
          w1=1;
        }
    
        if((in1_wa==1)&&(in2_wb==1))   //if sensor3 and sensor4 are blocked
        {
          w1=2;
        }
        if(in1_sa==1)  //if sensor5 is blocked
        {
          x1=1;
        }
        if((in1_sa==1)&&(in2_sb==1))  //if sensor5 & sensor6 are blocked
        {
          x1=2;
        }
    
        if(in1_wa==1)   //if sensor7 is blocked
        {
          y1=1;
        }
        if((in1_wa==1)&&(in2_wb==1))            //if sensor7 and sensor8 are blocked
        {
          y1=2;
        }
    
        // You're still within the while loop
    
        // LED control here ? - First glimpse suggests that there should be no problem with above
      }
    
      // Assuming it falls through to the remainder of your main function here
    

    So the obvious question is where are the loops to control the LEDs?

Children
  • Let me explain the idea of the code:
    i am emulating a density based traffic system for a 4 way traffic junction. the code for the normal operation of a traffic junction works fine.

    where i am having difficulty is the part where i take inputs from one or 2 of the sensor ports and make them 'on' using a for loop for when this condition is meet signifying the lane where those sensor are has a higher density and should be given priority.

    so am i approaching it wrong, or is there a problem with a part of the code. as this is the only problematic part of the code.

  • We've got past your initial problem. Now it's time for you to put your thinking cap on and consider the most suitable sequence to achieve your final result.