Bob Blomquist
Published

Pinball Sounds and Music

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

AdvancedFull instructions provided16 hours12,288
Pinball Sounds and Music

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
WAV Trigger
SparkFun WAV Trigger
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

WAV Trigger Shield Connections

Code

Arduino Controlled Pinball Machine with Musica and Sound

Arduino
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;
  }
}

Credits

Bob Blomquist

Bob Blomquist

3 projects • 34 followers
Chemist with computer programming skills.

Comments