Why Hardware Gotta Make Me Love It So Hard?

Often when working on an electronics project I experience a problem that takes me way too long to debug. I don’t mind iterative debugging, it’s where I’m partially hanging my hat these days professionally, but it’s painful how often my lack of experience leads me up a dead end. The last time I got so angry I felt the need to write a post about it was an hour lost to software debugging when I had just crossed a couple of wires.

Continue reading

Fun For Young and Old

One of my goals for the 2011/2012 Christmas break was having fun stuff to do with my daughters to cut down on the boredom and minimize slack-jawed TV watching.  While planning this out I stumbled across the Wayne and Layne kits, and the Blinky POV kit looked really nice.  It looked like a great kit to use to teach my daughter Mary (5) how to solder – it didn’t have too many components, it looked like it would be fun to play with when it’s finished, and I’m really, really intrigued by the “hold it up to the screen” programming method.

Right before I ordered the kit my Mom contacted me and asked for gift ideas for the girls. I immediately suggested this kit for Mary and sent my Mom the link to the project.  She said it looked like a great gift for Mary (which I expected) and she also said she really liked the idea of the Tactile Metronome kit (which I did not expect). I couldn’t pass that up, so I bought both kits.

Continue reading

Abusing a Character Display

One of the first things I bought to use with my arduino was a 2×16 character display, specifically this one from Adafruit.  Playing with it was fine, but after looking at the interface to the LiquidCrystal library I decided to see if I could abuse it by making it into a cheap-o Etch-a-Sketch.

Stage 1 of this project worked fine, and was actually a bit of fun (although it was kind of cheating – I’m supposed to be challenging myself with hardware, and this was really all just programming).  I knocked up a couple of trimmers to use as the knobs, whipped up some modulo arithmetic, and I turned my 16×2 character display into a 80×16 pixel display.

Here’s a not-great video of the result:

Next I wanted to make drawing persistent, to be able to actually draw.  Seemed simple enough – I had been building custom characters on every update, with the current x,y bit turned on, then writing that char to the correct x,y character position.  All I should have to do it maintain an array of characters.  Then on each iteration when I set the x,y position, it’s an update, not an entirely new character.  Then when I write the character out, I’ll get the entire state for the board.

Unfortunately, this exposed a pretty fundamental misunderstanding I had of the character displays.  I assumed that since I was only updating a single “character” at each loop, all the characters I wasn’t touching would just stay put.  However, when I re-create the custom character at each loop, that also updates every place on the display that was showing that “character”.  It actually looks fairly interesting as, over time, multiple characters on the screen start to come to life and start changing in sympathy with each other.  Cool, but not at all what I wanted.

This failure completely makes sense as I think about it.  Since the LiquidCrystal library only supports 8 unique custom characters I can’t make use of the whole screen just through the standard library.  It’s possible that the 8 character limitation is arbitrary and that I could raise it by mucking around in the library, but that doesn’t sound like that much fun.

There are a couple of possible next steps I could take here:

  1. Just to show the core of what I wanted to do, use the 8 custom characters I get from the LiquidCrystal library to make a 2×4 character (16×20 pixel) display.  I will probably do this and take a video of it just to have done it.  One of the original goals in this project was to just make anything that I wasn’t copying from someone else.  Ideas include a write/erase function, “status” lights, an “invert” button, a non-state-changing cursor, basically anything to take up a pin on the Arduino.  Now that I write this down and remember what my goal was, I’ll probably do this no matter what
  2. I could track down the LiquidCrystal library and understand what the root of the 8-custom-character limit is.  If it’s an arbitrary software limit, maybe raise it and recompile.  If it’s not arbitrary but due to memory, maybe make a custom version of the library in which some standard characters are missing, allowing me to use their memory space for more custom chars.  I doubt I do this anytime soon, but if I decide to play around more with Arduino libraries, this might be a fun platform to learn more about them.
  3. If the limit on the number of custom characters turned out (somehow) to be hardware based, then I could skip the LiquidCrystal library altogether and see if I could somehow force the hardware to do what I want if I give up on the need for standard characters.  I think the only way I would do this is if I actively needed to learn about the LCD chipset’s low level use for some other reason.
  4. There’s a strong possibility that I may bag everything above because I had an idea for a new project that I want to get working using my Arduino.  Since this is the most interesting thing I’ve made that I haven’t copied form someone else, I should take this opportunity to think about how I will store project information.  I’m not fond of the Arduino dev environment’s file system layout requirements and concessions I have to make to it to use SVN, but I do have this sketch in SVN.  Need to lean more about creating and storing schematics.  Can I store them with the Arduino sketch without the Arduino software complaining?  What’s the best software to use?  Right now I just want to sketch something out that I can use to recreate the circuit later, and that I can put in version control.  ASCII art would work right now, but I wonder if there’s an advantage to finding more specific software this early in my learning cycle…

Right.  Disappointed that my original idea didn’t work, but still plenty to learn in that list above.  I wonder if I can get 1 and 4 done while waiting for parts for NewProjectIdea?

Arduino’s map() Function and Numeric Distribution

The Arduino map() function is an interesting beast. Very technically it works exactly as its documented to work, but not the way almost every example uses it.

Here’s an example you can find in hundreds of sketches online, including the actual documentation for map():

val = map(val, 0, 1023, 0, 255);

This is a simple map, and one would expect that every four ticks on the input would map to one tick on the output (that is, {0,1,2,3} -> 0, {4,5,6,7} -> 1, etc). But that’s not what the function above actually does.

To show the issue, let’s make the output range smaller (but still an even divisor):

val = map(val, 0, 1023, 0, 15);

This should result in an even distribution, 64 input ticks per one output tick. To test this, I wrote a quick script implementing the Arduino map logic (which they were nice enough to document):

long map(long x, long in_min, long in_max, long out_min, long out_max)
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

The script prints a table of output values and the number of times that value was returned. Here’s the output for map(?, 0, 1023, 0, 15) for each value in 0..1023:

map(0..1023, 0, 1023, 0, 15);
  0   69
  1   68
  2   68
  3   68
  4   68
  5   69
  6   68
  7   68
  8   68
  9   68
 10   69
 11   68
 12   68
 13   68
 14   68
 15    1

That’s definitely not an even distribution. Now here’s a really stark example:

map(0..1023, 0, 1023, 0, 1);
  0 1023
  1    1

That’s a pretty egregious imbalance.

I mentioned earlier that the function’s actually working how it’s documented to work, just not how it’s usually used in examples. The map() docs state that “[t]he map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.”

This completely makes sense – if you imagine a range of 1024 values between 0 and one, all of them will be less than 1 except the last value, and since it’s integer arithmetic, all the less-than-1 values are 0.

The solution is fairly simple – increase the in_max and out_max args by one more than the actual maximum value (and then wrap the output in constrain(), which you ought to have done anyway). It’s fairly easy to work through why this works in your head, but here are the same examples I gave above with the increased maximums:

map(0..1023, 0, 1024, 0, 2);
  0   512
  1   512
map(0..1023, 0, 1024, 0, 16);
  0   64
  1   64
  2   64
  3   64
  4   64
  5   64
  6   64
  7   64
  8   64
  9   64
 10   64
 11   64
 12   64
 13   64
 14   64
 15   64

I have worked through this and I now understand how to get the values I want out of constrain. What I don’t know, however, is why the docs and the examples don’t address this issue. I know I’m not the first person to find this issue because I’ve found sketches on the internet where people are doing this (and they’re doing it in a way that leads me to believe they’re doing it on purpose), but I couldn’t get Google to show me any pages where someone actually addresses this point. I wonder why?

As for why I was looking into this, I was playing around with a couple of pots and an LCD, making a fakey etch-a-sketch using the 2-row LCD I have for such things. I could not figure out why my vertical control would only drop me down a line when I had the pot turned 100%. Now I know…

Edit 2011-09-13:

I posted this in the arduino.cc forum last night. As I expected, this is an issue known in the arduino community. Among other things, I was referenced to this post discussing the issue. My problem with all of this isn’t so much that it’s odd behavior, but that it’s been odd for at least 2 years and there’s no mention of the oddness in the map() documentation. The docs are a wiki, so I proposed an addition in the forum thread, maybe I’ll get to add it and help someone out in the future.

This is a Thing I Made

I made this a while ago.

It’s 8 LEDs and a pot, with the position of the pot controlling how many of the LEDs are lit. This is not a complex project, but I like it very much because it’s the first thing I put together that was not just copied from a tutorial. The shift register/8 LEDs was from one project, but I figured out how to add a pot to the circuit, read the pot position off of an analog pin, and set the LEDs accordingly. I am very proud of my silly little circuit.

Hardware is hard, let’s go shopping…

After a brief diversion into soldering I was back to playing with the Arduino last night, working through samples in the Arduino Experimentation Kit I bought a while back from Adafruit.  I’m only up to experiment 4 because I’ve spent a lot of time trying to understand the underlying electronics theory (why can’t you drive a motor directly from an arduino pin?  I mean, mathematically?  What the heck does a transistor actually do?).  The experimentation kit seems geared more towards getting people comfortable with the programming than the electronics, so wikipedia and I have been spending a lot of time together.

Last night’s experiment was working with a shift register to drive 8 LEDs.  There was a previous 8 LED project, but those were driven directly from Arduino pins, this project was designed to drive the same 8 LEDs, but only using 3 pins.  Wiring it was a snap (more or less, still trying to get a grip on the best order in which to place components) and the software was easy to load.  Just needed to give it a cursory run to prove it works so I could move on to what I really wanted to do, which was to see if I could figure out how to load a potentiometer into the circuit and use the LEDs as an indicator of the pot’s position.

Compile, load, and… Well, ok, the light’s were blinking, but not in the order I expected.  Changed the code to increase the delay between cycles, and the blinks were definitely not what I expected.  Rereading the project details, I notice it never actually says what the lights will do, but it seems really clear from the code that it will be a 0-255 binary counter.  But that’s not what it’s doing.  I spent an hour reviewing my code (which means I would have found a bug in the kit’s sample code, which seemed unlikely), removing the loop, trying to drive just one led for the whole app…  Nothing.  The pattern of the lights would change everytime I made a change, but not in any manner I could predict.

It was late so I decided to hang it up for the night and try again later.  I put everything away, and was standing up when I happened to glance at the wiring diagram.  I instantly noticed I had reversed the CLOCK and LATCH pins on the shift register.  The Arduino still had the last sketch I loaded on it (which simply sent “8” to the shift register), so I plugged in the 9v battery and voila – LED #3 lit up.  Arggg!

It seems likely that it will take me a while to get used to the rhythms of electronics/microcontroller debugging.  95% of the time I know where to look for a bug in a piece of software I’m writing instantly, but because the lights did something, I assumed the circuit was right and the code was wrong.  Bzzt.  That would be like a junior developer telling me his code wasn’t working, and he was pretty sure it was a bug in the library.  Nope.

On the plus side, now I’m ready to play with the pot tonight!  Whee!