Project tutorial
Pinball Sounds and Music

Pinball Sounds and Music

A previous project of an Arduino controlled pinball machine is augmented with sound and music using a WAV Trigger board.

  • 6,097 views
  • 3 comments
  • 18 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

About this project

In January, I uploaded a project called “Arduino Controlled Pinball Machine”.

https://www.hackster.io/BobB/arduino-controlled-pinball-machine-525863

It was a tutorial on how to build a pinball machine using an Arduino to control the game play. One of the biggest differences between this unit and a commercial pinball machine was the absence of sound effects and music. These elements are a key component of pinball experience. This project describes how to add these to a pinball machine using a Wav Trigger board. That board is then directed by the Arduino controlling the pinball machine game play. This allows me to change songs with every ball and trigger sound effects when various pinball components are activated.

The Wav Trigger is an excellent board to use for this application. The main reason is that it is polyphonic. This means that it can play multiple sound files as the same time. This is critical in this project, since we want the sound effects not to affect the music playing in the background.

To ready the Wav Trigger board for use, I first had to attach a header. If you are like me and had never previously soldered a header to a board, I strongly suggest that you check out the link below. Specifically, I recommend watching the video called “Soldering Your First Component”. It does and nice job and can help prevent a lot of problems.

https://learn.sparkfun.com/tutorials/how-to-solder---through-hole-soldering

At this point, I screwed the board to the underside of the pinball machine and connected it as shown in the picture below.

For a power adapter, I used a 6V unit that I happened to have lying around. The stereo cable runs to a pair of old computer speakers. These speakers get their power from a USB port, so I simply used a USB-to-AC adapter so that I could plug them into the power strip mounted on the back of the pinball machine. The Ground wire simply ran back to the ground for the Arduino board and the only other wire went from the RX location on the Wav Trigger to the Digital i/o port #46 on the Arduino Mega 2560 I used to run the pinball machine. (We’ll talk about why #46 later!)

I then loaded my .wav files unto the SDHC memory card. The Wav Trigger requires .wav files that are designed to run at 44.1kHz. To convert my sound files to this format, I did as the Wav Trigger manufacturer (robertsonics) recommended and downloaded a free program called Audacity from the website below. It did a nice job. Simply load the file, select 44.1 kHz in the bottom left of the screen, and then export it as windows .wav file.

https://sourceforge.net/projects/audacity/

For the files, I used 5 music files, one for each ball you get when playing the pinball machine. I also downloaded some pinball special effects sounds from the link below. While these files were already in .wav format, I still had to import them into Audacity to change them to the 44.1kHz format.

https://www.freesound.org/people/relwin/packs/10675/

That’s it for the hardware. For the Arduino software, I needed to install two additional libraries. If you’ve never installed Arduino libraries before, you can find the procedure at the link below.

https://www.arduino.cc/en/Guide/Libraries

The first library you will need is called AltSoftSerial. Which can be obtained from the link below.

https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

This library handles the serial communication between the Wav Trigger and the Arduino. You may recall that I had previously used a similar library called SoftSerial to handle communication between the Arduino and the LCD display I used to show the score and ball #. Fortunately, these programs don’t conflict.

On the above website, you will find the number of the pin you need to use for your particular Arduino board. For the Mega 2560 I used, I need pin 46, which is why I connected the Wav Trigger as described above.

The other library you will need is called the “WAV-Trigger-Arduino-Serial-Library”.

https://github.com/robertsonics/WAV-Trigger-Arduino-Serial-Library

This is where you will find all the different commands you can do with the board. While the board can do much more, I only needed to start and stop the playing of specific .wav files on command. To do so, I needed to use the following commands.

wTrig.start() – Used at the start to initialize serial communication.

wTrig.masterGain(int gain) -- To set the initial volume. I used a value of zero for the int gain. My speakers have a volume control on them, so I didn’t have to be too picky.

wTrig.trackPlayPoly(int t) – This is the command that you need to run a specific .wav file. When loading the files onto the memory chip, it is import to rename the files so that they begin with a three digit number, such as 001, 002, etc. It is this number that you will use to select the file that you will run.

wTrig.stopAllTracks() -- Stops all tracks.


And that’s it! The attached video will show you what it all sounds like. The attached code will show you how the above functions were used to get the sounds/music to work when needed.

I hope you found this interesting and helpful.

Bob




Code

Arduino Controlled Pinball Machine with Musica and SoundArduino
Code for and Arduino Mega 2560 Board to control a pinball machine and Wav Trigger board.
  const int TxPin = 17;
  long Score = 0;
  long OldScore = 0;
  long Target = 1;
  long Pop = 1;
  long Roll = 10;
  int Targets[8];
  int Rolls[3];
  int Pops[4];
  int Milli = 10;
  int Sum = 0;
  int Flash = 100;
  int Ball = 0;
  int i=0;
  int Shot = 0;
  int Lost = 0;
  int Pressure = 1024;
  
#include <SoftwareSerial.h>;
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
#include <AltSoftSerial.h>    // Arduino build environment requires this
#include <wavTrigger.h>
wavTrigger wTrig;       

void setup() {
  /* Words without an s are the value achieved by interacting with a device. 
   * Works with an s keep track of which individual ones were interacted with. 
   * The latter is needed to determine when all have been hit and the value needs upgrading
   * and the lights need turning off.
   */
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
  mySerial.begin(9600);
  mySerial.write(12);                 // Clear             
  mySerial.write(17);                 // Turn backlight on
  wTrig.start();
   // If the Uno is powering the WAV Trigger, we should wait for the WAV Trigger
  //  to finish reset before trying to send commands.
  delay(1000);
  
  // If we're not powering the WAV Trigger, send a stop-all command in case it
  //  was already playing tracks. If we are powering the WAV Trigger, it doesn't
  //  hurt to do this.
  wTrig.stopAllTracks();
  wTrig.masterGain(0);                  // Reset the master gain to 0dB                               
   
  //target inputs
  pinMode(2,INPUT_PULLUP);
  pinMode(3,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
  pinMode(7,INPUT_PULLUP);
  pinMode(8,INPUT_PULLUP);
  pinMode(9,INPUT_PULLUP);
  //rollover inputs
  pinMode(10,INPUT_PULLUP);
  pinMode(11,INPUT_PULLUP);
  pinMode(12,INPUT_PULLUP);
  //lower ball shot switch
  pinMode(15,INPUT_PULLUP);
  //upper ball shot switch
  pinMode(16,INPUT_PULLUP);
  //lcd output
  pinMode(17,OUTPUT);
  //target lights, respective
  pinMode(32,OUTPUT);
  pinMode(33,OUTPUT);
  pinMode(34,OUTPUT);
  pinMode(35,OUTPUT);
  pinMode(36,OUTPUT);
  pinMode(37,OUTPUT);
  pinMode(38,OUTPUT);
  pinMode(39,OUTPUT);
  //rollover lights, respective
  pinMode(40,OUTPUT);
  pinMode(41,OUTPUT);
  pinMode(42,OUTPUT);
  //pop bumper lights
  pinMode(50,OUTPUT);
  pinMode(51,OUTPUT);
  pinMode(52,OUTPUT);
  pinMode(53,OUTPUT);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  //If a pull-down resistor is used, the input pin will be LOW when the switch is open and HIGH when the switch is closed. 
  //check if a target was hit

//****** Targets *****

  for (int i=0; i<8; i++){
    if (digitalRead(i+2) == LOW){
      //Target activated
      Targets[i]=1;
      Score = Score + Target;
      //turn on Target light
      digitalWrite(i+32,HIGH);
      //delay so as not get multiple points for one hit
      wTrig.trackPlayPoly(8);
      delay(Milli);
      break;
    }
  }
  Sum = 0;  
  for (int i=0; i<8; i++){
    Sum = Sum + Targets[i];
  }
  if (Sum == 8){
    //all Targets lit, so flash and then turn off.
    for (int j=0; j<3; j++){
      for (int i=0; i<8; i++){
        digitalWrite(i+32, LOW);
      }
      delay(Flash);
      for (int i=0; i<8; i++){
        digitalWrite(i+32, HIGH);
      }
      delay(Flash);
    }
    for (int i=0; i<8; i++){
      digitalWrite(i+32, LOW);
      Targets[i]=0;
    } 
    delay(Flash);   
    //Multiply target value by 10
    Target = Target * 5;
    //goto Skip;  
  }
  

// ***********  Rollovers *********

  
   for (int i=0; i<3; i++){
    if (digitalRead(i+10) == LOW){
      //rollover activated
      Rolls[i]=1;
      Score = Score + Roll;
      //turn on rollover light
      digitalWrite(i+40,HIGH);
      //delay so as not get multiple points for one hit
      wTrig.trackPlayPoly(6);
      delay(Milli);
      break;
    }
  }
  Sum = 0;  
  for (int i=0; i<3; i++){
    Sum = Sum + Rolls[i];
  }
  if (Sum == 3){
    //all rollovers lit, so flash and then turn off.
    for (int j=0; j<3; j++){
      for (int i=0; i<3; i++){
        digitalWrite(i+40, LOW);
      }
      delay(Flash);
      for (int i=0; i<3; i++){
        digitalWrite(i+40, HIGH);
      }
      delay(Flash);
    }
    for (int i=0; i<3; i++){
      digitalWrite(i+40, LOW);
      Rolls[i]=0;
    } 
    delay(Flash);   
    //Multiply score by 2
    Score = Score * 2;
    Roll = Roll * 10;
    //goto Skip;  
  }
  
  //**********  Pop Bumpers **********
  
   for (int i=0; i<4; i++){
    if (analogRead(i) > 500){
      //pop activated
      Pops[i]=1;
      Score = Score + Pop;
      //turn on pop bumper light
      digitalWrite(i+50,HIGH);
      //delay so as not get multiple points for one hit
      wTrig.trackPlayPoly(7);
      delay(Milli);
      break;
    }
  }
  Sum = 0;  
  for (int i=0; i<4; i++){
    Sum = Sum + Pops[i];
  }
  if (Sum == 4){
    //all pop bumpers lit, so flash and then turn off.
    for (int j=0; j<3; j++){
      for (int i=0; i<4; i++){
        digitalWrite(i+50, LOW);
      }
      delay(Flash);
      for (int i=0; i<4; i++){
        digitalWrite(i+50, HIGH);
      }
      delay(Flash);
    }
    for (int i=0; i<4; i++){
      digitalWrite(i+50, LOW);
      Pops[i]=0;
    } 
    delay(Flash);   
    //Multiply target value by 10
    Pop = Pop * 2;
    //goto Skip;  
  }
Skip:
 
  if (digitalRead(15) == LOW){   
    //ball hit lower alley switch
    //if not already done so, increase Ball
    if (Shot == 0){
        if (Ball == 0){
          Score = 0;
        }
      //Set Lost = 0 since not on pressure pad
      Lost = 0;
      Pressure = analogRead(7) + 15;  
      //set OldScore so as to reprint ball value on LCD
      OldScore =-1;
      Ball = Ball + 1;
      wTrig.stopAllTracks();
      delay(Milli);    
      wTrig.trackPlayPoly(Ball);
      Shot = 1;
      delay(Milli);
    }
  }
  if (analogRead(7) > Pressure){
    //ball on pressure pad
    Shot = 0;
    if (Lost == 0){
      //mySerial.print(analogRead(7));
      //Score = Score + 100;
      Lost = 1;
      if (Ball == 5){
        //Game Over
        //flash rollovers and then turn off.
        for (int j=0; j<3; j++){
          for (int i=0; i<3; i++){
            digitalWrite(i+40, LOW);
          }
          delay(Flash);
          for (int i=0; i<3; i++){
            digitalWrite(i+40, HIGH);
          }
          delay(Flash);
        }
        for (int i=0; i<3; i++){
          digitalWrite(i+40, LOW);
          Rolls[i]=0;
        } 
        // flash pop bumpers and then turn off
        for (int j=0; j<3; j++){
          for (int i=0; i<4; i++){
            digitalWrite(i+50, LOW);
          }
          delay(Flash);
          for (int i=0; i<4; i++){
            digitalWrite(i+50, HIGH);
          }
          delay(Flash);
        }
        for (int i=0; i<4; i++){
          digitalWrite(i+50, LOW);
          Pops[i]=0;
        } 
        //Flash Targets and then turn off.
        for (int j=0; j<3; j++){
          for (int i=0; i<8; i++){
            digitalWrite(i+32, LOW);
          }
          delay(Flash);
          for (int i=0; i<8; i++){
            digitalWrite(i+32, HIGH);
          }
          delay(Flash);
        }
        for (int i=0; i<8; i++){
          digitalWrite(i+32, LOW);
          Targets[i]=0;
        } 
        mySerial.write(12);                 // Clear
        delay(5);
        // Required delay
        mySerial.print(Score);  // First line
        mySerial.write(13);                 // Form feed
        mySerial.print("Game Over!!!");   // Second line
        Ball = 0;
        Target = 1;
        Roll = 1;
        Pop = 1;
        wTrig.stopAllTracks();
        delay(Milli);
        wTrig.trackPlayPoly(10);
        delay(Milli);
      }
    }
  }
  //print to LCD
  if (Score != OldScore){ 
  mySerial.write(12);                 // Clear
  delay(5);                           // Required delay
  //mySerial.print(analogRead(7));
  mySerial.print(Score);  // First line
  mySerial.write(13);                 // Form feed
  mySerial.print("Ball = ");   // Second line
  mySerial.print(Ball);
  OldScore = Score;
  }
}

Schematics

WAV Trigger Shield Connections
Wav%20trigger%20image%202

Comments

Similar projects you might like

Arduino Controlled Pinball Machine

Project tutorial by Bob Blomquist

  • 31,929 views
  • 47 comments
  • 94 respects

Homemade Arduino Pinball Machine

Project showcase by Frogger1108

  • 11,176 views
  • 15 comments
  • 50 respects

Fingerscan: Music at Your Fingertips

Project showcase by touchmysound

  • 9,379 views
  • 6 comments
  • 29 respects

Arduino Conversion of an EM Pinball Machine

Project showcase by Frogger1108

  • 3,965 views
  • 8 comments
  • 26 respects

Build a Pinball Machine

Project showcase by grahamasker

  • 3,015 views
  • 9 comments
  • 10 respects

Control Music Volume and Ledbar Using Slider of 1Sheeld

Project tutorial by ahmed ismail

  • 1,576 views
  • 2 comments
  • 12 respects
Add projectSign up / Login