HELPICkit2... traffic pedestrian crossing program

Discussion in 'Programmer's Corner' started by retromotoracer, Jul 25, 2013.

  1. retromotoracer

    Thread Starter New Member

    Jul 25, 2013
    4
    0
    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)

    Code ( (Unknown Language)):
    1.  
    2. void main (void)
    3. {
    4.       initPORT();     // PIC IO Port Configuration and Initialisation
    5.  
    6. while(1)
    7. {
    8.   RC0 = 0; //red light off
    9.   RC1 = 0; //amber light off
    10.   RC2 = 1; //green light on
    11.  
    12.   RC4 = 1; //red-man on
    13.   RC6 = 0; //green-man off
    14.  
    15.           if (RB0 ==1 || RB2==1) //button 0 or button 2 pressed
    16.           {
    17.       __delay_ms(10000);
    18.    
    19.       RC0 = 0; //red light off
    20.       RC1 = 1; //amber light on
    21.       RC2 = 0; // green light off
    22.  
    23.       RC4 = 1; //red-man on
    24.       RC6 = 0; //green-man off
    25.  
    26.      __delay_ms(3000);
    27.    
    28.        RC0 = 1; //red light on
    29.        RC1 = 0; //amber light off
    30.        RC2 = 0; //green light off
    31.  
    32.        RC4 = 0; //red-man off
    33.        RC6 = 1; //green-man on
    34.  
    35.      __delay_ms(10000);
    36.    
    37.         RC0 = 1; //red light on
    38.         RC1 = 0; //amber light off
    39.         RC2 = 0; //green light off
    40.  
    41.         RC4 = 0; //red-man off
    42.         [COLOR=red]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!!![/COLOR]
    43. [COLOR=red][/COLOR]
    44. [COLOR=red]Followed by...[/COLOR]
    45. [COLOR=#ff0000][/COLOR]
    46.          RC0 = 1; //red light on
    47.          RC1 = 0; //amber light off
    48.          RC2 = 0; //green light off
    49.  
    50.          RC4 = 1; //red-man on
    51.          RC6 = 0; //green-man off
    52.  
    53.      __delay_ms(2000);
    54. }
    55. }
    56. }
    57.  
    I believe after this it will loop back again to the top...

    Thank you for the help in advance.
     
    Last edited by a moderator: Jul 26, 2013
  2. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Instead of a 10 second delay how about ten 1 second delays.
     
  3. retromotoracer

    Thread Starter New Member

    Jul 25, 2013
    4
    0
    that will do... as long as the sequence work
     
  4. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    What language are you using?
     
  5. retromotoracer

    Thread Starter New Member

    Jul 25, 2013
    4
    0
    sld be C language, thank u.
     
  6. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Another hint would be toggling a bit 10 times with a for loop.
     
  7. retromotoracer

    Thread Starter New Member

    Jul 25, 2013
    4
    0
    ok... well, i just have to do that i guess.
     
  8. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Use this:

    Code ( (Unknown Language)):
    1.  
    2. for(i = 0; i < 5; i++) {
    3.   delay_ms(1000);
    4.   rc6=1;
    5.   delay_ms(1000);
    6.   rc6=0;
    7. }
    8.  
     
  9. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.

    Code ( (Unknown Language)):
    1.  
    2. for(i = 0, rc6=1; i < 9; i++) {
    3.   delay_ms(1000);
    4.   rc6=~rc6;
    5. }
    6.  
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.
     
  11. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.
     
Loading...