Jul 17, 2014

2012 S4 Review

So, a few weeks ago, I traded in my 2008 328xi for a 2012 Audi S4!  I wrote up a review on /r/cars but I thought I'd share it here too.  So far, I'm wildly impressed with it!

About the Car

2012 S4 on Edmunds. Mine is a Premium Plus with a handful of options: MMI+ with Navigation, B&O Sound System, Sports Diff, and Audi Drive Select. It is equipped with the 7-speed S-Tronic dual-clutch transmission. It is Misano Red (a non-standard MY color, very similar to Brilliant Red) with Black Leather/Lunar Silver alcantara interior.

The Options, Notables, and Creature Comforts

So, the car is a 2012 Premium Plus, with a few select (and important) options. I do miss some creature comforts that I've previously had, such as adaptive headlights and keyless entry/start. On the walk up to the car, these are minor irks, but from the moment the gauges sweep and the engine comes to life, I forget all about the fact that I had to use the keyfob to unlock the car.

Initially, I was looking at 2013 S4's. Quite a few of the facelift updates were attractive to me - the new grille and front bumper, headlights (specifically the DRLs), new MMI interface, and flat-bottom steering wheel all were important at one point. However, for the price I paid for it, I really don't miss them (the difference would have been around $8,000).

A big surprise when I drove off the lot was the lack of Bluetooth audio streaming in the MMI+, despite having BT phone connectivity. This seems to have become standard as part of the 2013 refresh of the MMI. This was very disappointing, and combined with the slightly dated look of the MMI system, I was considering retrofitting a 2013 MMI+ system. These plans were quickly squashed when I saw the $5,000 estimated cost of a retrofit. I came across a Bluetooth adapter that works with the iPod AMI cable to handle BT audio streaming, and that really scratched the itch. BT streaming works very well now - great quality and great experience.

The Nav system is nice, voice control is marginally useful, and the trim and finish of the interior is beautiful. MMI+ is useful, but it definitely takes some time to get used to. Unfamiliar passengers have no idea how to use it, despite its instinctive feel after only one week.

I love the color of the car. The Misano Red pops, and the black/lunar silver interior is sharp. The alcantara/leather seats (coupled with very supportive side bolsters) hold you in place but are comfortable enough. The look and feel of the car give it a good amount of flair, something I think Audis of old lacked.

The Drive

Audi Drive Select and the Sports Diff make this car. It's hard to overstate this. The varying characteristics of the car sold me on the move from a coupe to sedan, and the ability to switch between more comfortable or more engaged driving modes made me happy to leave behind the sporty coupe.
On a calm and quiet run (in Comfort), it's not the most comfortable car - bumps are still translated through the chassis to the seats - but it feels solid and refined when it does go over imperfect roads. Put the car into Dynamic mode and my favorite mountain roads are just as fun as always. My only gripe on this note would be the active steering. Nine times out of ten I know how firm/soft the steering will be, but every one in a while I'm caught off guard by it. I wish there was a setting for "always firm," but it's really not a major gripe.

The Sports Diff is hard to describe. I never test drove an S4 without it, so I can't compare it to models without, but it is something that is absolutely noticeable in spirited driving. This car just joyously pivots through turns. It's a sensation that you immediately notice - the rear of the car just pulls itself to the outside of the turn and you feel like you're in a tiny RWD coupe. It's an amazing sensation.

Road Feel Compared to 2008 BMW 328xi

Compared to the 2008 328xi 6MT coupe I traded in, there are a couple of key differences. Adjusting from a manual to a DCT with paddle shifters takes a little getting used to, but it does not hamper the fun of the car. In fact, there's a wonderful snarl in aggressive upshifts when moving from 1st through 4th that 6MT S4's do not make. It's also very, very smooth, so I'm no longer the world's angriest man in stop-and-go traffic. It's definitely larger, and it feels that way. There's a noticeable heft to the car, but it compensates with surprisingly agile handling and effortless ability to accelerate. Going from a coupe to sedan, I gained two usable back seats. There's enough room back there for 2 good sized adults. The 3 coupe had a respectable amount of room for a coupe, but this has a respectable amount of room for a small sedan, which is a big difference.

Engine and Power Compared to 2006 BMW M Roadster

I'd like to compare the engine to the S54 in my other car, an '06 BMW M Roadster. They both drop 330/333HP (the S4 @ 5500 RPM and the M @ 7900 RPM), but the torque peaks vary by about 60 ft-lbs - 325 ft-lb @ 2900 in the S4, and 262 ft-lb @ 4900 in the M. To compare the two is hard, since they're wildly different cars, but there's one difference I can draw. The M comes to life, singing its song at the high-end of each gear. The S4 delivers a ceaseless surge of power from the moment you pass 2,000 RPM. It's an entirely different sensation. If I had to compare the two engines, I'd say the M invokes excitement whereas the S4 invokes awe. This is just based on the engine - comparing an AWD sedan to a RWD roadster is otherwise a very difficult job.

Four or Two Doors? Compared to Crashspeeder's 2014 Audi S5

So, these cars are very similar, but there are some noticeable differences. I'm going to try and look past some of the differences between pre- and post-facelift and compare just the coupe to sedan.
First and foremost is the cabin. There is one win and one loss in each of our scorebooks as far as I'm concerned. The S5 feels like it's a cockpit. You feel a little lower, and little more enclosed, and in effect more connected to the car. It feels a little more special, and fun, in this respect. However, the back seats just did not work for me. The beautiful sloping rear of the S5 takes away a lot of the rear headroom. While Crashspeeder is happy as a clam sitting in the backseat, I could not fit comfortably.

As far as driving goes, I drove both back to back, and the same sentiment from above applies. The ride feels just a hair more special in the S5, despite sharing largely the same platform. While in my drives, they both felt more or less equally competent on the twisty roads of NE PA (with the S5 a hair moreso), there is something to be said for both, and here's my sentiment boils down to when comparing the two: the S5 impresses me because of its sporty feel, and the S4 impresses me because of how very close it comes to the sporty feel of its two-door sister.

First Week Impressions

I really can't wait to continue settling into this car. I was amazed at the confidence it inspired in the first week, and am sure that trend will continue. The torrential wave of power from that wonderful engine, mixed with the surprisingly agile feel of a stable AWD platform makes this a wonderfully fun and capable car.

Jan 20, 2014

A Tiny Black Box

I'm not a writer.  I really haven't had a clue how I wanted to frame, or even title, what I've been doing over the last few weeks.  I knew wanted to tell the story of an evolving project that taught me so much. As I sit here, thinking about the title of an article whose direction and purpose aren't perfectly clear to me, I turn and see the end result of the last few weeks.  It's a small, black box. A box that has an LCD, a simple keypad, and a couple of taped handwritten notes. It's a deceptively simple, and compact, an unimposing byproduct of the wanton creation that's taken place over the last few weeks.

My new "war chest" of parts (one of three, actually) and a picture of my desk 
in one of the early crazy nights of work.

So many spools of wire, cut and recut, and recut again to produce the right length cables to fit inside the box and to other components. Brand new organizers filled to the brim with parts, each one telling a story and waiting for another adventure. Soldering burns all over my hands. I see three, and feel four. A weekend of unadulterated learning and progress, moving from a blinking LED to a working prototype. A box of early version soldered boards with transistors, LEDs, wire, caps, and crimped quick connect terminals. A haze of memories that is almost distant in the tidal wave of new knowledge, but comes rushing back when I look at the photo backups in Dropbox.

I think back to where this whole thing first seeded from, and think of how completely unnecessary this whole project really was.  I solved a problem that, really, wasn't a problem.  While I'll admit it's neat seeing trains whiz about on the track in an ordered fashion, I have a lot more fun controlling them myself. It's one of the reasons I eventually added a "manual" mode to the project. I started this because I saw a challenge.  I became obsessed by this because I saw new frontiers of information and learning. I finished it because, apparently, I am incapable of letting a project go unfinished.

I worked on the initial track status module independently, but then drafted Orlando Marin to help with the prototype module.  He's a fantastic software engineer and friend, and it was a thrill to stay up until 4AM on weekend nights, starting off with teaching him how to make a blinking LED, and then working with him as a peer in the build process. We got the majority of the system working that weekend. It was a tiny bit buggy (my fault), only had one mode, used LEDs as status indicators, and was zip-tied to a piece of cardboard. But it was there. And it was glorious.

And I think I owe a little bit of an apology to my girlfriend. I've been a little absent at times when I got wrapped up in this. It's really hard to explain and quantify how I get wrapped up in these things, but you've dealt with me and my newfound obsession very well in the last few weeks. I can't begin to express my appreciation of that. It's a tremendous comfort to know that you support me in the crazy, stupid, weird things that I do, and I love you for it. I had to laugh when we were driving and you told me you were amazed, this was the longest I'd been obsessed with a project. I was thinking the exact same thing.

So, like every journey, let's start at the beginning...

The intersection of two passions

This whole project started off with two passions that I really didn't know would intersect. I've been in love with the idea of using an Arduino, if just for the ability to turn my professional software engineering skills into something tangible, touchable, and real. I know websites are real, but not real in the sense that you can touch it, or feel it. It's not a physical representation of your work as much as a blinking LED is. I remember my first experience with Arduino - building a circuit that lit up an LED to the inverse amount of light that a photoresistor sensed. This year, it started pretty simply as well, with a LED divisibility counter.

This was actually revision two or three - after I realized what transistors were
and why they were important.  I think back to this day and am honestly amazed
how little I knew about circuits and electronics.

As far as LGB trains?  I can't really remember a Christmas from my youth without them. I remember the little black engine was my favorite, and trying to make the trestles work, going around our living room couch (and failing). I set one train up last year, for the first time after a very long hiatus from trains. This year, insanity ensued. I made it a mission to clean out my storage unit, and inadvertently stumbled upon some boxes in the back. More trains! Holy shit, a lot more trains! I brought up the boxes of track and trains, and setup a three train, siding and spur track that occupied most of my dining room and the area around the Christmas tree. I felt like I was hunting Bugs Bunny every time I walked around it - every step was deliberate and exaggerated. If you've never stepped on a section of LGB socks without shoes, I'll tell you now - it fucking hurts. I remember one evening I was expecting my girlfriend, was tinkering with the trains, and as she walked into my place I spun around, holding two pieces of track. All I could think of saying was "Christine, I think I have a problem."

Pictures of the initial track layout and some diagramming.  It went through a few iterations
before I settled on the final layout in the last picture.  I had the timings of the trains down
perfectly - I could run all three at once with... minimal collisions.  Also, yes, that's "Fringe" in
the background.  A good show.  You should watch it.

Videos of some of the earlier "tree lines" that took up the entirety of
my dining room and a good portion of the space leading to the living room.
Little did I know - it would take up even more space in the coming weeks.

The first build - LEDs and photoresistors

The first build was primitive, to say the least.  But in that first round, I learned quite a few important things, some of which became very solid concepts for me. Other circuit design features were implemented with more of a "we do it this way because it works this way, that's why" attitude. For the longest time, I had absolutely no idea why the leads to the analog input for the sensors (photoresistors) had to go in between a resistor and the sensor.  But more on that later.

Initial attempts at playing around with LEDs and photoresistors.

The first build of the trainyard wasn't actually a trainyard, it was just a status tracker. I wanted to be able to see what was going on, and test out the photoresistor sensors I made. I started with three banks of LEDs, the left three for train activity indicators, the right three for track section power indicators, and the two in the middle to indicate when a sensor was tripped or cooling down. The mismatch in color and size drove me nuts, but my war chest at this point was a tiny purple box that had some resistors, LEDs, buttons, and jumper cables that came with the Arduino starter kit. Paltry in comparison to the 3 Stanley organizers of parts I wound up with.

Like most neat things I do in software engineering, I thought the cooldowns were the smartest thing I'd done in my life. Since I was using photoresistors to track when a train rolled over a section, I was relying on a dip in light (I later learned that this was an increase in resistance, lowering the voltage of the voltage divider circuit I was making) to trip the sensor. But as the train rolled over it, if light snuck in between the cars, would it trip again? So there was a cooldown cycle - timed to be about 1 second.

I swear, I sat there for a good 5 minutes just moving my fingers over the photoresistors on that board and watching the train and track status lights go up. It's one of the most remarkable feelings I've experienced in my entire programming career - to see a physical, touchable finite state machine operating as you specified. This must be what elevator programmers feel.

See, Christine? This is how transistors work... wait, what the hell?

I still didn't really have a grasp on how transistors worked.  I kind of assumed that it was like a relay of sorts. Pass a current to the collector, and allow it to pass out of the emitter when a voltage was applied to the base. I sat my girlfriend down and went to show her how when "I disconnect the 5v source from the collector, the LED will no longer light up!  Because it's like a switch!" I had the transistors emitter lead connected to the positive terminal of the LED. Anyone familiar with amplifying transistors might know what happened next. The voltage and current flowed through the base, through the LED, and out the emitter. There was no difference whether the collector was powered or not. Later that night, I went back to the project to see what I had done wrong. Eureka! It's an amplifying transistor, a 2N2222! Both terminals will connect to ground, and neither will if there's no activating current on the base! However, if there's voltage at the base terminal, regardless of current at the collector, it will still flow to ground.

Someone might be laughing at me now, but this is how out of depth I was with some of these devices. I was so caught up in trying to learn something new that I completely overlooked the fact that the base voltage had to go somewhere. So, I moved the transistor to the negative lead of the LED, and voila. The LED only worked when voltage and current were present in the collector and base. Small victory, but one of those clicking moments where you finally get something. The pieces of knowledge you have are combined to form a larger comprehension of what you're working with.

Sensors! They couldn't have worked out better!

I honestly couldn't have planned this out better. I was intent on using LDR photoresistors in this project to keep track of the trains. Problem was, I didn't really know how to mount them. I took a look through my train parts and the answer was there - tiny, flexible track clips.  They were perfect!  I could mount the LDR on them, put them in between two track sections, and that was that. I broke out the drill and put two holes straight through them, and used some Radioshack audio jack parts to to make it a little more modular, and it was done.  I electric taped the whole assembly, and I had working train sensors.

This really couldn't have worked out better.  They looked really good, in my opinion.
This was the first time I soldered in this project also - with a brand new soldering iron.

When I saw these suckers in action, I was so stoked that they worked. I had no idea what effect adding a whole load of cable in between the Arduino and LDR would have, but as it turned out, whatever effect it had was minimal enough, or standard enough so that the "difference" between light and dark was noticed.

Let's get physical, physical, I wanna get physical

After seeing the breadboard made, I didn't have anything else I could do as I waited on a whole bunch of parts from Amazon. So, I went out to RadioShack, got myself some patterned perfboard and a couple more transistors and LEDs (now they're nice, standard colors), and started playing with molten lead. I actually wound up having sushi that night and thought "boy, the heavy metals are really gonna get me." This is what resulted:

This was the first board.  Note the proper use of transistors and resistors.  I've played with the
Arduino and photoresistors before, but never have I actually used these components in a
build.  Mostly because I had no clue why they were important and necessary.

I was immensely proud of my accomplishment. The only soldering I had done before this was soldering heavier-gauge wire onto little Microsizer antennae to make the reception better. In that case, I actually destroyed the thing. Even though my solders were a little rough, the connections seemed to be fine. I grabbed my multimeter and repeatedly stabbed sections of the board to test conductivity. I used quick connect terminals to connect peripherals to the board. I was just so excited I was able to solder this board, and even more impressed that it worked when I plugged it in.

This first attempt ultimately should have shown me how little I knew about how to connect components. For example, I could have soldered male header pins to the board and just plopped it on the Arduino. Instead, I soldered the male header pins to the end of the wires. It was... horrific.  And some of the worst soldering in the entire job - I repeatedly pricked my fingers with the pointy solder, just about every time I grabbed it to plug the pins into the Arduino.

This is the point in the story where I really should have slowed down. I should have learned more about electric circuits, resistance, current, voltage and their relationship, power supplies, and Arduino basics. All of these principles ranged from a vaguely-understood concept to completely out-of-depth. Unfortunately, the hook was set, and I had grand plans in mind.

This is what we're doing this weekend

A friend of mine, Orlando, was coming out to visit for the weekend.  I had recently been bit by the project bug, and I knew I could convince him to jump onboard.  So I sent him an email.  It went something like this:

Subject: We're doing this this weekend
Body: http://pastebin.com/h2JBsfSe [this was an attachment, but I dropped it into Pastebin for this]

In some spare time, I had mocked up various layouts, including markings for sensors, switches and powered sections. I even attempted to work out the cycle. Unfortunately, at this stage I didn't quite have the flow of the system down, and some of it was a little wonky, but it was a start.

Enter: drama. The day before my scheduled delivery of a whole bunch of mission-critical parts (relay board, additional 5v relays, an LCD screen, some M-M and M-F jumper cables and an Arduino Mega), UPS notified me that they were going to be delayed in delivering my package. The package was due on Friday, and they don't deliver on Saturdays. I saw my grand plans for the weekend slip away. Thankfully, the guys at the local UPS warehouse were kind enough to fetch my packages out of the truck for me, and let me pick it up that Friday. Weekend: saved.

The Weekend Rush

That weekend, kicked off a lot of progress in the project. Piece by piece, we assembled the entire project. Orlando was new to Arduino, so the first night, I started teaching him about the Arduino platform. Honestly, he probably could have used a better teacher, because at one point, I even said "resistors don't really matter, just put one in if you want. Oh, but you do need it for photoresistors for some reason." Sigh.  Those were my dark ages.

Orlando's Blinking LED... err, his fading LED... err, his pot-controlled pulsing "BMW-blinker-link" Macbook power indicator LED

So, we opened a couple beers and got to work. By the end of the night, he breadboarded a tiny LED project. Starting with the blinking LED, he moved through a couple of examples to get to a pulsing LED whose fade speed was controlled by a potentiometer.  I believe he even added a button to turn it on/off before he was done with the project.  It was fun, seeing in him the same kind of enthusiasm and excitement I felt when turning a software project into a hardware project.  I wish I had a video of his reaction to playing with something he built and programmed.

Day 1: Track Power

Since we had the system status indicators and the connections for the photoresistors in place on my first board, we started with the powered section relays. We started off trying to power the relays directly from an I/O pin. Anyone familiar with Arduino power limitations is probably cringing right now. They require 78 mA to activate the coil. Now, you're crying. At this point, I knew about transistors, but I really thought that a very small current to the relay would do the trick.  I had no idea how to calculate required current from the coil resistance, no idea what the real max current from the board was, so on. Needless to say, this did not work out well. 

The next attempt at relay control involved transistors and resistors. Oh, that's right. We weren't using resistors up until this point because "resistors don't really matter." Some parts of this story are just... painful to recall. This attempt worked - we were powering the coil via the 5v rail of the Arduino, and they were activating. We had 4 on a breadboard, which we migrated over to a perfboard and soldered up. So many things were wrong with the schematic of these relays. Diodes weren't used as flybacks, they were inline with the negative coil pin to ground. Resistors were only used on IO pins, not on the 5v rail to provide the correct amount of current to the relay coil. It was kind of amazing that the whole thing even worked. But we had a sketch that clicked them on in succession.

Naturally, a premature test was in order. We setup a small straight section of track, and isolated one section. We tested the voltage of the main power section as well as the voltage of the powered section as it was turned on and off. Expected values were seen, and congratulatory alcohol was consumed. So we slapped a train on it, and moved it back and forth. In the main section, it ran fine as the relay clicked open and closed. When it got to the relay-powered section, it would move when it had power, and stop when it didn't. It was amazing. And we had more congratulatory beers.

Day 2: Switch (turnout) operation

Going into this project, I had no idea how this was going to be accomplished. LGB turnouts, it appeared, used AC. To Orlando and me, AC was magical, not a clue how it worked. I spent a couple days researching, until I came across a very helpful forum post that explained that the turnouts used something they were calling "rectified AC." First thoughts in my mind were "dammit, not only are we going to have to figure out how AC works, but we're going to have to learn about rectified AC." Then, I read a bit further and saw this diagram.

Diagram of LGB turnout operation using rectified AC.

I breathed a sigh of relief - I think I understood it. Diodes controlled the flow of electricity. AC works in a sine wave that I still don't fully understand. The diagram above is what we created, directly out of the relay board. Low voltage, AC current was allowed to pass directly through a diode, creating rectified AC. If you're familiar with AC, you might be guessing that since we weren't using a rectifier, just passing it through a diode, this was half-wave rectified AC.

First, we setup the Sainsmart relay board. After connecting it and playing with it for a while, we learned that it required a HIGH signal for relaxed and LOW for a relay to be activated. Once we worked out the clicking, we got to work soldering together the half-wave rectifier board. For each switch that was to be operated, two relays were used. Each relay sent its load through a diode (one in each direction), which connected and went to the switch. For a switch connected two two relays, a pulse from either would have opened it in one direction or another. We hooked it all up and gave it a shot. 

The first test of the switch system was a crazy important turning point in the weekend. I think we were a little tired of just making things without seeing them work, and I was really hoping for a success. We hooked it up, powered it, and waited. First relay pulse, nothing. I wasn't too worried, it could have been just switching to the side it was already on. Second relay pulse, nothing. I was devastated. Again, it seem like the plans for this project were falling apart. Both of the relays had clicked and sent their pulses, and nothing happened in the turnout. Third click, twitch! It was working! The pulses were set at 100ms, we just needed to up the timing a bit and the motor should get enough juice to flip to one side or the other. We took it all down, changed the code, and gave it another shot.

Attempts one and two at getting the turnouts to operate using the relay board, sending left/right 
pulses to the turnouts. Attempt one had 100ms pulses, and attempt two had 200ms pulses.

It was working.  It was working!  Now it was time to start playing with more than one switch...

Now, we really wanted to see something more - first starting with directions, left/right for two
different switches, and then actual track directions - sending pulses that corresponded to
section one, section two, and section three.

So, let's recap. Regardless of the proper circuit design and protection, this this is working. Again, we should have stopped, learned more, and regrouped later. But that's not how I operate, for sure.

Sunday Afternoon Tests

On Sunday, we were both pretty toast. We had been up until 3-4 AM the previous two nights, and weren't entirely up for too much more. Even the night before, Orlando was starting to fade away from the project, and we resorted to watching Fringe and I stripped cables to be hooked up later. After breakfast, we slowly ramped back into the project.

We started hooking everything up to the trains power transformer, turned it on, and sparks started flying. Directly into my lap, no less, since I'm holding said transformer. After the fear of an explosion destroying important body parts passed, I became mortified of not only destroying the Arduino, but not being able to see this project come to fruition. The whole sparks ordeal was caused by improper connections - I had shorted the transformer. And in the process, one of the connectors I was using to quickly test the voltage from the track relays had sparked up from the short. After a quick reset on the transformer and connecting the leads properly, all was back to normal.

We started laying out the track for the yard. The mainline (everything outside the three powered sections in the yard) was connected directly to the transformer, positive to the inside rail and negative to the outside rail. Each of the powered sections were basically one insulated rail, on the inside.

Two of the first full system tests.  I think system crashes may have been the cause of
the problem in these (and most) instances where we tested the system.  It became more
stable as the tests continued, somehow, but it wasn't really there entirely.

Not all of our tests proved faults in the automation system.  Some showed overly sensitive "shoes," the
DC pickups on the trains, some showed derailments because of the second sensor being triggered too
early, and some showed a very light train being flung at very high speeds around
a corner that was way too tight.  Too full?  Too full.

Towards the end of the day, a lot of the problems we were seeing were with timing
and the buggy software.  Running subsequent tests, it would run through just fine, but
since the cycles were hardcoded instead of based on train location.

Again, this probably should have been the end of it. The system was largely working, I could have packed up the trains, thrown the components into a box and returned my apartment to its normal, non-train-infested state. Of course, that's not how it worked. Because look at that tangled mess! Also, I may have mentioned before... LCD?

But this weekend was such a blast. In all seriousness, I do not recommend having a large scale project as your first real Arduino or DIY experience, but this really was an incredible amount of fun. Despite doing an obscene amount of things wrong, the system worked. Some glitches were in the placement of the sensor, some were due to poorly designed control software, but it worked. It was tripping sensors, switching turnouts, powering sections, and generally amazing the two of us. Seeing these results after a weekend of binge planning, coding, and building (and a little drinking) was beyond words. The feeling of accomplishment after this weekend trumped all of the other satisfactions in the course of the project, just because of the leaps and bounds in progress in such a short amount of time.

The finishing touches - like 80% of the time spent on 20% of the project

You know when they say software development is 80% maintenance and 20% new code? Or that premature optimization is the root of all evil? Or that the proper workflow is dev to staging to production? Yeah, I ignored pretty much all of those warnings and practices moving forward. Left unchecked with trains littering my apartment, this project spiraled even further out of control. There was so much more I wanted to do.

Things that were totally necessary to the completion of this project and my ultimate happiness and satisfaction with this build:
  1. Incorporate the LCD screen into the project for displaying status
  2. Some neat and enclosed end product - no cardboard and zip ties
  3. Add multiple modes: logical, shifting (what we already kinda made), natural, and manual
  4. Add some sort of input mechanism for manual mode
Not surprisingly, this is where the lack of knowledge and experience with Arduino started to really backfire.

Various modes of operation

Previously, the system worked on a very deterministic system - the next powered section that had to be set live was stored in an array, basically an array that was hardcoded into the initial sketch constants. Also, it was completely wrong. So, I modified the software to work much more modularly. Setting the number of trains, yard type, spur indicator, and other details would make the program work much more fluidly. The software changes really turned out great. I think, maybe next year, I'll go a little further and modularize the software into an Arduino library.

The LCD screen, the first cracks started to appear

First, I started playing around with the LCD screen and the sketch. Independently, it worked fine. I had a pretty good display system for showing what's going on with the system. When the system was running, it would display information like this:
  1. First Line: The currently selected mode of operation
  2. Second Line: The currently selected yard and mainline path
  3. Third Line: Train and Yard Status indicators
  4. Fourth Line: The last action that occurred in the system (ie, T1 left yard)
Early working version of the LCD and train sketch running on an Uno, with very early status info
displayed to the screen. At the end of this project, I never got "path" to display, but it's there,
just waiting for next years build.  

So, plugged it into the Uno, it worked fine. But as soon as this was implemented on the Mega with the rest of the components, things took a turn for the worst. Still, I don't know exactly why this was happening, outside of it having something to do with relays deactivating and causing interference that would cause issues and ultimately reset the board. Researching this, it seemed it was due to a lack of opto-isolation on the relay board. Instead of researching a proper solution, I opted to just swap out the mechanical relay board with an SSR board. The 200ms pulses the relays were subjected to did not affect the board at all in the end. As the board was in transit, I saw on the product page that it would only work between 75VAC and 220VAC. Which was, unfortunately, way higher than the 18VAC I was using for the switches. Luckily, upon arrival and implementation, they seemed to work.

Early not-working-so-well version of the LCD installed in the box, and crashing when the relays deactivated. With the Sainsmart 8-channel SSR board installed, the switches were operating perfectly with the LCD, additional relays, and all other components. Seeing them work was a breath of relief.

The Matrix Keypad

Amazingly, even this ridiculous little piece of hardware gave me woes.  I asked a question on the Arduino forum, but didn't find much help. As it turned out, the connector on the keypad was a little wonky. When I used the male headers to connect it to the Arduino board, they were coming into contact with two of the outputs from the keypad. Long and short of it, I wiggled the connector a bit, pulled the male headers out a little bit, and it worked. And I was, at this point, just completely done with adding things to this project.

It's finished.  It's finally finished.

I threw everything all together, cleaned up the box, and tested it out. It worked, and pretty well, at that. I created a new layout, long after my Christmas tree was discarded, and played with all the modes. Everything came together, was working well, and made me very, very happy.  Here's some video of the end result:

Manual and cycling modes on the final layout.  In manual, you control the open section by pressing a number and power sections by holding the number. In cycling mode, trains pull into the station of the last departing train.

Logical and natural modes.  Logical mode returns a train from the station it departed from. Natural mode randomly picks a train to depart.  Sometimes it's a new train, sometimes it's a pass-through. The natural mode hows how the switches and track power are independent.

One last video, showing off some of the operations from a high vantage point (me standing on my kitchen counter, basically):

Here it is, working fluidly in natural mode.  With Hobbes completely desensitized to the trains he used to despise with all his tiny little heart.

One last link: my Github Project with the LGB train control in play. More updates will come in the 2014 holiday season, when I pull the trains out of storage again, but for now, you can use it to play with your trains if you're so inclined!

I really can't think of anything else to say besides what's been said in the entire post above. This was such a fun project, such an amazing learning experience. If you have any questions about train automation, feel free to reach out and I'll help however I can. Just take all my recommendations with a grain of salt, I am the guy that said "resistors don't really matter."

Jun 24, 2013

Z4M... Finally an M

I'm really tired right now, but I have to post this...


Here are some pictures from a recent run I had with Orlando and Nash around NEPA (Francis E Walter Dam, down through Mountain Top, and around Rt. 29.  M3, M5, Z4M.  Awesome.

Update: Turns out the first picture here wound up winning BavAutos "Best Group BMW photo" in their 2013 photo award contest.  Might be featured in their 2014 catalog!

Jan 11, 2013

On Loving and Losing

First off... it's miserable.

I just left the rock climbing gym I go to after 20 minutes.  On the way out, one of the employees asked why I was leaving early.  I replied with a friendly "Lots to do today! Only had a couple minutes to drop in" while I was just thinking "Lots of misery to be felt tonight, have to get right to it!"

A couple years ago, I adopted a philosophy that I credit myself for, but in all reality, I probably just picked it up and forgot about the source: "you can't regret your past if you're happy with your present."

Right now, I regret my past.  I guess the inverse of the above, happy, positive sentiment would have to be "if you're not happy with your present, go ahead and regret the past."

It's really petty and I wish I didn't feel this way.  But I'm not happy with the present.  And for the immediate future, there's going to be a lot of regret.  I'm going to look back with a bitter recollection of the events of the last year.  I'm going to wish I was better, I acted differently, I had more luck.  I'm going to doubt my future moving forward.  I have to feel this way, because I have to be true to my emotions.

Losing love for me, is like being evicted from your home (or, leaving your home, depending on your perspective).

You might not always enjoy your home.  The pipes may leak, the floorboards may creek, and your neighbors might be freaks.  But for a period of time, you've grown into it.  You love your space.  You fit in it - you know how to stop the fridge from rattling, and have finally mastered the temperature setting on the hot water heater.

But then it's time to leave.  Who knows, in the future there might be a mansion in your future, a huge upgrade.  As insensitive as that sounds, it's not about looks or how big it is, it's about how good it fits you.  But you've become so used to that home, so comfortable with it, anything that could even remotely, by a third party observer, be considered a fault is just part of the package.  The package that you love.

The grief washes over you, overtakes you.  Everything feels muted and without substance.  Life feels like it won't go on, like it can't.  What you called yours no longer can be yours.  Words like pointless, hopeless, futile don't even begin to describe the emptiness you feel.  How could they?  Everything lacks substance.  The panic that paralyzes and pulls you down makes you understand what the fuck a SEGFAULT actually feels like.  When everything just stops working, grinding to a halt.

You just want to go back to the comfort you felt in your old home.

You don't want anyone to EVER reside in that home after you.

You vacillate back and forth between frantically searching for a new home and saying, with lackluster and unsubstantiated resolve, that you don't need to find a new home.  The search doesn't interest you.

But you know it was really all worth it.  You know that the reason you feel all of this is because you wanted it to continue.  Which means that you really miss it, which means you really enjoyed the time you had.  Floating somewhere out of reach, right past all the loss and pain and heartbreak is a real resolve, and a real sensation of growth.  You know it, but don't want to admit it, don't want to feel it.  You want to be angry, even if you can't be.  You don't want to be angry after you've just been consumed with rage.

You know it's inaccessible, but you know it's there.  I think that's why it's so hard.  You want so much to feel better, for relief.  But you can't get that relief.  You can't see it, or touch it, or even imagine what it looks like.

But you will.

And she will.

And all of these conflicting emotions and pains are part of the process.  Part of finding out who you are and what you want and where you want to go.  They're necessary to completing YOUR journey in life.  And that's important to remember - it's your journey, not your (plural).  It's your life to plan and figure out.  Ever done a puzzle? (Last metaphor, promise).  Ever been so certain a piece fit?  The pattern, edges, and color all match.  But eventually that piece is needed elsewhere.  It might just have to go back into the box, it may be paired with another.  It leaves you incomplete for a bit, but there is another piece somewhere that will fit you even better.

Plus, as a perk that comes nowhere near offsetting the pain, you get a shitload of time to work on your side projects that haven't been looked at in months.  So, hey, kind of a win.  Right?  Right?

Sep 2, 2012

What you know... it might be a lot.

Wow.  Just some precursory information here...

A friend and I are looking to build a PHP framework.  We have some good ideas, but we're trying to find a good direction (market) to head in to actually make a difference and generate adoption.

I just recorded a 1.5 hour chat session where we talked about the direction, focus, and implementation of the project, and I can only summarize in a couple words: holy shit, we know a lot.

It's hard to see how much you know until you listen to yourself talk in a meeting where you are confident and comfortable with the topics you are talking about.  And let me tell you, the topics we spoke about were very, very horizontal.  We spoke of everything from session handlers, to technologies, to framework ideologies, and so much in between.

After 20 minutes into listening to the conversation, I cannot even begin to express how amazed at how much I know.

It amazes me, how much information I have gathered and internalized over the last 7 years of personal and professional software development.  So much that I have become comfortable talking about, teaching, learning, understanding, and just shooting the shit about.

It does kind of illustrate a gaping flaw in my speaking and conversational abilities, to be honest.  I'm comfortable, in situations where I know that I'm comfortable, admitting that I don't know something, and learning something new.  I'm comfortable in situations where I know exactly what I'm talking about.  I'm comfortable discussing competing ideologies and theories if I'm well-informed.

But, what I have noticed, I'm far less comfortable when out of my element.  I'm not as comfortable being assertive, even when so much information in general.  I guess I need to work on that.

Aug 27, 2012

Do and apologize later, or not at all.

I routinely run into a code comment that I wrote a long time ago.  It always gives me a chuckle.  I like comments.  Unlike Atwood's approach to comments - the code should tell the story - I think that comments offer additional insight.  The why is just as important as the how.

The why tells the story.  It humanizes us.  The why in this comment reminds me of one thing:  I'm pretty damn smart.  The comment was something to the effect of:
This is bullshit.  There's no reason to not implement this check. I know it's going to be asked for later, it fixes 99.99% of abuse, will negatively affect a negligible percentage of our normal users, saves me hours a week combing through possible abuse cases, and took 5 minutes to write.  Executive.  Decision.
The last bit is an inside joke, but every bit of the comment came true.  Abuse dropped off a cliff, not a soul complained, I saved myself a boatload of time, and I was asked to implement it about 6 months after I had already done so.  Every time I run into that comment (which, admittedly, was a bit much for a comment - "this fixes abuse, doesn't affect normal use" would have sufficed) I'm reminded that I'm a pretty bright guy.  When I do things, I think them through and have a reason for it.  And if anyone came across that code, they would understand that there was a reason I implemented the check.

Code should definitely tell the how, but comments are a great place for a why.  Atwood's right, good code should be simple to follow and easy to understand what's going on, but sometimes the why it's being done gets lost in the soup.

We always need to remember the why.


I think we can all agree, idempotence is a pretty awesome word.  It rolls off the tongue, is in a class of words outside of the regular lexicon, and it starts with a long vowel sound.

This blog post on duruk.net is a fantastic guide to becoming a good web developer.  If you follow all of these (most of these) simple tips and guidelines, you'll be creating useful, secure web applications in no time.  All that's left at that point is the creative spark required to generate an idea (and some basic web development knowledge).  One thing that was in here that I'm guilty of not following is this:

Know why it’s important that your GET requests should always be idempotent.

I mean, I know why it's important, but I just never actually follow through with it.  I don't think I've once run a check for a POST request on a logout page.  I feel a little depressed thinking about that, but then I realize that none of my projects have garnered enough popularity for an externally hosted image logout hack to really affect my user base.  And then I became more depressed, but at least a little more secure in my depression.

Idempotence, for those of you who don't know and are too lazy to use Wikipedia, is the quality of an operation to produce the exact same result, without changing the state of an application, when performed multiple times (at least, the computer science definition).

Logout actions are not idempotent.  They change the state of the application, and more importantly - they're frequently performed as normal ol' GETs.  When I think of a web application, I think of the following types of basic actions:

  • GET "pages" that display information to a user
  • POST "form submissions" that accept information from a user
  • POST "actions" that change the state of an application (I'm thinking $.ajax here)
That's my basic model for a webapp.  You have pages that are accessible, idempotent, and display information to the user.  You have forms that allow your application to collect a decent chunk of information from a user (signup form, settings, post a comment, etc).  You have Ajax actions that can, among other things, change the state of the application (these should be wrapped in XHR checks).  Of course, you have other things, like email confirmation links and the like, that do change the state of your application, but are secure enough since you probably have a key or other unique identifier (ie /confirm?username=mimarcos&key=foo).  I completely overlook the fact that certain GET operations may alter state without any significant security (ie /logout).

So I guess the point of this post is to make sure you are constantly revising the mental model you employ while developing your applications.  There's bound to be something you don't know that could significantly update how you view your development world.

This one liner about idempotence is not necessarily a hidden jewel of programming practice, it's actually pretty fundamental.  But, it's something I've overlooked, you may overlook, and internalizing it tweaked my view on web development.