Panel Cookies
Shocking Game - Code
06/09/2021 | Views: 5817 | Arduino | by: ELECTRONOOBS      


Get the code from below and maybe consider supporting my work as well. You will aslo need the library for the DFplayer and the RGB LED strip so download taht as well and go to sketch, include library and add .zip library and there select the zip file you have just downloaded. Compile, connect the USB connector adn upload the code. Read the comments in the code for more.







//Library for the DFplayer 
#include <SoftwareSerial.h>     //We need this for serial on pin 10 and 11
#include <DFPlayer_Mini_Mp3.h>  //Download here: https://electronoobs.com/eng_arduino_DFPlayer_Mini_Mp3.php
SoftwareSerial DFPlayerSerial(10, 11); // DFPlayer RX and TX pins
//FUNCTIONS To use with DFplayer library
/*mp3_play();      //start play
  mp3_play(5);     //play "mp3/0005.mp3"
  mp3_pause();
  mp3_stop();
  mp3_next();      
  mp3_prev();
  mp3_set_volume(uint16_t volume);   //0~30
  mp3_set_EQ();   //0~5
  void mp3_single_loop(boolean state);   //set single loop
  void mp3_random_play();*/

//Library for the WS2812 LED strip
#include <Adafruit_NeoPixel.h>  //Downlaod it here: https://electronoobs.com/eng_arduino_Adafruit_NeoPixel.php
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            13
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      4
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


//Inputs/Outputs
int start_button = 7;
int p1_button = 5;
int p2_button = 6;
int p3_button = 4;
int p4_button = 3;
int p1_zap = 9;
int p2_zap = 2;
int p3_zap = 8;
int p4_zap = 12;


//Variables
int mode = 0; //mode 0 = Off
unsigned long previousMillis,currentMillis, counter;  
bool start_button_state = false;
int num_players = 2;
bool play_once = false;
int rgb_value = 0;
bool rbg_rising = false;
int player_1_button = 0;
int player_2_button = 0;
int player_3_button = 0;
int player_4_button = 0;
int players_added = 0;
bool player_1_selected = false;
bool player_2_selected = false;
bool player_3_selected = false;
bool player_4_selected = false;
int looser_led = 0;
int looser_number = 1;
int players[4] = {0,0,0,0};
float jump_rate = 200;
int random_looser = 0;
int looser_counter = 0;
int the_looser = 0;
bool HV_ON = HIGH;

void setup()
{ 
  //Put all transformers to OFF
  pinMode(p2_zap, OUTPUT);
  digitalWrite(p2_zap, LOW);
  pinMode(p1_zap, OUTPUT);
  digitalWrite(p1_zap, LOW);  
  pinMode(p3_zap, OUTPUT);
  digitalWrite(p3_zap, LOW);
  pinMode(p4_zap, OUTPUT);
  digitalWrite(p4_zap, LOW);
  
  pinMode(start_button, INPUT_PULLUP);
  pinMode(p1_button, INPUT_PULLUP);
  pinMode(p2_button, INPUT_PULLUP);
  pinMode(p3_button, INPUT_PULLUP);
  pinMode(p4_button, INPUT_PULLUP);
  
  Serial.begin(9600);
  DFPlayerSerial.begin(9600);
  mp3_set_serial(DFPlayerSerial);
  mp3_set_volume(30);
  delay(100);
  pixels.begin(); // This initializes the NeoPixel library.
  delay(100);
  Serial.println("Game is booting");
  Serial.println(" ");
  play_hello();

  currentMillis = millis();
  previousMillis = currentMillis;

  randomSeed(analogRead(0));  //We use this function to get a real random number using random function
}

void loop(){
  //Mode 0 (we wait for user to press the start button)
  if(mode == 0){
    if(!digitalRead(start_button)){
      currentMillis = millis();
      start_button_state = true;
      if(currentMillis - previousMillis > 2000){    //If the push over 2s, we start the game
        previousMillis = currentMillis;
        mode = 1;   //Mode 1 = game start
        Serial.println("Game starts");
      }    
    }
    else{
      if(start_button_state){
        counter = currentMillis - previousMillis;
        if(counter > 60 && counter < 1000){         //If the push time is below 1s, then we change the amount of players
          
          num_players++;
          if(num_players > 4){
            num_players = 2;
          }
          if(num_players == 2){
            mp3_play(2);
          }
          if(num_players == 3){
            mp3_play(3);
          }
          if(num_players == 4){
            mp3_play(4);
          }
          Serial.print("Amount of players: ");
          Serial.println(num_players);
        }      
        start_button_state = false;
      }
      
      previousMillis = millis();   
    }
  }//end mode == 0;



  //Mode 1 (game starts so we wait for players to touch their pads)
  else if(mode == 1){
    if(!play_once){
      mp3_play(5);  
      Serial.print("All players (");
      Serial.print(num_players);
      Serial.print(") touch their pad");
      play_once = true;  
    }
    currentMillis = millis();    
    if(currentMillis - previousMillis > 5){
      previousMillis = currentMillis;
      if(rbg_rising){
        rgb_value++;
        if(rgb_value > 254){
          rbg_rising = false;
        }
      }
      else{
        rgb_value--;
        if(rgb_value < 1){
          rbg_rising = true;
        }
      }
      pixels.setPixelColor(0, pixels.Color(rgb_value,rgb_value,0)); // Moderately bright green color. 
      pixels.setPixelColor(1, pixels.Color(rgb_value,rgb_value,0)); // Moderately bright green color. 
      pixels.setPixelColor(2, pixels.Color(rgb_value,rgb_value,0)); // Moderately bright green color. 
      pixels.setPixelColor(3, pixels.Color(rgb_value,rgb_value,0)); // Moderately bright green color. 
      pixels.show(); // This sends the updated pixel color to the hardware.
    }
    
    ///////////////////////////////////////////////////////////
    if(!digitalRead(p1_button) && !player_1_selected){
      if(player_1_button == 0){
        player_1_button = 1;
        players_added++;
        player_1_selected = true;
        Serial.println("Entered as PLAYER 1");
      }
      else if (player_2_button == 0){
        player_2_button = 1;
        players_added++;
        player_1_selected = true;
        Serial.println("Entered as PLAYER 2");
      }
      else if (player_3_button == 0){
        player_3_button = 1;
        players_added++;
        player_1_selected = true;
        Serial.println("Entered as PLAYER 3");
      }
      else if (player_4_button == 0){
        player_4_button = 1;
        players_added++;
        player_1_selected = true;
        Serial.println("Entered as PLAYER 4");
      }
      Serial.print("Players in game: ");
      Serial.println(players_added);
    }
    ///////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////
    if(!digitalRead(p2_button) && !player_2_selected){
      if(player_1_button == 0){
        player_1_button = 2;
        players_added++;
        player_2_selected = true;
        Serial.println("Entered as PLAYER 1");
      }
      else if (player_2_button == 0){
        player_2_button = 2;
        players_added++;
        player_2_selected = true;
        Serial.println("Entered as PLAYER 2");
      }
      else if (player_3_button == 0){
        player_3_button = 2;
        players_added++;
        player_2_selected = true;
        Serial.println("Entered as PLAYER 3");
      }
      else if (player_4_button == 0){
        player_4_button = 2;
        players_added++;
        player_2_selected = true;
        Serial.println("Entered as PLAYER 4");
      }
      Serial.print("Players in game: ");
      Serial.println(players_added);
    }
    ///////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////
    if(!digitalRead(p3_button) && !player_3_selected){
      if(player_1_button == 0){
        player_1_button = 3;
        players_added++;
        player_3_selected = true;
        Serial.println("Entered as PLAYER 1");
      }
      else if (player_2_button == 0){
        player_2_button = 3;
        players_added++;
        player_3_selected = true;
        Serial.println("Entered as PLAYER 2");
      }
      else if (player_3_button == 0){
        player_3_button = 3;
        players_added++;
        player_3_selected = true;
        Serial.println("Entered as PLAYER 3");
      }
      else if (player_4_button == 0){
        player_4_button = 3;
        players_added++;
        player_3_selected = true;
        Serial.println("Entered as PLAYER 4");
      }
      Serial.print("Players in game: ");
      Serial.println(players_added);
    }
    ///////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////
    if(!digitalRead(p4_button) && !player_4_selected){
      if(player_1_button == 0){
        player_1_button = 4;
        players_added++;
        player_4_selected = true;
        Serial.println("Entered as PLAYER 1");
      }
      else if (player_2_button == 0){
        player_2_button = 4;
        players_added++;
        player_4_selected = true;
        Serial.println("Entered as PLAYER 2");
      }
      else if (player_3_button == 0){
        player_3_button = 4;
        players_added++;
        player_4_selected = true;
        Serial.println("Entered as PLAYER 3");
      }
      else if (player_4_button == 0){
        player_4_button = 4;
        players_added++;
        player_4_selected = true;
        Serial.println("Entered as PLAYER 4");
      }
      Serial.print("Players in game: ");
      Serial.println(players_added);
    }
    ///////////////////////////////////////////////////////////


    if(players_added == num_players){
      mp3_play(6);  //Suspense sound
      jump_rate = (1.0/num_players) * 300.0; 
      mode = 2;   //Roulette starts spinning
      random_looser = random(20, 100);
      looser_counter = 0;
      the_looser = 0;
      Serial.println(" ");
      Serial.println("Roulette starts!!!");      
      Serial.print("Looser will be at jump ");
      Serial.print(random_looser);
      Serial.println(" ");
      
    }    
  }//end of mode == 1




  //Mode 2 (all players have touched their pads so now we start the roulette)
  if(mode == 2){
    currentMillis = millis();    
    if(currentMillis - previousMillis > jump_rate){
      previousMillis = currentMillis;      
      looser_number++;
      if(looser_number > num_players){
        looser_number = 1;
      }

      looser_counter++;
      if(looser_counter >= random_looser){                
        mode=3;
      }
      
      if(looser_number == 1){
        if(player_1_button == 1){
          all_leds_off();
          pixels.setPixelColor(0, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 1;
        }
        else if(player_1_button == 2){
          all_leds_off();
          pixels.setPixelColor(1, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 2;
        }
        else if(player_1_button == 3){
          all_leds_off();
          pixels.setPixelColor(2, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 3;
        }
        else if(player_1_button == 4){
          all_leds_off();
          pixels.setPixelColor(3, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 4;
        }
      }

      if(looser_number == 2){
        if(player_2_button == 1){
          all_leds_off();
          pixels.setPixelColor(0, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 1;
        }
        else if(player_2_button == 2){
          all_leds_off();
          pixels.setPixelColor(1, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 2;
        }
        else if(player_2_button == 3){
          all_leds_off();
          pixels.setPixelColor(2, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 3;
        }
        else if(player_2_button == 4){
          all_leds_off();
          pixels.setPixelColor(3, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 4;
        }
      }

      if(looser_number == 3){
        if(player_3_button == 1){
          all_leds_off();
          pixels.setPixelColor(0, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 1;
        }
        else if(player_3_button == 2){
          all_leds_off();
          pixels.setPixelColor(1, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 2;
        }
        else if(player_3_button == 3){
          all_leds_off();
          pixels.setPixelColor(2, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 3;
        }
        else if(player_3_button == 4){
          all_leds_off();
          pixels.setPixelColor(3, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 4;
        }
      }

      if(looser_number == 4){
        if(player_4_button == 1){
          all_leds_off();
          pixels.setPixelColor(0, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.    
          the_looser = 1;      
        }
        else if(player_4_button == 2){
          all_leds_off();
          pixels.setPixelColor(1, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 2;
        }
        else if(player_4_button == 3){
          all_leds_off();
          pixels.setPixelColor(2, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 3;
        }
        else if(player_4_button == 4){
          all_leds_off();
          pixels.setPixelColor(3, pixels.Color(0,255,0)); // Moderately bright green color.       
          pixels.show(); // This sends the updated pixel color to the hardware.
          the_looser = 4;
        }
      }
     
      /*If someone releases the button, we go back to mode 1 and wait
        for all players to join*/
      if(check_players(num_players)){
        mode = 1; 
        play_once = false;
        players_added = 0;
        player_1_button = 0;
        player_2_button = 0;
        player_3_button = 0;
        player_4_button = 0;
        player_1_selected = 0;
        player_2_selected = 0;
        player_3_selected = 0;
        player_4_selected = 0;
      }

      
      
    }
  }//end of mode == 2


  //Mode 1 (A random looser was selected and we apply a "zap")
  if(mode == 3){
    mp3_play(7);  //Zap sound
    play_once = false;
    players_added = 0;
    player_1_button = 0;
    player_2_button = 0;
    player_3_button = 0;
    player_4_button = 0;
    player_1_selected = 0;
    player_2_selected = 0;
    player_3_selected = 0;
    player_4_selected = 0;
    Serial.print("The looser is on button ");
    Serial.println(looser_number);
    if(the_looser == 1){
      digitalWrite(p1_zap, HV_ON);
      all_leds_off();
      pixels.setPixelColor(0, pixels.Color(255,0,0)); // Moderately bright green color.       
      pixels.show(); // This sends the updated pixel color to the hardware.
      delay(400);
      digitalWrite(p1_zap, LOW);
      delay(3000);
      blink_red(0);
      mode = 0;
    }
    else if(the_looser == 2){
      digitalWrite(p2_zap, HV_ON);
      all_leds_off();
      pixels.setPixelColor(1, pixels.Color(255,0,0)); // Moderately bright green color.       
      pixels.show(); // This sends the updated pixel color to the hardware.
      delay(400);
      digitalWrite(p2_zap, LOW);
      delay(3000);
      blink_red(1);
      mode = 0;
    }
    else if(the_looser == 3){
      digitalWrite(p3_zap, HV_ON);
      all_leds_off();
      pixels.setPixelColor(2, pixels.Color(255,0,0)); // Moderately bright green color.       
      pixels.show(); // This sends the updated pixel color to the hardware.
      delay(400);
      digitalWrite(p3_zap, LOW);
      delay(3000);
      blink_red(2);
      mode = 0;
    }
    else if(the_looser == 4){
      digitalWrite(p4_zap, HV_ON);
      all_leds_off();
      pixels.setPixelColor(3, pixels.Color(255,0,0)); // Moderately bright green color.       
      pixels.show(); // This sends the updated pixel color to the hardware.
      delay(400);
      digitalWrite(p4_zap, LOW);
      delay(3000);
      blink_red(3);
      mode = 0;
    }
    all_leds_off();
    pixels.show();
  }  
}



//Used functions!
void play_hello(){
  mp3_play(1);
  int x = 0;
  while(x<18){
    for(int i=0; i < 5; i++){
      if(i>0){
        pixels.setPixelColor(i-1, pixels.Color(0,0,0)); // Moderately bright green color.
        pixels.setPixelColor(i, pixels.Color(x*14,80,x*6)); // Moderately bright green color.
      }
      else{
        pixels.setPixelColor(0, pixels.Color(0,0,0)); // Moderately bright green color.
        pixels.setPixelColor(i, pixels.Color(x*14,80,x*6)); // Moderately bright green color.
      }      
      pixels.show(); // This sends the updated pixel color to the hardware.
      delay(30);
    }
    x++;
  }
}


void all_leds_off(){
  pixels.setPixelColor(0, pixels.Color(0,0,0)); //All colors off
  pixels.setPixelColor(1, pixels.Color(0,0,0)); //All colors off
  pixels.setPixelColor(2, pixels.Color(0,0,0)); //All colors off
  pixels.setPixelColor(3, pixels.Color(0,0,0)); //All colors off
}


bool check_players (int i){
  int players_detected = 0;
  if(!digitalRead(p1_button)){
    players_detected++;    
  }
  if(!digitalRead(p2_button)){
    players_detected++;    
  }
  if(!digitalRead(p3_button)){
    players_detected++;    
  }
  if(!digitalRead(p4_button)){
    players_detected++;    
  }
  if(players_detected >= i){
    return false;
  }
  else{
    return true;
  }
  
}


void blink_red(int i){
  for (int x=0; x<3; x++){
    pixels.setPixelColor(i, pixels.Color(255,0,0)); 
    pixels.show(); 
    delay(400);
    pixels.setPixelColor(i, pixels.Color(0,0,0)); 
    pixels.show(); 
    delay(400);
  }
  
}













Last tutorials

All about Arduino PWM frequencies
RLC Transistor Tester PCB with Arduino
Ferrofluid 3D printed Bluetooth Speaker
10A Bharger/Protection Module
TMC silent driver comparison A4988 stepper driver

ADVERTISERS



Affiliate Disclosure

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel Intermedio