/****************************************************************************************
* File Name : Triffic Light *
* Version : 0.0 *
* Description : Archery Traffic Light sytem with both manual and auto modes. *
* Author : Cameron Ross *
* Target : Archery Traffic Lights *
* Compiler : Microchip C18 V3.40 C Compiler *
* IDE : Microchip MPLAB IDE v8.87 *
* Programmer : PICKit2 or PICKit3 *
* Last Updated : 26 Mar 2013 *
* *
**********************************************************************************************/
#pragma config PLLDIV = 5
#pragma config USBDIV = 2
#pragma config FOSC = HSPLL_HS
#pragma config PBADEN = OFF
#pragma config CPUDIV = OSC1_PLL2
#pragma config WDT = OFF, LVP = OFF
#include "p18f4550.h"
#include "delays.h"
unsigned char LightState[3] = {0x01, 0x04, 0x02};
unsigned char LightStatePointer = 0;
unsigned char Pswitch;
unsigned long Timer1 = 0x240; //2 min
unsigned long Timer2 = 0x480; //4 min
unsigned long Timer3 = 0x030; //10 sec
unsigned long Timer4 = 0x060; //20 sec
unsigned long Timer5 = 0xF50; //30 sec
unsigned long TimeRemaining; //Sequence length
unsigned long TimeRemaining2; //start delay length
unsigned char test;
void main()
{
TRISD = 0x00;
TRISB = 0xFF;
INTCON2bits.RBPU = 0;
while(1)
{
PORTD = LightState[LightStatePointer];
while((PORTB == 0x38) || (PORTB == 0x3A) || (PORTB == 0x34) || (PORTB == 0x36) || (PORTB == 0x34) || (PORTB == 0x2E) || (PORTB == 0x3F) || (PORTB == 0x3C) ) //automatick sequence set
{
LightStatePointer = 0;
PORTD = LightState[LightStatePointer];
if((PORTB == 0x2E) || (PORTB == 0x2C) || (PORTB == 0x36) || (PORTB == 0x34))
{
if((PORTB == 0x34) || (PORTB == 0x2C))
{
TimeRemaining2 = Timer4; //set 20sec start delay
}
if((PORTB == 0x36) || (PORTB == 0x2E))
{
TimeRemaining2 = Timer3; //set 10sec start delay
}
if((PORTB == 0x2E) || (PORTB == 0x2C))
{
TimeRemaining = Timer1; //2 min sequence length set
}
if((PORTB == 0x36) || (PORTB == 0x2C))
{
TimeRemaining = Timer2; //4 min sequence length set
}
while(LightStatePointer < 2)
{
while(TimeRemaining2 > 0)
{
Delay10KTCYx(250);
TimeRemaining2 --; //start delay while loop
}
LightStatePointer = 1; //sets light to green at start of sequence
PORTD = LightState[LightStatePointer]; //set Port d to the value at lightStatePointer
while(TimeRemaining > 0)
{
Delay10KTCYx(250);
TimeRemaining--; //Sequence length while loop
if((PORTB == 0x3A) || (PORTB == 0x38))
{
TimeRemaining = 0; //force stop
}
if(TimeRemaining == Timer5)
{
PORTD = 0X02; //turns on amber at 30s to go
}
}
LightStatePointer++;
}
}
}
if(PORTB == 0x3B)
{
LightStatePointer = 0; //manualy set light to Red
}
if(PORTB == 0x2F)
{
LightStatePointer = 1; //manualy set light to Green
}
if(PORTB == 0x37)
{
LightStatePointer = 2; //manualy set light to Amber
}
}
}