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.

  •  #include<reg52.h>
    
    sbit in1_na=P2^0;               //sensor1
    sbit in2_nb=P2^1;       //sensor2
    sbit in1_wa=P2^2;       //sensor3
    sbit in2_wb=P2^3;    //sensor4
    sbit in1_sa=P2^4;    //sensor5
    sbit in2_sb=P2^5;               //sensor6
    sbit in1_ea=P2^6;               //sensor7
    sbit in2_eb=P2^7;               //sensor8
    
    sbit out1_ng=P0^0;
    sbit out2_ny=P0^1;
    sbit out3_nr=P0^2;
    
    sbit out1_wr=P0^3;
    sbit out2_wy=P0^4;
    sbit out3_wg=P0^5;
    
    sbit out1_eg=P1^0;
    sbit out2_ey=P1^1;
    sbit out3_er=P1^2;
    
    sbit out1_sr=P1^3;
    sbit out2_sy=P1^4;
    sbit out3_sg=P1^5;
    
    unsigned int i,j,k=0,v1=0,w1=0,x1=0,y1=0,z1=0,t1=0;
    
    void delay_2sec();
    void delay_5sec();
    void delay_12sec();
    
    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;
                                            }
    
    
    
           out1_wr=1; //led off
           out2_wy=0; //led off
           out3_wg=0; //led on
    
           out1_sr=1; //led on
           out2_sy=0; //led off
           out3_sg=0; //led off
    
           out1_eg=0;  //led off
           out2_ey=0;  //led off
           out3_er=1;  //led on
    
           out1_ng=1; //led on
           out2_ny=0; //led off
                             out3_nr=0; //led off
    
    
                            delay_5sec();  //delay for 5sec
    
                            out3_er=1;                                      //led off
                            out2_ey=0;          //led off
                            out1_eg=0;          //led on
                            out1_ng=0;                                      //led off
                            out2_ny=1;           //led on
                            out3_nr=0;           //led off
    
                            delay_2sec();   //delay for 2sec
    
                            out1_ng=0;          //led off
                            out2_ny=0;          //led off
                            out3_nr=1;          //led on
    
                            out1_eg=1;           //led on
                            out2_ey=0;           //led off
                            out3_er=0;           //led off
    
                            out1_sr=1;          //led off
                            out2_sy=0;          //led on
                            out3_sg=0;          //led off
    
                      out1_wr=1;         //led on
                            out2_wy=0;         //led off
                            out3_wg=0;         //led off
    
    
        delay_5sec();  //delay for 5sec
    
    
                            out3_er=0;                                      //led off
                            out2_ey=1;          //led off
                            out1_eg=0;          //led on
                            out1_sr=1;                                      //led off
                            out2_sy=0;           //led on
                            out3_sg=0;           //led off
    
        delay_2sec();       //delay for 2sec
    
                            out1_ng=0;          //led off
                            out2_ny=0;          //led off
                            out3_nr=1;          //led on
    
                            out1_eg=0;           //led on
                            out2_ey=0;           //led off
                            out3_er=1;           //led off
    
                            out1_sr=0;          //led off
                            out2_sy=0;          //led on
                            out3_sg=1;          //led off
    
                      out1_wr=1;         //led on
                            out2_wy=0;         //led off
                            out3_wg=0;         //led off
    
    
        delay_5sec();  //delay for 5sec
    
                            out1_sr=0;                                      //led off
                            out2_sy=1;           //led on
                            out3_sg=0;           //led off
                            out1_wr=1;         //led on
                            out2_wy=0;         //led off
                            out3_wg=0;         //led off
    
        delay_2sec();       //delay for 2sec
    
                            out1_ng=0;          //led off
                            out2_ny=0;          //led off
                            out3_nr=1;          //led on
    
                            out1_eg=0;           //led on
                            out2_ey=0;           //led off
                            out3_er=1;           //led off
    
                            out1_sr=1;          //led off
                            out2_sy=0;          //led on
                            out3_sg=0;          //led off
    
                      out1_wr=0;         //led on
                            out2_wy=0;         //led off
                            out3_wg=1;         //led off
    
    

  • for(k=0;k<=v1;k++)
                    {
                    delay_2sec();
                    }
                    out3_wg=1;
    
                    for(k=0;k<=w1;k++)
                    {
        delay_5sec(); //delay for 5sec
                    }
                            out2_ny=0;         //led on
                            out3_nr=1;          //led off
                            out2_sy=0;           //led on
                            out3_sg=1;           //led off
    
        delay_2sec();
    
                            out1_eg=1;          //led off
                            out2_ey=1;          //led off
                            out3_er=0;          //led on
    
                            out1_sr=0;         //led on
                            out2_sy=1;           //led off
                            out3_sg=1;           //led off
    
                            out1_ng=0;         //led on
                            out2_ny=1;         //led off
          out3_nr=1;        //led off
    
          out1_wr=1;           //led off
          out2_wy=1;           //led off
          out3_wg=0;           //led on
    
                            delay_2sec();
                            out1_ng=1;
                    }
            }
    
            /*******************12s delay*****************/
    
            void delay_12sec()
            {
            for(j=0;j<12;j++)
            {
            for(i=0;i<1000;i++)
            {
    
             TMOD=0X01;          // SET THE TIMER IN MODE 1 MODE
             TL0=0XFE;           // LOAD THE THO VALUE
             TH0=0XFB;           // LOAD THE TL0 VALUE
             TR0=1;              // START THE TIMER0
             TF0=0;
             while(TF0==0); // WAIT FOR TIMER OVERFLOWS
             TF0=0;              // CLAEAR THE TIMER0 OVER FLOW FLAG
                                     TR0=0;      //STOP THE TIMER
            }
            }
    }
    
            /*******************5s delay*****************/
    
            void delay_5sec()
            {
            for(j=0;j<5;j++)
            {
            for(i=0;i<1000;i++)
            {
    
             TMOD=0X01;          // SET THE TIMER IN MODE 1 MODE
             TL0=0XFE;           // LOAD THE THO VALUE
             TH0=0XFC;           // LOAD THE TL0 VALUE
             TR0=1;              // START THE TIMER0
             TF0=0;
             while(TF0==0); // WAIT FOR TIMER OVERFLOWS
             TF0=0;              // CLAEAR THE TIMER0 OVER FLOW FLAG
                                     TR0=0;      //STOP THE TIMER
            }
            }
    }
    
    /****************** 2s delay ********************/
    
            void delay_2sec()
            {
            for(j=0;j<2;j++)
            {
            for( i=0;i<1000;i++)
            {
           TMOD=0X01;  //SET THE TIMER0 IN MODE 1 MODE
           TL0=0XFE;  //LOAD THE TH0 VALUE
           TH0=0XFD;  //LOAD THE TL0 VALUE
           TR0=1;     //START THE TIMER0
           TF0=0;
           while(TF0==0); //WAIT FOR TIMER0 OVERFLOWS
           TF0=0;      //CLEAR THE TIMER0 OVER FLOW FLAG
           TR0=0;      // STOP THE TIMER
    
            }
            }
    }
    
    

  • Hi badelmilk.

    Can you say what part of the code is working.

    Did you try putting in printfs to your code to see where it is going.

  • I'm afraid that the layout & spacing of your code has gone haywire - rendering it extremely hard to read.

    This is probably due to you using TAB characters - these are notoriously unreliable and will lead to exactly this kind of problem.

    The answer is to use only space characters.

    Any decent programmer's editor - including uVision - can be configured to insert spaces when you press the TAB button on your keyboard.

    Any decent programmer's editor - including uVision - can also replace TABs in existing code with spaces.

    So, please reformat you code - with spaces only - into a legible form & re-post.

  • Admittedly, the code layout isn't super great, but I really can't see the difficulty in determining the basic program flow.

    There are two nested delay loops within one loop in main and there are three delay subroutines each containing nested loops using an 8051 timer to create a small delay.

    H H Habdir's response is sensible. You need to say what you mean by "but my for loops wont execute".

    So, either there is something fundamentally wrong with your basic 8051 timer delay or your action sequence isn't as you expect. Maybe put some debug code in to determine

    Happy debugging.

  • Good Day, the problem i am facing is after compiling and creating the hex file in keil, i simulate the circuit with Proteus but it seems like the micro controller doesnt take the input from the sensors that activates the for loop part.

  • So you must break down the functionality and determine the problem(s).

    Why not try something even simpler first. Like just flashing a single LED. When that is working, build up your code. Many projects are built this way.

  • and does that not allow you to single-step through the code and see exactly what is happening?

    That, surely, is one of the key reasons to use a simulator?!

  • Admittedly, the code layout isn't super great, but I really can't see the difficulty in determining the basic program flow.

    those of us that try to help do that in small breaks from our paid work.
    thus any difficulty reading is likely to reduce the answers

  • thus any difficulty reading is likely to reduce the answers

    Agreed, but I have to admit to finding it very odd that someone could find the time to write so much about formatting and tabs and editors while not spending very little time looking at the code.

    It's so blatantly obvious that the OP is new to this stuff. IMO they deserve a little break; which is why I tried to give a sensible response instead of going on about editors [face-palm]

  • Agreed, but I have to admit to finding it very odd that someone could find the time to write so much about formatting and tabs and editors while not spending very little time looking at the code.

    the issue is not what takes the most time (BTW you are reversed in that) but the fact that readable code is even a benefit to the OP.

    there has been cases where the result of an OP being advised to "line up" the code has resulted in the OP posting "OH, now I see"

    anyhow, I have a suspicion

    WHICH CHIP?

  • Indeed - hence taking the time to explain to them why the layout had gone haywire, how to fix it, and how to avoid it happening again in the future.

  • BTW you are reversed in that

    Not sure what you're meaning by that. But hey-ho.

    there has been cases where the result of an OP being advised to "line up" the code has resulted in the OP posting "OH, now I see"

    Of course, a very valid point. But we don't know what the code looks like on the OP's computer (though I'd hazard a guess that it's not as bad as this Keil forum likes to make things). Again, in this instance, there's so little there. The basic code structure is simple. Really, really, really, really simple. Way simpler than much of the really badly formatted tosh that sometimes appears here. Way simpler than much of the well formatted tosh that sometimes appears here.

    Yes, I'll say it. I guess I just have an aversion to OTT (aka anal) responses.

    WHICH CHIP?

    Yes, a very appropriate question.

  • i have already identified the problem with the code, all the outputs (LEDs) are working fine. Now where i am having problems is the input let me breakdown the code for the input:

    sbit in1_na=P2^0;  //sensor1
    sbit in2_nb=P2^1;  //sensor2
    sbit in1_wa=P2^2;  //sensor3
    sbit in2_wb=P2^3;  //sensor4
    sbit in1_sa=P2^4;  //sensor5
    sbit in2_sb=P2^5;  //sensor6
    sbit in1_ea=P2^6;  //sensor7
    sbit in2_eb=P2^7;  //sensor8
    

    sensor declaration at P2.0-7 above

    unsigned int i,j,k=0,v1=0,w1=0,x1=0,y1=0,z1=0,t1=0;
    

    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;
                    }
    }
    


    sensor input conditions for "for loops", i just want to know if this is correct. Because when i simulate the code in proteus, and set all inputs to ON, the for loops never initiates. Only the output part of the code and the delay timers work.

  • 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?