HELPICkit2... traffic pedestrian crossing program

Thread Starter

retromotoracer

Joined Jul 25, 2013
4
hello, trying to get help with my school project to program a simple traffic lights with pedestrian crossing. I'm using PICkit2 with MPLAB IDE. Set-up PORTB as input (buttons) & PORTC as output (LEDs)

Rich (BB code):
void main (void)
{
      initPORT();     // PIC IO Port Configuration and Initialisation
 
while(1) 
{ 
  RC0 = 0; //red light off
  RC1 = 0; //amber light off
  RC2 = 1; //green light on

  RC4 = 1; //red-man on
  RC6 = 0; //green-man off
  
          if (RB0 ==1 || RB2==1) //button 0 or button 2 pressed
          {
      __delay_ms(10000);
    
      RC0 = 0; //red light off
      RC1 = 1; //amber light on
      RC2 = 0; // green light off

      RC4 = 1; //red-man on
      RC6 = 0; //green-man off
  
     __delay_ms(3000);
    
       RC0 = 1; //red light on
       RC1 = 0; //amber light off
       RC2 = 0; //green light off

       RC4 = 0; //red-man off
       RC6 = 1; //green-man on
  
     __delay_ms(10000);
    
        RC0 = 1; //red light on
        RC1 = 0; //amber light off
        RC2 = 0; //green light off

        RC4 = 0; //red-man off
        RC6 = 1; //I need the green-man to blink for 10 seconds (on for 1sec & off for 1sec... 5times) but not sure what to input here!!!
 
Followed by...

         RC0 = 1; //red light on
         RC1 = 0; //amber light off
         RC2 = 0; //green light off
 
         RC4 = 1; //red-man on
         RC6 = 0; //green-man off
 
     __delay_ms(2000);
}
}
}
I believe after this it will loop back again to the top...

Thank you for the help in advance.
 
Last edited by a moderator:

WBahn

Joined Mar 31, 2012
33,109
Depending on the quality of the compiler and how small you need the code, this might produce smaller code because it only has to do the setup for the delay_ms() call once. Notice that I do the first setting of rc6 in the setup so that if there is a hiccup somewhere that toggles rc6 before this loop is reached, it will always recover at this point to the proper starting state. This means that you need one few passes through the loop.

Rich (BB code):
for(i = 0, rc6=1; i < 9; i++) {
  delay_ms(1000);
  rc6=~rc6;
}
 

WBahn

Joined Mar 31, 2012
33,109
Use this:

Rich (BB code):
for(i = 0; i < 5; i++) {
  delay_ms(1000);
  rc6=1;
  delay_ms(1000);
  rc6=0;
}
This will probably work fine for this application, but notice that there is a 1s delay between the red man going off and the green man going on. This might be okay (might even be better), but it is not the same thing that the present code does.
 

Brownout

Joined Jan 10, 2012
2,390
Of course it doesn't. The present code leaves the light on. The OP just asked to make the light blink. If he wants the light immediately, he can swap the delay and assignment statements.
 
Top