Pinball

Pinball

Posted on Updated on

A project that I have started few months ago.

Basically, everything is working. Using Pinball fx2 for the games.

Link to vector artwork

Link to 3D of cabinet

Link to control

Time to keep up with the project… It has been sleeping forĀ  while.

What’s coming next:

Backglass with DMD and screen for image. Nudge control with accelerometer (basic setting). A bit of lighting…

DMD: Setting of Pin2Dmd.

I use a STM32F4 discovery board, P4 1/16 led matrix (cheaper than P2.5). I spent about a day to make this working; I could not get the board working with the last firmware (2.42). The communication with the PC was not responding. I then downgraded to 2.38 and all was fine excepted an image not sharp (kind of glitching or ghosting). I found out that I needed a bit more of 3.3 V to control the P4 because of the bus-driver of the P4, 74HC245, the HC version requires more than the HCT version. This is something that I will add on shield board for final connection. I decreased the supply of the P4 of few mV (final supply 4.7V).

Everything works fine now. I just need to register to get a key from PIN2DMD.

pin2dmd

Next, a bit of light show for the backglass. To make the thing simple, I tested something like Ambilight. It extends the sides color of the screen to led stripes. Simple and okay to make some animation. For testing, I used the software AMBIBOX (direct download) and the following arduino code (need FastLED Library)

#include "FastLED.h"
#define NUM_LEDS 24
#define DATA_PIN 3
#define serialRate 115200
static const uint8_t prefix[] = {'A', 'd', 'a'};

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2811, DATA_PIN, RBG>(leds, NUM_LEDS);
  Serial.begin(serialRate);
  Serial.print("Ada\n");
}

void loop() {
  for (int i = 0; i < sizeof(prefix); ++i) {
    while (!Serial.available());
    if (prefix[i] != Serial.read())
      return;
  }
  while (Serial.available() < 3);
  int highByte = Serial.read();
  int lowByte  = Serial.read();
  int checksum = Serial.read();
  if (checksum != (highByte ^ lowByte ^ 0x55)) {
    return;
  }

  uint16_t ledCount = ((highByte & 0x00FF) << 8 | (lowByte & 0x00FF) ) + 1;
  if (ledCount > NUM_LEDS) {
    ledCount = NUM_LEDS;
  }

  for (int i = 0; i < ledCount; i++) {
    while (Serial.available() < 3);
    leds[i].r = Serial.read();
    leds[i].g = Serial.read();
    leds[i].b = Serial.read();
  }
  FastLED.show();
}

 

Nudge control:

I use my previous Arduino due arcade control and added a bit of code for the accelerometer (ADXL345) reading. It is in testing now not 100% okay.

#include 

#include 
#include 
#include 

//change debounce time to 1ms
//will add led control

#define DEBOUNCE 1                                                      // how many ms to debounce

//define the buttons that we'll use.
byte buttons[] = {22, 23, 24, 25, 26, 27, 28, 29};
byte out[] = {44, 45};
byte key[] = {129, 133, 176, 218, 217, 216, 215, 177};                  //left shift, right shift, return, Up Arrow, Down Arrow, Left Arrow, Right Arrow, Esc


//128 left ctrl
//132 right ctrl
//32 space


// need to add up down left right esc

//determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
#define NUMOUTS sizeof(out)

//track if a button is just pressed, just released, or 'currently pressed'
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS];
float x_before;
float y_before;
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void setup() {
  byte i;

  /* Initialize the sensor */
  if (!accel.begin())
  {
    while (1);
  }


  Keyboard.begin();

  for (i = 0; i < NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);

    digitalWrite(buttons[i], HIGH);

  }

  for (i = 0; i < NUMOUTS; i++) {
    pinMode(out[i], OUTPUT);
    digitalWrite(out[i], LOW);

  }

  accel.setRange(ADXL345_RANGE_2_G);
  accel.setDataRate(ADXL345_DATARATE_3200_HZ);

  sensors_event_t event;
  accel.getEvent(&event);

  x_before = event.acceleration.x;
  y_before = event.acceleration.y;


}

void loop() {

  float x_now, y_now;
  sensors_event_t event;
  accel.getEvent(&event);
  x_now = x_before - event.acceleration.x;
  y_now = y_before - event.acceleration.y;
  if (abs(x_now) > abs(y_now)) {

    if ((x_now) > 1.5) {

      Keyboard.press(32);
      delay(50);
      Keyboard.release(32);
    }
  } else
  {

    if ((y_now) > 2) {
      Keyboard.press(128);
      delay(50);
      Keyboard.release(128);
    }
    if ((y_now) < -2) {
      Keyboard.press(132);
      delay(50);
      Keyboard.release(132);
    }
  }

  check_switches();

  for (byte i = 0; i < NUMBUTTONS; i++) {

    if (justpressed[i]) {

      Keyboard.press(key[i]);

    }

    if (justreleased[i]) {

      Keyboard.release(key[i]);
    }

  }
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;
  if (millis() < lasttime) {
    // we wrapped around, lets just try again
    lasttime = millis();
  }
  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
    return;
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();
  for (index = 0; index < NUMBUTTONS; index++) {
    justpressed[index] = 0; //when we start, we clear out the "just" indicators
    justreleased[index] = 0;
    currentstate[index] = digitalRead(buttons[index]); //read the button

    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
        // just pressed
        justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
        justreleased[index] = 1; // just released
      }
      pressed[index] = !currentstate[index]; //remember, digital HIGH means NOT pressed
    }
    previousstate[index] = currentstate[index]; //keep a running tally of the buttons
  }
}

 

Backglass work in progress.

Advertisements