Discussion in "Project Help" started by    CitronsG6    Jun 4, 2017.
Sun Jun 04 2017, 04:06 PM
Good Day,
Need some help with my code, i am working on a code for a density based traffic system and i am trying to understand how to write a loop for when my some of my inputs are on but i am having difficult in getting results. i am a beginner at c programming, so in case i am make very obvious errors please take it easy on me. below is my the code i am working on.
```#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 yellow
sbit out3_sg=P1^5; //south light green

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

void delay_2sec();
void delay_5sec();
void delay_12sec();

void main()
{

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

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

if((in1_na==1)&&(in2_nb==1))  //if sensorl & sensor2 are blocked
{
v1=2;
break;
}

if(in1_wa==1)                     //if sensor3 is blocked
{
w1=1;
break;
}

if((in1_wa==1)&&(in2_wb==1))		//if sensor3 and sensor4 are blocked
{
w1=2;
break;
}

if(in1_sa==1)                         //if sensor5 is blocked
{
x1=1;
break;
}
if((in1_sa==1)&&(in2_sb==1))  //if sensor5 & sensor6 are blocked
{
x1=2;
break;
}

if(in1_wa==1)                     //if sensor7 is blocked
{
y1=1;
break;
}

if((in1_wa==1)&&(in2_wb==1))		//if sensor7 and sensor8 are blocked
{
y1=2;
break;
}

for(k=0;k<=v1;k++)
{
delay_2sec();
}
out3_wg=1;

for(k=0;k<=w1;k++)
{
delay_5sec(); //delay for 5sec
}
out2_ny=1;         //led on
out3_nr=1;          //led off
out2_sy=1;           //led on
out3_sg=1;           //led off

delay_2sec();

out1_eg=1;          //led off
out2_ey=1;          //led off
out3_er=1;          //led on

out1_sr=1;         //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

}
}
}

```

Mon Jun 05 2017, 08:47 PM

i am a beginner at c programming, so in case i am make very obvious errors please take it easy on me. below is my the code i am working on.

No problem, we are here to help beginners.

I have attached a re-worked version of your code to help to see what it does.

The main problem is the break statements in your while(1) loop.
Every "break" statement causes your code to exit the while(1) loop.
```if(in1_na==1)                         //if sensor1 is blocked
{
v1=1;
break;  //"break" out of while loop
}
```

In this case there is nowhere to go outside the loop, so the program will crash.

Attachment

[ Edited Fri Jun 09 2017, 05:50 AM ]

### Information

Powered by e107 Forum System

#### Conversation

Thu Mar 29 2018, 10:47 AM
please help me for the configuration bits for dspic30f2010
Thu Mar 29 2018, 10:45 AM
can any one using dspic30f2010.
Thu Mar 29 2018, 06:03 AM
Neena
can anybody give solar tracking in 8051 using proteus and keil to mail [email protected]
Mon Mar 19 2018, 07:33 PM
sorry bedo.. maybe next time
Sun Mar 18 2018, 07:48 AM
Mourya Manojkumar
Please send me circuit diagram and PCB diagram
Thu Mar 01 2018, 05:49 AM
Felixc
May you please help me with a code for programmable load shedding controlling 4 loads
Sun Feb 25 2018, 08:29 PM
ezeoke.charles
i need of it
Sat Feb 24 2018, 03:35 AM
Aaquib
I want help for digital ic tester
Tue Feb 20 2018, 04:01 PM
Join up.. and post your problem
Mon Feb 19 2018, 11:17 AM
ashishkumar
i needs help for my project

### Recent

#### Comments

jamiel
Sat Apr 14 2018, 12:47 AM
akrepkral76
Wed Apr 04 2018, 07:51 PM
samanthaeaves
Mon Apr 02 2018, 06:27 AM
rana2056
Sat Mar 31 2018, 02:11 PM
Darshanparabbnb
Mon Mar 26 2018, 07:17 AM
marttinsmarcio
Sat Mar 24 2018, 12:31 PM
LAMBO
Tue Mar 20 2018, 09:51 AM
bedo123
Mon Mar 19 2018, 07:48 AM

#### Online

Guests: 43, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37993
Newest member: jamiel
Forum Activity
##### Posted By ExperimenterUK
Mon Apr 16 2018, 08:49 PM
On the diagram, a diode feeds a rectified sine wav...
##### Posted By LAMBO
Mon Apr 16 2018, 06:39 AM
Hi sir i haven't connected any serial connec...
##### Posted By ExperimenterUK
Fri Apr 13 2018, 02:09 AM
I assume you are doing this project.http://www.805...
##### Posted By LAMBO
Thu Apr 12 2018, 06:51 PM
hii sir,I am working on ac motor controller .I hav...
##### Posted By ExperimenterUK
Tue Apr 10 2018, 06:12 PM
The lpc214x has a 32-bit prescaler , so I would ...
##### Posted By Praneeth1299
Tue Apr 10 2018, 09:16 AM
Hi,I'm generating constant frequency of 1hz and va...
##### Posted By akrepkral76
Sat Apr 07 2018, 01:57 PM
I have used diptrace.I think ı have recevied from ...
##### Posted By ExperimenterUK
Sat Apr 07 2018, 01:45 AM
You will need to say which software you are using,...
##### Posted By akrepkral76
Fri Apr 06 2018, 12:22 PM
I have drawed many PCB board. But it is still not ...
##### Posted By ExperimenterUK
Wed Apr 04 2018, 10:49 PM
Do you have a working HM2007 board as shown in the...