Sample Traffic Light Code - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Sample Traffic Light Code

EST394EST394 Posts: 1Member
We have to build a traffic light scenario with a 4 way intersection using c and we have to incorporate a push button representing the car sensor and a push button representing pedestrian button. We have the following code and its not working that well. Any help would be appreciated. We are using MPLAB and a protoboard with a PIC16F917

#include //specify PIC commands//
//_CONFIG(WDTDIS & INTIO & MCLREN & UNPROTECT & BORDIS & IESODIS & FCMDIS & DEBUGDIS);//
#define _XTAL_FREQ 4000000
void init(void) //Prep for program//
{
TRISA = 0b00000100; //defines what are the pins PORT B:
TRISB = 0b00000100; // =1 > inputs
TRISC = 0b00000100; //defines what are the pins PORT B:
TRISD = 0b00000100; // =1 > inputs // =0 > outputs
TRISE = 0b00000000; //defines what are the pins PORT B:

}
void wait (void)

{
SLEEP (1000);

}

void waiti (void)

{
SLEEP (5000);

}

void waitiii (void)

{
SLEEP (5000);
SLEEP (5000);
SLEEP (5000);
SLEEP (5000);
}

void nsgreen (void)

{

RB0=1; // N/S red
RB1=1;// E/W Red
RA0=0;
RC6=0;
RD1=0;
RE2=0;
wait();

RB0=1; // N/S red
RB1=0;
RA0=0;
RC6=0;
RD0=1; // E/W Green
RE0=0;
waitiii();

RB0=1; // N/S red
RB1=0;
RA0=0;
RC6=1; // E/W Yellow
RD0=0;
RE0=0;
waiti();


}

void ewgreen (void)

{
RB0=1; // N/S red
RB1=1; // E/W Red
RA0=0;
RC6=0;
RD0=0;
RE0=0;
wait();

RB0=0;
RB1=1; // E/W Red
RA0=0;
RC6=0;
RD0=0;
RE0=1; // N/S green
waitiii();

RB0=0;
RB1=1;// E/W Red
RA0=1;// N/S yellow
RC6=0;
RD0=0;
RE0=0;
waiti();



}
void main (void)

{


while(1)
{
init(); // initialize
RB0=1; // N/S red
RB1=1;// E/W Red
RA0=0;
RC6=0;
RD1=0;
RE2=0;
wait();

RB0=1; // N/S red
RB1=0;
RA0=0;
RC6=0;
RD0=1; // E/W Green
RE0=0;
waitiii();
if(RA2==1)
if(RB2==1)
{
waiti();
nsgreen();
}
else


RB0=1; // N/S red
RB1=0;
RA0=0;
RC6=1; // E/W Yellow
RD0=0;
RE0=0;
waiti();

RB0=1; // N/S red
RB1=1; // E/W Red
RA0=0;
RC6=0;
RD0=0;
RE0=0;
wait();

RB0=0;
RB1=1; // E/W Red
RA0=0;
RC6=0;
RD0=0;
RE0=1; // N/S green
waitiii();
if(RA2==1)
if(RB2==1)
{
waiti();
nsgreen();
}
else


RB0=0;
RB1=1;// E/W Red
RA0=1;// N/S yellow
RC6=0;
RD0=0;
RE0=0;
waiti();

}

}
Sign In or Register to comment.