Advertisement
Guest User

Untitled

a guest
Dec 10th, 2015
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. #define PIN 9 // The pin the LED's are connected to.
  4.  
  5. #define TRIG 2 //The button sensing the trigger
  6. #define THUM 3 //The button sensing the location of the thumb (right hand)
  7. //#define SWITCH 4 **UNUSED**
  8.  
  9. Adafruit_NeoPixel strip = Adafruit_NeoPixel(9, PIN, NEO_GRB + NEO_KHZ800);
  10.  
  11. // AN IMPORTANT ANNOUNCEMENT FROM ADAFRUIT:
  12. // To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  13. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  14. // and minimize distance between Arduino and first pixel.  Avoid connecting
  15. // on a live circuit...if you must, connect GND first.
  16.  
  17. //powerup setting - by default, the powerup is: "Sneak Attack" where the gun goes black for 8 seconds but will strobe bright white for 3 seconds afterward.
  18. int powerup_num = 3; //the number of powerups the player gets.
  19.  
  20. //counting the hits on the player
  21. int hitcount = 0;
  22. int hitlimit = 2; //aka lives, the amount of hits a player can have before they are 'dead'
  23.  
  24. int r = 0;
  25. int g = 0;
  26. int b = 0;
  27.  
  28. //The "state" of the player: 0 = off/deactivated, 1 = normal player, 2 = "shot" player, 3 = "dead" player.
  29. int state = 0;
  30.  
  31. //Determines team the player is on, either blue (1) orange(2), (3) which is FFA (purple), and (4) which is dark mode (no color).
  32. //Zero means the gun is awaiting activation.
  33. int team = 0;
  34.  
  35. void setup() {
  36.  
  37.   Serial.begin(9600);
  38.  
  39.   pinMode(TRIG, INPUT);
  40.   pinMode(THUM, INPUT);
  41.  
  42.   strip.begin();
  43.   strip.show(); // Initialize all pixels to 'off'
  44.  
  45.   waitForActivation(); //wait for the gun to be activated, this is when the teams are chosen.
  46. }
  47.  
  48. void loop() {
  49.   if(digitalRead(TRIG) == 1){ //check if the user has registerd a hit.
  50.     delay(150);
  51.     if(digitalRead(THUM) == 1){ //this little bit of code adds a bit of slack when deciding if 1 or 2 buttons are being pressed.
  52.       hit();
  53.     }
  54.   } else if(digitalRead(THUM) == 1 && digitalRead(TRIG != 1)){ //check if the user wants to use powerup
  55.     delay(250);
  56.     if(digitalRead(TRIG) != 1){
  57.     activatepowerup();
  58.     }
  59.   }
  60.  
  61.   for(int i = 0; i < 9; i++)
  62.   {
  63.     strip.setPixelColor(i,r,g,b);
  64.   }
  65.     strip.show();
  66. }
  67.  
  68. ////////////////////////////////////////////////
  69. //--------------------------------------------//
  70. ////////////////////////////////////////////////
  71.  
  72. void activatepowerup(){
  73.   powerup_num--; //take away one powerup
  74. if(powerup_num > 0){
  75.   Serial.println("");
  76.   Serial.println("Powerup Activated: Sneak Attack");
  77.   Serial.println("");
  78.    for(int i = 0; i < 9; i++)  //wait 8 seconds with the gun black to hide the player.
  79.     {
  80.        strip.setPixelColor(i,0,0,0);
  81.     }
  82.        strip.show();
  83.        delay(8000);
  84.        Serial.println("Powerup Used");
  85.        Serial.print(powerup_num-1);
  86.        Serial.println(" remain");
  87.        for(int j = 0; j < 6; j++){
  88.    for(int i = 0; i < 9; i++) //Now strobe bright white for 3 seconds to expose the player.
  89.     {
  90.        strip.setPixelColor(i,255,255,255);
  91.     }
  92.        strip.show();
  93.     delay(250);
  94.     for(int i = 0; i < 9; i++) //Now strobe bright white for 3 seconds to expose the player.
  95.     {
  96.        strip.setPixelColor(i,0,0,0);
  97.     }
  98.        strip.show();
  99.     delay(250);
  100.        }
  101.   }
  102. }
  103.  
  104. void hit(){
  105.   if(hitcount >= hitlimit){ //if all the 'lives' have been used up
  106.      for(int i = 0; i < 9; i++) //go bright white for a second then go red
  107.       {
  108.         Serial.println("DEAD");
  109.         strip.setPixelColor(i,255,255,255);
  110.       }
  111.       strip.show();
  112.       delay(1000);
  113.       Serial.println("Player has been killed");
  114.        
  115.       while(true){ //death animation (infinite while(true) loop :D)
  116.         for(int aa = 0; aa < 3; aa++){
  117.         for(int i = 0; i < 9; i++)
  118.       {
  119.         strip.setPixelColor(i+1,0,0,0);
  120.         strip.setPixelColor(i-1,0,0,0);
  121.         strip.setPixelColor(i,100,0,0);
  122.         strip.show();
  123.         delay(25);
  124.       }
  125.       }
  126.       for(int i = 0; i < 9; i++)
  127.       {
  128.         strip.setPixelColor(i,255,0,0);
  129.       }
  130.         strip.show();
  131.         delay(500);
  132.       }
  133.   } else { // or... just lose 1 life and be dead for 20 seconds.
  134.     for(int g = 0; g < 80; g++){ // flash red for 20 seconds (death timer)
  135.       for(int i = 0; i < 9; i++)
  136.       {
  137.         strip.setPixelColor(i,100,0,0);
  138.       }
  139.         strip.show();
  140.         delay(200);
  141.        for(int i = 0; i < 9; i++)
  142.       {
  143.         strip.setPixelColor(i,0,0,0);
  144.       }
  145.         strip.show();
  146.         delay(50);
  147.     }
  148.     hitcount++;
  149.     Serial.println("Lost a life");
  150.     Serial.print("Hit ");
  151.     Serial.print(hitcount);
  152.     Serial.println(" times");
  153.     Serial.print("Max hit limit is ");
  154.     Serial.println(hitlimit);
  155.   }
  156. }
  157.  
  158. void waitForActivation(){
  159.   Serial.println("Choosing teams...");
  160.   while(digitalRead(TRIG) != 1){ //Use the Thumb switch to toggle through teams and then the trigger to select the team.
  161.     if(digitalRead(THUM) == 1)
  162.     {
  163.       team++;
  164.       if(team > 4) team = 1;
  165.       if(team == 1) strip.setPixelColor(7,0,0,255); // the seventh LED is the one facing downward at the back.
  166.       if(team == 2) strip.setPixelColor(7,255,128,0); // These colors show what team is being selected
  167.       if(team == 3) strip.setPixelColor(7,255,0,255); // Purple represents FFA
  168.       if(team == 4) strip.setPixelColor(7,255,255,255); // White represents black out FFA (dim color)
  169.       strip.show();
  170.       delay(250);
  171.     }
  172.   }
  173.  
  174.   Serial.println("Gun Activated!, Team Chosen!"); //Used for debugging
  175.   Serial.print("Team ");
  176.   Serial.print(team);
  177.   Serial.println(" chosen.");
  178.  
  179.   //Setting the background colors for the teams:
  180.     switch(team){
  181.       case 1:
  182.        r = 0;
  183.        g = 0;
  184.        b = 255;
  185.        break;
  186.       case 2:
  187.        r = 255;
  188.        g = 128;
  189.        b = 0;
  190.        break;
  191.       case 3:
  192.        r = 255;
  193.        g = 0;
  194.        b = 255;
  195.        break;
  196.       case 4:
  197.        r = 32;
  198.        g = 32;
  199.        b = 32;
  200.        break;
  201.     }
  202. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement