Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help needed with code to control a train

Status
Not open for further replies.

lynford

New Member
So i have to program a pic16f88 using mikroc to control a train to travel between 3 stations. Each station has a different sensor and the train should stop at the 1st station for 5 secs ,the 2nd station it stops for 3 secs and the 3rd station it stops for 5 secs and returns to the 2nd station and stops for 3 secs and then the 1st and stops for 5 secs..and it takes a max time of 10 secs to travel between stations.

Now iv'e got the code to move the train from station to station and the lights working but i need to get a timing system in that counts 10 SECONDS and if the train does not arrive at the sensor at a station the l.e.d's should start blinking on the part of the track the train is on........

and another timer a transit timer to maek sure that the train is on time and if the train is delayed at one station it would make up for it by either going faster or taking shorter breaks at the other stations .

and i have to implement a master reset button, a halt button and a resume button.Thanks..

This is the code to make the train to go from station to station and control the lights.
By the way case 0 is when the train is placed anywhere on the track it brings the train back to station 1 and then starts the process.

// Train Code V2 travels to stations sensors with lights.

#define A0 PORTA.F0
#define A1 PORTA.F1
#define A2 PORTA.F2

#define B0 PORTB.F0
#define B2 PORTB.F2
#define B3 PORTB.F3
#define B4 PORTB.F4
#define B5 PORTB.F5


char state = 0; //start from state 0
void main()
{
ANSEL = 0 ;
CMCON = 0X07; //PORT A SET TO DIGITAL MODE
TRISA = 0b00011111; // RA.0-RA.4 set to input, rest set to output
TRISB = 0b00000000; // All ports on port b set to output

switch(state)
{

case 0:
B3 = 0; //all lights green
B4 = 0;
B5 = 0;
while (A0 = 0) //until station 1 sensor picks up something
{ //train will continue to travel towards station 1
B0 = 1;
B2 = 0;
}
if (A0 = 1) //when station1 sensor picks up something train will stop
{ //for 5 seconds and go on to case1 commands
B0 = 0;
B2 = 0;
delay_ms(500);
state = 1;
}
else //if nothing gets picked up on sensor0 then case 0 will repeat
{
state = 0;
}
break;



case 1:
B3 = 1; //all lights red
B4 = 1;
B5 = 1;
while (A1 = 0) //train will travel towards station2 until sensor1 picks up
{ //something
B3 = 0; //Station1 light green
B0 = 0;
B2 = 1;
}
if (A1 = 1) //if sensor1 picks something up it will stop for 3 seconds
{ //and go on to case2 commands
B3 = 1; //station1 light red
B0 = 0;
B2 = 0;
delay_ms(300);
state = 2;
}
else //if sensor1 doesnt pick something up case 1 will repeat
{
state = 1;
}
break;



case 2:
while (A2 = 0) //train will travel towards station3 until seonsor 2 picks
{ //something up
B4 = 0; //station2 light green
B0 = 0;
B2 = 1;
}
if (A2 = 1) //if sensor 2 picks something up train will stop for 5 seconds
{ //and go on to case3 commands
B4 = 1; //station2 light red
B0 = 0;
B2 = 0;
delay_ms(500);
state = 3;
}
else //else case 2 will repeat
{
state = 2 ;
}



case 3:
while (A1 = 0) //train will travel towards station 2 until sensor 1 picks
{ //something up
B5 = 0; //station 3 light green
B0=1;
B2=0;
}
if (A1 = 1) //if sensor 1 picks up something train will stop for 3 seconds
{ //and go on to case4 commands
B5 = 1; //station3 light red
B0 = 0;
B2 = 0;
delay_ms(300);
state = 4;
}
else //else it will repeat case 3 commands
{
state = 3;
}
break;


case 4:
while (A0 = 0) //train will travel towards station 1 until sensor0 picks up
{ //something
B4 = 0; //station 2 light green
B0=1;
B2=0;
}
if (A0 = 1) //if sensor0 picks soemthing up then train will stop for 5
{ //seconds and go on to case1 commands
B4 = 1; //station2 light red
B0 = 0;
B2 = 0;
delay_ms(500);
state = 1;
}
else //else it will repeat case 4 commands
{
state = 4;
}
break;
}
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top