Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #define PIN 9 // The pin the LED's are connected to.
- #define TRIG 2 //The button sensing the trigger
- #define THUM 3 //The button sensing the location of the thumb (right hand)
- //#define SWITCH 4 **UNUSED**
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(9, PIN, NEO_GRB + NEO_KHZ800);
- // AN IMPORTANT ANNOUNCEMENT FROM ADAFRUIT:
- // To reduce NeoPixel burnout risk, add 1000 uF capacitor across
- // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
- // and minimize distance between Arduino and first pixel. Avoid connecting
- // on a live circuit...if you must, connect GND first.
- //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.
- int powerup_num = 3; //the number of powerups the player gets.
- //counting the hits on the player
- int hitcount = 0;
- int hitlimit = 2; //aka lives, the amount of hits a player can have before they are 'dead'
- int r = 0;
- int g = 0;
- int b = 0;
- //The "state" of the player: 0 = off/deactivated, 1 = normal player, 2 = "shot" player, 3 = "dead" player.
- int state = 0;
- //Determines team the player is on, either blue (1) orange(2), (3) which is FFA (purple), and (4) which is dark mode (no color).
- //Zero means the gun is awaiting activation.
- int team = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(TRIG, INPUT);
- pinMode(THUM, INPUT);
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- waitForActivation(); //wait for the gun to be activated, this is when the teams are chosen.
- }
- void loop() {
- if(digitalRead(TRIG) == 1){ //check if the user has registerd a hit.
- delay(150);
- if(digitalRead(THUM) == 1){ //this little bit of code adds a bit of slack when deciding if 1 or 2 buttons are being pressed.
- hit();
- }
- } else if(digitalRead(THUM) == 1 && digitalRead(TRIG != 1)){ //check if the user wants to use powerup
- delay(250);
- if(digitalRead(TRIG) != 1){
- activatepowerup();
- }
- }
- for(int i = 0; i < 9; i++)
- {
- strip.setPixelColor(i,r,g,b);
- }
- strip.show();
- }
- ////////////////////////////////////////////////
- //--------------------------------------------//
- ////////////////////////////////////////////////
- void activatepowerup(){
- powerup_num--; //take away one powerup
- if(powerup_num > 0){
- Serial.println("");
- Serial.println("Powerup Activated: Sneak Attack");
- Serial.println("");
- for(int i = 0; i < 9; i++) //wait 8 seconds with the gun black to hide the player.
- {
- strip.setPixelColor(i,0,0,0);
- }
- strip.show();
- delay(8000);
- Serial.println("Powerup Used");
- Serial.print(powerup_num-1);
- Serial.println(" remain");
- for(int j = 0; j < 6; j++){
- for(int i = 0; i < 9; i++) //Now strobe bright white for 3 seconds to expose the player.
- {
- strip.setPixelColor(i,255,255,255);
- }
- strip.show();
- delay(250);
- for(int i = 0; i < 9; i++) //Now strobe bright white for 3 seconds to expose the player.
- {
- strip.setPixelColor(i,0,0,0);
- }
- strip.show();
- delay(250);
- }
- }
- }
- void hit(){
- if(hitcount >= hitlimit){ //if all the 'lives' have been used up
- for(int i = 0; i < 9; i++) //go bright white for a second then go red
- {
- Serial.println("DEAD");
- strip.setPixelColor(i,255,255,255);
- }
- strip.show();
- delay(1000);
- Serial.println("Player has been killed");
- while(true){ //death animation (infinite while(true) loop :D)
- for(int aa = 0; aa < 3; aa++){
- for(int i = 0; i < 9; i++)
- {
- strip.setPixelColor(i+1,0,0,0);
- strip.setPixelColor(i-1,0,0,0);
- strip.setPixelColor(i,100,0,0);
- strip.show();
- delay(25);
- }
- }
- for(int i = 0; i < 9; i++)
- {
- strip.setPixelColor(i,255,0,0);
- }
- strip.show();
- delay(500);
- }
- } else { // or... just lose 1 life and be dead for 20 seconds.
- for(int g = 0; g < 80; g++){ // flash red for 20 seconds (death timer)
- for(int i = 0; i < 9; i++)
- {
- strip.setPixelColor(i,100,0,0);
- }
- strip.show();
- delay(200);
- for(int i = 0; i < 9; i++)
- {
- strip.setPixelColor(i,0,0,0);
- }
- strip.show();
- delay(50);
- }
- hitcount++;
- Serial.println("Lost a life");
- Serial.print("Hit ");
- Serial.print(hitcount);
- Serial.println(" times");
- Serial.print("Max hit limit is ");
- Serial.println(hitlimit);
- }
- }
- void waitForActivation(){
- Serial.println("Choosing teams...");
- while(digitalRead(TRIG) != 1){ //Use the Thumb switch to toggle through teams and then the trigger to select the team.
- if(digitalRead(THUM) == 1)
- {
- team++;
- if(team > 4) team = 1;
- if(team == 1) strip.setPixelColor(7,0,0,255); // the seventh LED is the one facing downward at the back.
- if(team == 2) strip.setPixelColor(7,255,128,0); // These colors show what team is being selected
- if(team == 3) strip.setPixelColor(7,255,0,255); // Purple represents FFA
- if(team == 4) strip.setPixelColor(7,255,255,255); // White represents black out FFA (dim color)
- strip.show();
- delay(250);
- }
- }
- Serial.println("Gun Activated!, Team Chosen!"); //Used for debugging
- Serial.print("Team ");
- Serial.print(team);
- Serial.println(" chosen.");
- //Setting the background colors for the teams:
- switch(team){
- case 1:
- r = 0;
- g = 0;
- b = 255;
- break;
- case 2:
- r = 255;
- g = 128;
- b = 0;
- break;
- case 3:
- r = 255;
- g = 0;
- b = 255;
- break;
- case 4:
- r = 32;
- g = 32;
- b = 32;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement