SHARE
TWEET

Police Car Pinewood Derby Car Source Code

a guest Feb 15th, 2014 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Police Car
  3.  
  4.    B.J. H.
  5.    Bryan H.
  6.    
  7.    01-19-2014
  8.    
  9.    Code for B.J.s Pinewood Derby
  10.  */
  11.  
  12. const int SPEAKER = P1_2;
  13. /*                   Frnt,Frnt,Rear,Rear,TopR,TopR,TopB,TopB    */
  14. const int LEDS[8] = {P2_0,P2_1,P2_2,P2_3,P2_4,P2_5,P1_4,P1_5};
  15. const int SWITCHES[2] = {P1_0,P1_1};
  16.  
  17. const int Light_Pattern[16][8] = {
  18.   { 1, 0, 1, 0, 1, 0, 0, 1},
  19.   { 0, 0, 1, 0, 1, 0, 0, 1},
  20.   { 1, 0, 1, 0, 1, 0, 0, 1},
  21.   { 0, 0, 1, 0, 1, 0, 0, 1},
  22.   { 0, 1, 1, 0, 0, 1, 1, 0},
  23.   { 0, 0, 1, 0, 0, 1, 1, 0},
  24.   { 0, 1, 1, 0, 0, 1, 1, 0},
  25.   { 0, 0, 1, 0, 0, 1, 1, 0},
  26.   { 1, 0, 0, 1, 1, 0, 0, 1},
  27.   { 0, 0, 0, 1, 1, 0, 0, 1},
  28.   { 1, 0, 0, 1, 1, 0, 0, 1},
  29.   { 0, 0, 0, 1, 1, 0, 0, 1},
  30.   { 0, 1, 0, 1, 0, 1, 1, 0},
  31.   { 0, 0, 0, 1, 0, 1, 1, 0},
  32.   { 0, 1, 0, 1, 0, 1, 1, 0},
  33.   { 0, 0, 0, 1, 0, 1, 1, 0}
  34. };
  35.  
  36. const int Normal[8] = {1,1,1,1,0,0,0,0};
  37.  
  38. /* flags */
  39. int button1State;
  40. int button2State;
  41. int loaded = 0;
  42. int racing = 0;
  43.  
  44.  
  45. void setup() {
  46.   int i;
  47.   for (i=0;i<8;i++){
  48.      pinMode(LEDS[i],OUTPUT);
  49.      digitalWrite(LEDS[i],LOW);
  50.   }
  51.   for (i=0;i<2;i++){
  52.     pinMode(SWITCHES[i], INPUT_PULLUP);
  53.   }
  54. }
  55.  
  56. void loop() {
  57.   int i;
  58.   if ((digitalRead(SWITCHES[1]) == LOW) && (loaded == 0))
  59.   {
  60.     ready_state();
  61.   }
  62.   if (digitalRead(SWITCHES[1]) ==  HIGH)
  63.   {
  64.    loaded = 0;
  65.    racing = 0;
  66.   }
  67.  
  68.   if (loaded && digitalRead(SWITCHES[0]) == LOW){
  69.     racing = 1;
  70.   }
  71.   if (racing && loaded && digitalRead(SWITCHES[0]) == HIGH){
  72.      siren();
  73.   }
  74.   else
  75.   {
  76.     noTone(SPEAKER);
  77.     for(i=0;i<8;i++){
  78.       digitalWrite(LEDS[i],Normal[i]);
  79.     }
  80.   }
  81. }
  82.  
  83. void waiting_to_go(){
  84. }
  85.  
  86. void ready_state(){
  87.   tone(SPEAKER,900);
  88.   digitalWrite(LEDS[2],LOW);
  89.   digitalWrite(LEDS[3],LOW);
  90.   delay(50);
  91.   noTone(SPEAKER);
  92.   digitalWrite(LEDS[2],HIGH);
  93.   digitalWrite(LEDS[3],HIGH);
  94.   delay(50);
  95.   tone(SPEAKER,900);
  96.   digitalWrite(LEDS[2],LOW);
  97.   digitalWrite(LEDS[3],LOW);
  98.   delay(50);
  99.   noTone(SPEAKER);
  100.   digitalWrite(LEDS[2],HIGH);
  101.   digitalWrite(LEDS[3],HIGH);
  102.   loaded = 1;
  103. }
  104.  
  105. void siren(){
  106.   int i;
  107.   int k;
  108.   int l;
  109.   while ((digitalRead(SWITCHES[0]) == HIGH) && (digitalRead(SWITCHES[1]) == LOW)) {
  110.     for (k=0;k<8;k++){
  111.       for (l=0;l<8;l++){
  112.         digitalWrite(LEDS[l],Light_Pattern[k*2][l]);
  113.       }
  114.       for (i=650;i<900;i=i+2){
  115.         tone(SPEAKER,i);
  116.         delay(1);
  117.       }
  118.       for (l=0;l<8;l++){
  119.         digitalWrite(LEDS[l],Light_Pattern[(k*2)+1][l]);
  120.       }
  121.       for (i=900;i>650;i=i-2){
  122.         tone(SPEAKER,i);
  123.         delay(1);
  124.       }
  125.     }  
  126.   }
  127. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top