Arduino Pinball – targets, breadboard and arduino code

So I have finally found a way to make targets with blinking lights and score. This feels like a good start for coding the rest of the game. It’s still in the early prototype stage, but it works.

I searched the arduino.cc forums and suddenly found a user who had made what looked like what I was trying to do. The thread was called pinball-like and racemaniac really helped me understand arduino coding. I have further developed the code to show score on a 8 digit 7-segment display. I got help from Ralph S Bacon after watching his tutorials on youtube.

Bill of materials

  • Arduino nano
  • 220 Ohm resistors
  • 4 LED
  • 4 buttons
  • MAX7219 LED Dot matrix 8-Digit display (Max7219)
  • jumper wires and a breadboard

I have set up a breadboard like the image below.

arduino pinball breadboard

Actual setup:

IMG_6604web

The arduino code is long, but here goes:

#define MAX7219_DIN 6  // set up for the display
#define MAX7219_CS  7
#define MAX7219_CLK 8

#define targetCount 4   // set up the number of targets in the target set

#define targetLed1 9
#define targetLed2 10
#define targetLed3 11
#define targetLed4 12

#define targetPin1 2
#define targetPin2 3
#define targetPin3 4
#define targetPin4 5

typedef struct target   
{                         
  byte detectPin;
  byte ledPin;
  byte hit;
};

uint16_t score;       //this is the variable to store the score

target targets[targetCount] = {{targetPin1,targetLed1, 0}
                              ,{targetPin2,targetLed2, 0}
                              ,{targetPin3,targetLed3, 0}
                              ,{targetPin4,targetLed4, 0}
};

  int last_a = 0; 
  int last_b = 0; 
  int last_c = 0; 
  int last_d = 0; 


// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(targetLed1, OUTPUT);
  pinMode(targetLed2, OUTPUT);
  pinMode(targetLed3, OUTPUT);
  pinMode(targetLed4, OUTPUT);
  pinMode(targetPin1, INPUT);
  pinMode(targetPin2, INPUT);
  pinMode(targetPin3, INPUT);
  pinMode(targetPin4, INPUT);

  //   Serial.begin(9600);
  //   Serial.println("Ready to begin!");
  
  initialiseMAX7219();
  output(0x0f, 0x00); //display test register - test mode off
  output(0x0c, 0x01); //shutdown register - normal operation
  output(0x0b, 0x07); //scan limit register - display digits 0 thru 7
  output(0x0a, 0x0f); //intensity register - max brightness
  output(0x09, 0xff); //decode mode register - CodeB decode all digits
  output(0x08, 0x0c); //digit 7 (leftmost digit) data
  output(0x07, 0x0b);
  output(0x06, 0x0d);
  output(0x05, 0x0e);
  output(0x04, 0x08);
  output(0x03, 0x07);
  output(0x02, 0x06);
  output(0x01, 0x05); //digit 0 (rightmost digit) data
}

// the loop function runs over and over again forever
void loop() 
{
  detectCollision();
  redrawTargets();
  checkAllTargets();
  displayScore(score);
}


//This shows the score variable on the 8-digit display
void displayScore(int score)
{
  //calculate number of digits
  String myString = String(score);
  int numberOfDigits = myString.length();
    //clear unused digits
    for (int i = 8; i > numberOfDigits; i--)
    {
      output(i, 0x0F);
    }
  
    //write digits to the display
    for (int i = 1; i <= numberOfDigits; i++)
    {
      output(i, myString[numberOfDigits-i]);
    }
} //end of score display

//This checks if targets are hit
void detectCollision()
{
  int a = digitalRead(targets[0].detectPin);
  int b = digitalRead(targets[1].detectPin);
  int c = digitalRead(targets[2].detectPin);
  int d = digitalRead(targets[3].detectPin);

      //Check if target1 is hit
       if(a != last_a){
          if (a == HIGH) {    // Switch is pressed
        }
        else {                // Switch is released and score added
          targets[0].hit = 1; 
          Serial.print("Traff A!"); Serial.print("New score: "); 
          Serial.print(score); Serial.print("!");Serial.println();
          score += 50;
        }
        delay(50);
        last_a = a;
        }
      //Check if target2 is hit        
       if(b != last_b){
          if (b == HIGH) {    // Switch is pressed
        }
        else {                // Switch is released and score added
          targets[1].hit = 1;
          Serial.print("Traff B!"); Serial.print("New score: "); 
          Serial.print(score); Serial.print("!");Serial.println();
          score += 50;
        }
        delay(50);
        last_b = b;
        }
      //Check if target3 is hit  
       if(c != last_c){
          if (c == HIGH) {   // Switch is pressed
        }
        else {               // Switch is released and score added
          targets[2].hit = 1;
          Serial.print("Traff C!"); Serial.print("New score: "); 
          Serial.print(score); Serial.print("!");Serial.println();
          score += 50;
        }
        delay(50);
        last_c = c;
        }  
      //Check if target4 is hit
       if(d != last_d){
          if (d == HIGH) {   // Switch is pressed
        }
        else {               // Switch is released and score added
          targets[3].hit = 1;
          Serial.print("Traff D!"); Serial.print("New score: "); 
          Serial.print(score); Serial.print("!");Serial.println();
          score += 50;
        }
        delay(50);
        last_d = d;
        }

}

void redrawTargets()
{
  for(int i = 0; i < targetCount; i++)
    digitalWrite(targets[i].ledPin, 1-targets[i].hit);
}

void dimTargets()
{
  for(int i = 0; i < targetCount; i++)
    digitalWrite(targets[i].ledPin, LOW);
}

void checkAllTargets()
{
  for(int i = 0; i < targetCount; i++)
    if(targets[i].hit == 0)
      return;
  score += 200;
    Serial.print("..and 200 Bonus! New score: "); 
    Serial.println(score); 
  for(int i = 0; i < targetCount; i++)
    targets[i].hit = 0;
  blinkTargets();
}

void blinkTargets()
{
    for(int i = 0; i < 5; i++)
  {
    redrawTargets();
    delay(200);
    dimTargets();
    delay(200);
  }
  redrawTargets();
}

void initialiseMAX7219()   //  MAX7219 7-Segment LED Display 
{
  digitalWrite(MAX7219_CS, HIGH);
  pinMode(MAX7219_DIN, OUTPUT);
  pinMode(MAX7219_CS, OUTPUT);
  pinMode(MAX7219_CLK, OUTPUT);
}
//common routine to talkt o MAX7219
void output(byte address, byte data)
{
  digitalWrite(MAX7219_CS, LOW);
  shiftOut(MAX7219_DIN, MAX7219_CLK, MSBFIRST, address);
  shiftOut(MAX7219_DIN, MAX7219_CLK, MSBFIRST, data);
  digitalWrite(MAX7219_CS, HIGH);
}

Please comment with all kinds of suggestions to improve!

My next step will be to build a functional prototype with actual targets using microswitches and test it with a pinball. But I’m still waiting for my pinballs to arrive in the mail…

 

Advertisements

2 thoughts on “Arduino Pinball – targets, breadboard and arduino code”

  1. Hi i try to use this code error or something missing. Could u please send to me working codes? Thank u very much

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s