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

#### Conversation

Wed Nov 08 2017, 01:35 PM
Anirudh jain
I want to burn code in atmel 89s51 24pc 0329 but cannot get the connection with the burner. Can somebody provide me with the circuit diagram for connecting controller to burner. I'm using usb burner
Thu Nov 02 2017, 11:44 AM
karpal
write a program to exchange the content of b register with external RAM address 02CFH help me
Wed Nov 01 2017, 05:47 PM
sanae
i am using the DHT11 but i'm getting some errors in the simulate the programe in proteus 8 professional,: 1)cannot find model file 'DHTXX.MDF'.2)simulation failed due to netlist linker error(s).
Fri Sep 29 2017, 02:45 AM
Maaz
Wed Sep 20 2017, 12:36 AM
Ted
In a missing pulse detector using a 555 timer connected as a monostable vibrator with a PNP transistor,how does the positve input pulse turn on the PNP transistor?
Sun Sep 03 2017, 02:29 PM
martymarty
How do I connect an <NOKIA 1100 LCD > to an atmega328P ?
Sun Aug 20 2017, 10:23 AM
i am intending to interface sd card with STM32F4 and want to write a file in sd card using SPI protocol but using HAL only. Can anyone help me PLEASE!!!!
Sun Aug 20 2017, 10:22 AM
hello
Fri Aug 18 2017, 11:37 AM
hi @ angel patel i will attach a file i hope it will be helpfull for you
Mon Aug 14 2017, 09:57 PM

### Recent

PANDIKOTESWARARAO
Mon Nov 20 2017, 07:40 PM
Hemantika
Sun Nov 19 2017, 07:34 PM
Wed Nov 15 2017, 10:46 AM
Ansh12
Sat Nov 11 2017, 03:43 AM
AngelGT93
Fri Nov 10 2017, 06:53 AM
JardCrocker
Wed Nov 08 2017, 05:27 PM
vivitern
Tue Nov 07 2017, 09:08 AM
catrons
Sat Nov 04 2017, 10:46 PM

#### Online

Guests: 41, Members: 0 ...

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

Members: 37918
Forum Activity
##### Posted By ExperimenterUK
Mon Nov 20 2017, 10:18 PM
Here is a version of the tutorial that reads a key...
##### Posted By Ansh12
Mon Nov 20 2017, 02:15 AM
As a starting point, can you tell us how much of t...
##### Posted By ExperimenterUK
Sun Nov 19 2017, 08:35 PM
As a starting point, can you tell us how much of t...
##### Posted By Ansh12
Fri Nov 17 2017, 04:07 AM
I am looking keypad interfacing program. I was rea...
##### Posted By JardCrocker
Wed Nov 08 2017, 05:29 PM
Hi...i am a new user here. I was also facing the s...
##### Posted By ExperimenterUK
Tue Nov 07 2017, 07:13 PM
> It employs a fairly advanced technique to detect...
##### Posted By vivitern
Tue Nov 07 2017, 09:33 AM
I own a used Clavia ddrum4 SE drum module. It empl...
##### Posted By bvdb
Wed Oct 18 2017, 01:50 AM
I purchased the RKitL51 8051 C compiler from Raiso...
##### Posted By ExperimenterUK
Mon Oct 16 2017, 11:44 PM
That looks complicated.Is 8051_BLK a standard 80...
##### Posted By ExperimenterUK
Sun Oct 15 2017, 07:29 PM
According to this, gmail should support POP3.https...