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.
Rearranged code for better comprehension, sorry for the confusion caused
#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; //north light green sbit out2_ny=P0^1; //north light yellow sbit out3_nr=P0^2; //north light red sbit out1_wr=P0^3; //west light red sbit out2_wy=P0^4; //west light yellow sbit out3_wg=P0^5; //west light green sbit out1_eg=P1^0; //east light green sbit out2_ey=P1^1; //east light yellow sbit out3_er=P1^2; //east light red sbit out1_sr=P1^3; //south light red sbit out2_sy=P1^4; //south light red sbit out3_sg=P1^5; //south light green 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 on out2_wy=0; //led off out3_wg=0; //led off 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 on out2_ey=0; //led off out1_eg=0; //led off 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 on out2_sy=0; //led off 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 on out1_eg=0; //led off out1_sr=1; //led on out2_sy=0; //led off 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 off out2_ey=0; //led off out3_er=1; //led on out1_sr=0; //led off out2_sy=0; //led off out3_sg=1; //led on 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 off out2_ey=0; //led off out3_er=1; //led on out1_sr=1; //led on out2_sy=0; //led off out3_sg=0; //led off out1_wr=0; //led off out2_wy=0; //led off out3_wg=1; //led on 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 } } }
That shows the problem very clearly, even though you're still not pasting the code nicely here.
You're in the while loop continually checking your inputs, and never come out of that loop to process your outputs.
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.
That shows the problem very clearly Isn't that amazing
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.