A pretty good twilight zone topper

Twilight Zone, being one of the top pinball machines with collectors and having a very good theme, has led to it being heavily customized, some of them being functional, but most just being ornamental, to make the machine more fun.

One of the cooler ones was done by a guy who took a small hallmark ornament that looked like a TV, put a small LCD display in it, and then used it to display pictures from the TV series while you played. It’s tiny (maybe 1.5”), and it fits inside of the game. And then somebody else did one that showed video.

I was thinking that it would be cool to do something that was game controlled, which is the whole reason I built the WPC pinball lamp decoder. And, instead of making it a small one that would go on the playfield, I decided to make it slightly larger and make it as a topper.

A topper is simply something ornamental that goes on top of your pinball machine. They are almost always lighted in some way, and, with a few exceptions, not controlled by the machine.

Mine would be. Twilight Zone has a bunch of different modes, and my plan is to detect those modes (and other events) by decoding the light state, and use that to play *appropriate* clips on the topper.

I needed a good enclosure. And, thanks to Ebay, it was simple to procure:

This is an early Sony 5” TV, and it was a bit of a design coup for Sony; nobody was doing TVs this small at the time. I toyed with the idea of just using it as a display, but then I took a look inside:

That is one of 3 circuit boards. These TVs have a reputation for being very hard to work on, and I don’t what that sort of project, so we are going to use it as an attractive case instead…

I plan on using as many of the controls as possible; at least the on/off switch and the volume knob.

For a display, I need a small LCD. I found one that is designed for a rear-view camera. Here’s a test-fitting in the case:

Fits but barely on width, but the height is less than I would like (the display isn’t really 4:3). Hmm. If I put it behind the original CRT protector…

That will likely work better. Assuming I stay with this display (and I think it’s the biggest one that will fit without hacking out the control section on the right), I will laser-cut an oval trim plate out of black acrylic that will cover the gaps. I’ll also modify the lens to get the display more forward.

and one more photo of the display working…


A bit about pinball

You may or may not know that I’m a pinball afficianado. Yes, yes, “ever since I was a young boy, I played the silver ball…”

During the arcade boom of the early 1980s, arcades were everywhere, and while they generally focused on video games, there were always a few pins. I spent lots of time and money on both, but pinball had more of a fascination, for three very important reasons:

  1. If you are decent on one pinball machine, you are decent on all of them. This is very much not the case on arcade machines; your skills on Defender help you when you play Tron, but you aren’t automatically decent.
  2. The physicality is much better. You get to shake the machine. No, you are *required* to subtly nudge and shake the machine while you play if you are going to be a good player. You work on your advanced moves. And you enter a society of players who know how to play the right way. It’s a bit of a craft.
  3. You can win free games. Plus, the difficulty level on a pin is mostly linear; yes, it becomes a *little* harder on most games as your points go up, but it’s not a lot harder. What that means is that once you are good, it becomes easier to get high scores. And, every once in a while, probability and skill will align, you will walk into an arcade, put one quarter into the pin, and play for half an hour, with the game knocking to announce your skill when you win a free game. And then, after that time, you will turn to the 12-year-old kid who has been watching you play and say, “I have to leave. Do you want my games?”

So, anyway, I played a bunch of pinball in my young days, but it tapered off when I got older because pins and vids became much harder to find. You could find them in bars, but I don’t do well with cigarette smoke, so I didn’t play as much as I used to.

Then, sometime in my 30s, I realized that a) it was possible to own a pinball machine, and b) I could afford to do so. So, I craftily bought a Williams Bad Cats:

The reason it was crafty is that I bought it because a) it was a relatively inexpensive machine (I think I paid $800 + shipping), and b) it was my wife’s favorite machine.

I reconditioned it, played it for a while, but there was still a problem.

I wanted a Twilight Zone…

Twilight zone is complex machine. A really complex machine, with ramps, ramp diverters, a working pinball machine, a magnetic mini-playfield, and a impressively complex ruleset. Oh, and it has a lightweight ceramic “powerball” that moves really fast, two different 3-ball multiballs, and – if you are worthy – “Lost in the Zone”, a 6-ball timed multiball mode.

In fact, it was so complex that it didn’t really do well commercially; novice players found it too challenging and confusing.

But skilled players loved it, and made it a hot commodity in the resale market. I was lucky/smart enough to buy mine around 10 years ago, when machines are a bit more plentiful, and paid around $3500 + shipping to get it. These days you will probably play twice that.


Pinball Lamp Matrix Decoding–Completed board

I got the boards back from OSHPark a while back, and got around to populating them. Though I have access to a real reflow oven, I’ve done boards like this with the heat gun method in the past, so I got out the solder paste, put it on the solder pads, put on the shift registers, and then went out to the garage.

I use an old license plate as a board holder, so the board goes on that.

Real reflow ovens use a time/temperature calibrated curve.

Basically, there’s a long soaking stage to gradually bring all the parts up to temp, a ramp up until the solder melts, and then a cooling-down period. I attempt to duplicate this with the heat gun, though it’s really not very precise.

The hard part with the heat gun is keeping it at a distance where it does not blow the parts around on the board; as the board heats up the flux will start to flow and the chips want to move. A little repositioning with a toothpick fixes that. You keep heating until you see the solder reflow, make sure it’s reflowed around all the parts, and then remove the heat.

And you get this:

The 34-pin connector and the two test point connections were hand soldered. I do need to test the board itself and then hook it up to the pin and see if it works.


Pinball Lamp Matrix Decoding–Board design

Since I got the prototype working, I now need to convert it to something real. In the past, I’ve typically done this by hand on perfboard. This takes a lot of time to do, and the results are functional but not that great.

This time I’ve decided to go direct to a PC board, so I fired up Eagle. I think this is my third design with it, so I’m reasonably decent at it, but I’ve forgotten how strange CAD user interfaces always are; they were developed before much was known about how to build good graphical user interfaces, they are all different, and they don’t follow UI guidelines. Oh, and they tend to have some weird metaphors as well, and there is no equivalent of github or stackexchange for them.

I installed the Adafruit library by downloading it, unzipping it, copying it to a directory and then adding that directory to the library path (nuget, anybody?) so that I could start with their arduino shield outline. It has all the pins on it. I then searched for the 74HC589 shift registers. No luck, so I stated to design my own, went back, did some more browsing, and found the 74xx589 SOIC-16 design, so I could use that. I need a 34-pix (2×17) connector header; I found a 2×20 and modified it. Then, I built a new version with named pins to make it easier to wire things up.

At that point, I started the wiring. A bunch of work, an “invoke” to get the Vcc and Gnd to show on the shift registers (once again, CAD weirdness), and I ended up with the following schematic.

Basically, we have the 8 data lines and two strobe lines going from the connector to the shift registers, the two strobes heading to Arduino (pins 2 and 3 because they are the ones that support interrupts), and then a bunch of lines between the Arduino and the shift registers. Prettiness isn’t important here, though it needs to be neat enough to know whether it’s correct or not.

I also threw on two test point headers; one for ground and one connected to pin 4. I often use a pin to write debug information for an oscilliscope, so the headers give me an easy way to hook that up. I run the schematic rules engine, fix a few things, and the schematic is done.

Once you have the schematic in good shape, you can create a board to implement it. I should have taken a snapshot at the beginning because it was quite the mess; there are 8 data lines that need to make it to both shift registers, 3 shared lines from the arduino to teach shift register, plus Vcc & Gnd. Lots of overlap potential.

I like doing board layout; it starts out looking pretty hopeless, and you play a little, it gets better, and then finally it sort of jells and then you are doing cleanup. Here’s the board layout I ended up with:

Lots of left-right rights to route all the signals that go to both chips. I’m pretty happy with this layout; it’s reasonably ordered, and you can tell where things go. I run one line right under the pins of the shift registers; something that I couldn’t do if they were through-hole chips.

Two things I could have done; I could have switched pins 2 and 3, and I could have pulled one of the signals off of pin 11 and used pin 7 instead. I didn’t because I would need to redo the software; a small thing, but the current layout works fine.

Once the layout is done, I run through the design rules. It’s really easy to end up with weird stuff; multiple vias slightly overlapping, traces that don’t quite go to pins, that sort of thing. I’m planning on fabbing the board at OSHPark.com, and I remember that they have their own design rules, so I download them and run them.

Then the last task is to shrink the design down from full shield size to something smaller to save a bit of money. Oh, and I add a label or two. Upload it to OSHPark, and they tell me $20 for 3 boards. Pretty good.


Decoding Pinball Bus Signals Part 5

In the last episode, I spend time figuring out how to detect and respond to the row and column strobes. Here’s the short list of tasks:

  1. Hook up the shift registers to the arduino and write some code to make sure that I knew how to drive them.
  2. Write some timer code to handle the delay after the row and column strobes.
  3. Test the shift register code while connected to the pinball machine
  4. Integrated in the lamp matrix code that I wrote separately.
  5. Figure out how to get the lamp state out of the arduino through the serial port.

It’s about to get real

Shift Registers

I’ll start by seeing if I can read static values out of the shift registers.

I slightly modified a 34-bit connector so I could plug it into my breadboard, and then plugged in the two shift registers underneath. I pulled out my breadboarding wires from Adafruit, and started wiring things up. The basic setup is like this:

  • The 8 data lines are connected to the data lines on the connector.
  • The RCK line is connected to the strobe line from the pinball. When the pin pulls this line low, the shift register will pull data in and latch it.
  • Vcc and GND are connected.
  • Output enable (OE) is grounded to enable the serial output.
  • SCK is hooked to the arduino. This will be driven by the arduino to shift the data out.
  • SLOAD is hooked to the arduino. We will pulse it low/high to move the data from the latch into the shift register.
  • SER (shift input) is grounded. This is used to gang together more than one shift register, but it’s not needed for what I’m doing.
  • QH  is the serial output and will be hooked to the arduino.

I spent a lot of time studying both the truth table and the timing diagrams in the data sheet, and came up with the following sequence:

  1. Pulse SLOAD low
  2. Get the current bit
  3. Pulse SCK low to get the next bit
  4. repeat steps 2-3 until you have 8 bits

I set up a loop that would do this continuously, wired up each data lines to Vcc or GND, and started it running. Success! I got a value out that looked like the way I had the lines wired. And then, I rewired the data lines, and nothing changed. After an embarrassingly long period of debugging, I realized that since I was running not connected to the pinball machine, there was nothing driving the data strobe (RCK) input. I modified the code to pulse RCK low/high, and it started returning real data.

Now, to check the speed.

I set up a pin to be a debug output, drove it high at the beginning of the routine, and low at the end. I could then hook it up to my oscilloscope and figure out how fast my data rate was (I could have used the logic analyzer but it was in the other room connected to pinball machine and the scope was handy). The period was about 0.2 mS, which makes the frequency about 5000 Hz. I would need to do this twice, which would put me at 2500 Hz. That gives me little time to do the rest of the work that I need to do, and seems pretty slow.

So, I did some investigation, and rediscovered that the arduino digitalWrite functions do a lot of work to protect you, but this work makes it very slow – on the order of 150 clock cycles to do a single call. I found a nice set of replacement macros online, and recoded things, and got the full read down to about 10 uS, which should be more than fast enough. Here’s the code:

byte readFromSerial()
{
  byte result = 0;
 
   // pulse SLOAD LH to move data from latches into shift register
  digitalPulseLowHigh(C_SLOAD);

  for (int i = 0; i < 8; i++)
  {
    result = (result << 1)  + digitalState(C_QH);
   
    // pulse clock LH to move to next bit
    digitalPulseLowHigh(C_SCK);
  } 
 
  return result;
}

Another option would be to use SPI to read the data. I didn’t explore that because the bit-banged approach is fast enough and I might need SPI for other communication later.

That gave me one shift register working, so I started on the second one. My original idea was to drive both the shift registers separately, using 6 wires:

Shift register 1

SLOAD1
SCK1
OUT1 (aka Qh)

Shift register 2

SLOAD2
SCK2
OUT2

I have plenty of free pins right now, and this would work just fine, but the speed of the shift register code depends on a fixed set of pins; I can’t pass in a parameter to use. I could easily duplicate the code, but instead I decided to use the output enable feature and share most of the signals. This allowed me to share signals using only 5 wires.

Shift register 1

SLOAD
SCK
OUT (aka Qh)
OE1

Shift register 2

SLOAD
SCK
OUT
OE2

The first 3 signals are in parallel. I choose between the two shift registers by carefully setting OE1 or OE2, putting one shift register into output mode and the other into high-Z mode. This also requires some care in the setup code; I set both OE pins to high before I turn them into outputs so that the outputs will not fight with each other.

At this point, I have code to read both shift registers working. Yea!

Delay code

The problem that I have to address is the strange behavior of the row and column strobes. Sometimes they show up row, column, sometimes they show up row, column, column, row. Which means that I can’t just base it on the row or column strobe. What I came up with is the following:

void handleDataStrobe()
{
  TCNT2=TIMER2_START_VALUE;
  TCCR2B=0x02;
}

void rowDataHandler()
{
  handleDataStrobe();
}

void columnDataHandler()
{
  handleDataStrobe();
}

When either the row or column strobe come in, we set the value of the timer2 count register and set the timer so it’s counting. If another strobe comes it, it merely resets the timer value, so we keep counting. Eventually, we hit the last strobe, the timer overflows, and our interrupt service routine gets called:

ISR(TIMER2_OVF_vect)          // timer compare interrupt service routine
{
  TCCR2B=0x00;
 
  readShiftRegistersAndDecode();
}

Setting TCCR2B to 0x turns off the timer counting, so we only get the interrupt once.

After a bit of tuning, it’s set up and working:

Channel 0 and 1 are the row and column strobes. Channel 2 and 3 are for debugging; channel 2 goes high when the first interrupt handler is called, and then pulses low when the timer expires. Channel 3 is the SCLK channel, and you can see (well, you could see if I zoomed in) two sets of 8 clock cycles to shift the data out.

Here’s the setup. I think that qualifies as the most complicated breadboard I’ve done; there are two 16-pin shift registers, 12 pins from the connector, and then 7 pins out to the arduino.

Testing the shift register for real

Now that I can read data – theoretically – I want to track what data the machine is sending over the bus. The simplest thing to do would be to just add a call to Serial.println() after the data is read.

But the serial port is *slow*. Too slow to be able to capture sequential values of data.  Instead, I allocate 128 bytes for the row and column, capture that many chunks of data, and turn off the capture while the main loop writes the data out the serial port. And then repeat.

I build the code, download, and I start getting data. Lots of data, since the pinball is in attract mode (lots of flashing lights), and that’s not what I need. I flip the machine into single lamp test mode, capture data, and see the following:

      Row       Column
11111110 11111110
11111111 11111101
11111111 11111011
11111111 11110111
11111111 11101111
11111111 11011111
11111111 10111111
11111111 01111111

That is precisely what I had hoped for. The system is walking sequentially through all of the bits in the column byte, and it is setting the row byte values appropriately. It’s not quite as simple as this, because the lamp is flashing on and off, but it’s close.

I did note that the C# code that I wrote has the bitness wrong for the columns, so I went back and fixed it so that both are enabled with a zero bit.

Integrate the lamp matrix code

Finally, I can integrate the C# code I wrote. I do a quick transform into C++ code, and hook it up. I run into a couple of issues, but it mostly goes clean. But the data has a bunch of junk in it; it doesn’t look like the data I was getting before. Thirty minutes yields no clues until I look up and realize the the weight of the Arduino and analyzer had pulled the connector out of the breadboard. A bit of strategically-deployed blue tape holds it in place, and the data comes out fine.

Okay, except for one thing; it’s in row-major form rather than column-major form, but that’s a quick change.

Communicating the state out

To correctly identify the state of the lights, I need to be able to detect three states:

  • Off
  • On
  • Flashing

The way I do this is to allocate a byte for each lamp, and then decode 128 separate chunks of data. Since it takes 8 chunks to cover the whole matrix, this gives us 16 samples for each lamp. That gives us a zero if the lamp is off, a 16 if the lamp is on, and something in between if it is flashing.

To communicate it out we create a 64-character string by adding the value for each lamp to the character ‘0’, so our range is 0123456789:;<=>?@  (I considered a modified hex with 0-G, but that would be a little harder and slower to create), and then send the values out the serial port.

But, as usual, it’s a wee bit more complicated. I don’t have time in the interrupt handler to send that whole string over the serial port, so instead I copy a snapshot of the string and have the main loop write it across the serial port.

And, at this point, this part of the project is all working; I get consistent strings that give me the state of all the bulbs in the machine.

Next steps

Now, I need to convert it from a mess of spaghetti wiring into something better. I have a few options:

  1. A hand-wired Arduino shield
  2. A custom pc board arduino shield
  3. A “Pinduino” design that takes the Arduino Uno reference design, deletes what I don’t need, and adds in the connector and the shift register

#1 and #2 seems the easiest. I might do #3 at some point.

Oh, and there’s the next section, the whole reason I built this thing. But that’s the subject for another post.


Decoding Pinball Bus Signals Part 4

After spending a few weeks working on the software, it’s time to get back to the hardware. My first task is to hook up the arduino and see if I can reliably trigger off the interrupts.

The answer to that question is yes; setting the trigger on “FALLING” works reliably. But I came across the following behavior that is a big puzzling:

These are the row and column strobes. On the left, you can see the top strobe happens first, and then the bottom strobe. If I zoom in, I can see that they occur when data is present on the bus (bus signals not shown). Just as it should be.

But what is going on with the signals on the right? Here’s a bigger picture:

This is weird; first we see a column/row strobe with some crappy looking bus data, and then a row/column strobe with some good looking bus data. This would seem to indicate that part of the time, the CPU sends data and then sends different data soon after, and other times it just sends the data. The hardware in the machine won’t care; the time that the first data is present is so short that it should not affect the behavior of the lights in a noticeable way.

But is this what is really going on, or is it a glitch in my logic analyzer? Time to find out…

BTW, the big square wave in the middle is an output from the Arduino. An interrupt service routine is hooked up to the bottom signal on the trace, and the lag between the negative edge on the strobe and the positive edge on the arduino trace is the time it takes the interrupt to happen + the time it takes to turn on a pin. So, it appear the decision not to do this purely in software on the arduino is a good one.

To figure out what is going on, I wrote an arduino program:

void setup() {
  pinMode(0, INPUT);
  pinMode(1, INPUT);
  
  attachInterrupt(0, rowDataHandler, FALLING);      // pin 2
  attachInterrupt(1, columnDataHandler, FALLING);   // pin 3
 
  Serial.begin(9600);
}

unsigned long startTime = 0;
const int ArraySize = 128;
unsigned long rowTimes[ArraySize];
unsigned long columnTimes[ArraySize];
int rowIndex = 0;
int columnIndex = 0;

void rowDataHandler()
{
  digitalWrite(8, HIGH);
  if (rowIndex < ArraySize)
  {
    rowTimes[rowIndex] = micros();
    rowIndex++;
  }
  digitalWrite(8, LOW);
}

void columnDataHandler()
{
  digitalWrite(9, LOW);
  if (columnIndex < ArraySize)
  {
    columnTimes[columnIndex] = micros();
    columnIndex++;
  }
  digitalWrite(9, LOW);
}

// the loop function runs over and over again forever
void loop() {
 
  while (rowIndex < ArraySize && columnIndex < ArraySize)
  {
    Serial.print(".");
  }
  Serial.println();

  Serial.println("Times");
  for (int i = 0; i < ArraySize; i++)
  {
    Serial.print(rowTimes[i] – startTime);
    Serial.print(",");
    Serial.println(columnTimes[i] – startTime);
  }

  Serial.println();

  startTime = micros();
  rowIndex = 0;
  columnIndex = 0;
}

Basically, it sets up interrupt handlers for both row and column strobes and then starts saving the time when the interrupt occurs. When it has collected 128 of each, it stops collecting them, and the main loop sends them out over the serial port. Then the process repeats itself.

I took the data and pulled it into Excel for analysis. This is the delta time between interrupts, all times are in microseconds.

Row Delta Col Delta
   
2064 2112
44 44
1992 1992
44 44
2048 2044
44 44
1952 1956
44 44
2000 2000
44 44
2008 2008
44 44
2000 1996
44 44
1996 2000
44 44
2000 1956
2048 2048
2056 2056
2052 2096
44 44
1988 1984
44 44
2008 2008
44 44
1996 1996
44 44
2000 2000
44 44
2000 2004

There are times with a double interrupt, and other times that only have a single. Sometimes the row interrupt comes first, sometimes the column one comes. The period is right around 2 mS, so the frequency is pretty close to 500 Hz; exactly what I had measured before. The time period between the first one and the second is either 44 or 48 uS; I think the difference depends on the ordering.

The ratio of the two time periods is 44/2000, or just about 2%. Based on my extensive experience dimming lights by microcontroller, I can tell you that 2% isn’t really a noticeable difference in how a light looks; you *might* be able to detect the difference between 98% and 100% brightness in a controlled setting, but it would be hard to do. I’m confident that you can’t tell the difference between 0% and 2%, because on a incandescent light, 2% is just off.

I did a little graph in Excel that looked pretty much exactly like the logic analyzer image, so I won’t share it here.

 

How to deal with this? Well, I think it will actually turn out to be pretty simple. Instead of doing work directly off of the row or column interrupts, I will set up a short timer interrupt (let’s say 100 uS or so) whenever the row/column interrupt fires. That will be updated by any new strobes that occur before the timer expires, so the effect will be that the timer interrupt will be serviced a short time after the last row/column strobe.


Embedded development, unit testing, and Port/Adapter/Simulator

I’ve been working on a project to decode pinball bus signals to figure out the current state of the lights on the machine.

Why I am doing that is still shrouded in mystery, but I’m to the point where I have the hardware ready to hook together for prototyping purposes. At this point, I would generally start writing code, but this is a bit more sophisticated than what I usually build – I have shift registers to read through serial, some decoding logic to write, and I need to detect whether a light is on, off, or flashing.

So, taking a page from my day job, I decided to write the code using TDD. One of the advantages at TDD is that it’s good at making progress if you aren’t quite sure what you need; you start building things that you need, and pretty soon you’re done.

Since I knew that I needed to be able to read serial data out of a the shift registers and convert it into bytes, I started writing a SerialToByte converter. And, I decided to write it in my fastest TDD language – C#.  My plan is to write the code – using as few non-C++ features as possible – get it all working, and then convert it to C++ code for the Arduino.

Once I had that, I decided to write the class that would talk with the 74HC595 shift register. Conceptually, the operation that I need to do is fairly simple:

  1. When new data is available (how will I know this?)
  2. Flip the shift register from parallel to serial mode (it’s just setting a pin to low).
  3. Read a bit
  4. Clock to the next bit
  5. repeat until 8 bits are read
  6. Flip the shift register back to serial mode

There is a problem, however; to talk to the shift register I need to send electrical signals to the hardware, which is something I can’t do until I’m writing code on the arduino and choosing pin assignments. Or can I?

The situation – dealing with external dependencies – is a common problem when using TDD, and there’s a great pattern to deal with it, which is described at length in my post: Port/Adapter/Simulator. Instead of building code that talks directly to the hardware, I can define an abstraction on top of the serial register. I’ll build a simulator that I can use for test purposes, and then a real version when I move the code to the arduino.

Here’s the abstraction (aka “port”):

interface IShiftRegister
{
byte GetBit();
void SetParallelMode(byte parallelMode);
void SetSerialClock(byte serialClock);
}

To test the code that uses this abstraction, I built a simulator (using TDD), along with a full set of unit tests for the simulator:

public class ShiftRegisterSimulator : IShiftRegister
{
public void SimulateParallelLoad(byte value) {}

public byte GetBit() {}
public void SetSerialClock(byte serialClock) {…}
public void SetParallelMode(byte parallelMode) {…}
}

The simulator implements the 3 methods in the port, and also one additional method to put values into the shift register. I also write a DataStrobeSimulator that lets me simulate an interrupt, and then I can write a test for the BusDataConverter class:

[Test]
public void when_strobe_with_a_medium_value__it_has_the_correct_value()
{
DataStrobeSimulator dataStrobe = new DataStrobeSimulator();
ShiftRegisterSimulator shiftRegister = new ShiftRegisterSimulator();

BusDataConverter busDataConverter =
new BusDataConverter(dataStrobe, shiftRegister);

shiftRegister.SetParallelMode(1);
shiftRegister.SimulateParallelLoad(179);
dataStrobe.SimulateStrobe();

Assert.AreEqual(179, busDataConverter.GetValue());
}

This test sets up the shift register simulator with a  value, simulates the strobe, and then verifies that the BusDataConverter correctly reads the data from the shift register.

So far, this approach has worked well; I’m confident that the code is pretty close to what I will need.


Decoding Pinball Bus Signals Part 3

A trip to Vetco yield three 34-pin IDC connectors and some ribbon cable. I built a nice & short cable, hooked it up, and the lights worked normally. So that is good.

The signals from the logic analyzer looked a little better, and I was able to make some measurements.

All the pulses are negative, which is expected because TTL is much better at sinking current than sourcing current.

The pulse width is 360nS. If that is one clock-tick wide, that means things are running at 2.78 MHz, which is pretty much as expected. The delay between the row/column strobes is about 2mS.

Let’s do some math:

At 16 MHz, the clock cycle time is 62.5 nS, which means I would have less than 6 clock cycles to respond to the interrupt and read the data values. My online reading has showed that the AVR needs at least 8 cycles just to *start* executing my interrupt vector, which means using interrupts to read the data directly isn’t going to work.

At which point we step back and analyze the options.

  1. Go with a different microcontroller. An arm-based solution running at 100+ MHz would probably work.
  2. They make buffer chips for a reason – just use them, and things will be great.
  3. Use polling

#1 sounds more complicated than I want to get right now. #2 would obviously work, at the cost of some additional external circuitry. My first reaction to #3 is “yuck yuck yuck”, because polling is rarely a clean solution, and it would probably be a bitch to get my logic plus I2C/ USB to work at the same time as polling. However, if I was willing to burn an extra AVR, I could probably build the bus-reading part in one and then use another one for the process & communication. Still yuck.

So, it looks like buffer chips are the simplest part. The driver board uses a 74LS374, which is an octal D-type positive edge-triggered flip-flop. When the clock signal goes from low->high (ie at the end of the strobe pulse), it grabs the current values on the inputs and saves them into the outputs, which could be read at our leisure.

So, just put one of these in-line with the data, hook it up to the strobe, grab an interrupt from the strobe, and then the data will be ready and waiting when we need it, right?

Well, that will work, but I need to get both row and column data out. If I use two buffers, that uses 16 input pins, plus two for the strobe interrupts, plus two for communication, which means 20 pins total. Which is precisely the number of pins on an arduino, so it will work, but with with no room for bells and whistles, which I much confess a certain fondness for.

If I come up with a way to combine the strobes, I can use a single buffer chip. What I want sounds like an OR operation, but remembering that the strobes trigger negative and spending a few moments with truth tables, it turns out what I really want is an AND; it will be high when both inputs are high, and will go low when either input is low.

And… that’s not going to problematic. The flip-flop is triggered on the rising edge which is right at the end of of the pulse. If we put a gate in front of it, we add a propagation delay of something like 20-50 nS, and we might miss the data.

Is there an alternative? Well, yes, there is. The 374 has an output enable input. Pull it low, and the chip will drive the outputs. Drive it high, and it disconnects the outputs. That will let us parallel two of the 374s and enable the one that we want from the Arduino. Works, but that is going to be a whole bunch of data lines and is going to make the circuit board layout interesting.

So, that’s where I am right now, but I’ve asked a few people if there are better alternatives.

There were a number of different faster microcontroller suggestions. There are a few that look workable – and I am professional at software and amateur at hardware – but I’m not excited about spinning up on a whole new toolchain.

On the hardware side, it turns out there are some better choices. A couple people suggested a 74HC165 as a nice choice, as it lets you read the data out serially. Slower, but with 7 less lines and the PCB routing in my head was already getting pretty bad. But… that chip both clocks in and clocks out on a master clock, which doesn’t work for what I need.

Enter the 74HC595, which has separate clocks for the store and read operations. It’s a tiny bit inconvenient in that it’s modal – you have to control whether it’s in “load in parallel on load clock” and “read out serially on read clock”, but it’s still simpler. I can only find it in SOIC, so I ordered some adapters from adafruit to go along with it. That, and some nice connectors for the ribbon cable. BTW, that thing that you put on the end of a ribbon cable is not called a “connector”, it’s called a “female header”. I have the usual digikey exasperation until I figured that out.

When that all shows up, I can breadboard the hardware, and then start on the software. The matrix decoding seems pretty straightforward to implement, but dealing with flashing lights looks to be challenging. While I could just send out a state change whenever a light turns on or off, it would be more convenient if I could just say that a specific light was on, off, or flashing. I have to do flash detection somewhere because I need to detect the difference between flashing and on for some things I want to do.


Decoding Pinball Bus Signals Part 2

The connection from the CPU board to the driver board uses 34 pins, in a standard 17×2 format. In the machine, it’s connected together by a short (4” or so) ribbon cable withi IDC (insulation displacement) connectors. If you were around when IDE drives were common, you’ve dealt with these connectors.

Look through my old stuff, I couldn’t find any cables that had 34 pins, but I did find an old 40-pin IDE drive cable with 3 connectors. I taped off one end the connectors, looked carefully at the schematics for the board, and wired up the logic analyzer to the third connector.

Then is was a simple matter to unlock the pin, remove the backglass, pull off the existing connector, and hook up the new cables. Turn on the pin, it boots up fine, and… none of the lights work. turn it off, pull the cable, replug it, turn it on, no lights. Put the factory cable back on, things work fine. My cable, no lights.

Not really sure why it didn’t work, but I was able to get a signal off of the analyzer. Sort of…

Hmm. What do we have?

Well, let’s start with the good news at the bottom. Channels 8 and 9 are the row and column strobes, and they are putting out nice signals, ones that should be easy to grab with the Arduino.

Channels 0-7 are supposed to be the data. What we see are really thin pulses (they do have some width to them, but not much) on some of the channels, and nothing on the rest of them. What’s going on? Well, I’d like to stress that I don’t know. What I do know is that the boards are built using TTL logic, and TTL inputs have a relatively low impedance, which limits the number of inputs you can drive with a single output (this is known as “fan out”). The data lines that I’m trying to read go to multiple boards, so my guess – well, actually, it’s more of a hope than a guess – is that the load of the analyzer plus not having the lines well terminated is causing the issue.

So, it’s off to Vetco to pick up some real 34-pin connectors and ribbon cable. I’ll make up a nice short cable, and we can see how that works with the analyzer.

If that doesn’t work, we’ll try the Arduino directly, which has nice high-impedance inputs, so it should put minimal load on the bus.


Fixing your cyclist and programmer back…

If you have a desk job, you spend a lot of time, sitting. This is bad for your back, and if you’re a cyclist, it isn’t helping you. If your back hurts, this is the post for you.

I’ve spend the last 6 months or so working on mine, and I thought I’d share what I’ve been doing. Note that this can help your cycling as well.

First off, you’ll want to fix your hip alignment. Most people have either an anterior or posterior pelvic tilt. This is the best article that I’ve found that talks about it and tells you which muscles you need to stretch and which ones you need to strengthen. My PT helped me with it but this article gave me a lot of backing. I had a fairly serious anterior pelvic tilt (also known as “hyperlordosis”).

I would also recommend reading this and this.

My recommendation is to start with those exercises and do them for at least a few weeks before you go any farther. You will have to find which ones work for you and which ones you can tolerate without pain.

Now, on to the back. This is about both posture and strengthening; cycling puts us the “arms forward / rounded” posture. There is some information in the articles I already listed, but here are a few other things that worked for me:

· Pectoral muscle stretches. Working on these every day makes a significant difference at keeping my shoulders back.

· Farmer’s walk. In this exercise, you have a weight in each hand, and you walk around. While you are doing this, you should hold your should back and your shoulder blades together. This is a great exercise. I recommend starting with something like 25 lbs in each hand (I use weight plates).

· Suitcase carry. Like the farmer’s walk, but harder because it’s asymmetric. Same style cues; keep the shoulders back.

On to the deadlift. I’ve come to believe that big functional motions like the deadlift are very useful, but you need to have good form and start slowly. I like the stronglifts 5×5 approach, and I recommend that, regardless of gender, you start out just using the bar. If your gym is good, it will have what are called “bumper plates”; they are round plates that are the size of 45s but are much lighter (generally 5lbs or 10lbs is the smallest size). You use these so that the bar is at the same height regardless of how much weight you use.

The best video I know of is “How to deadlift”. The big cue is to pull yourself down to the bar; this flattens your back and puts you in a good position.

Other stuff to do:

  • I try to get to a circuit training class once a week. They typically have a variety of core & strength moves.
  • I’ve also used Cyclo-Zen in the past; it has some good back and core workouts.

Pages:1234567...20