Sunday, February 15, 2009

Electronics: Microcontrollers and the DIY LED Heart for Valentine's Day

I’d been looking to continue my old hobbies in electronics. I’ve been building small circuits when I have free time to sit at the workbench and fiddle with the old soldering iron and breadboards, but I’ve really wanted to get into the world of microcontrollers. Well, thanks to a “Slashvertisement”, I came across the DIY LED Heart Valentine Card from Nerkits.com.


Figure 1. The Microcontroller based Valentines Card.

Pretty much, most of the details are on their site. The project requires the USB Nerd Kit, wire strippers (I use a set of auto-strippers that I bought several years ago, when you have to strip wires a lot and quickly they are great to have, and it’s a pain to use the old fashion kind), and some basic construction tools. I used a piece of foam/cardboard I picked up from Walmart to build the heart, a Exacto knife to cut the hole for the LED Faceplate, and a drill to drill holes for the LED’s. To hold everything together, I used Gorilla Glue, which had a fortunate side effect of causing the Styrofoam in the board to bubble up before hardening, which worked great to hold all the circuit pieces onto the poster board.

Putting the circuit together was simple. I first followed the directions in the Nerdkit PDF file to build the base circuit (in the Nerdkit PDF, this is all the way up to step 10, which includes building the circuit, attaching the LCD screen, and attaching the programmer, and compiling and installing some test programs). From there, the rest of the project involved wiring up the LED’s as indicated in this template:

A little hint, to do the wiring from LED to LED, I used an old fasion wire wrap tool. This took maybe 15-20 minutes to completely wire the entire common wire run across the back of the template. To do the individual connections, I again, used a wire wrap tool to connect two posts in the LED’s, then I would only need to connect a single wire from the MCU to the pair.


Figure 2. Wire Wrap. Notice the single large white wire connecting to the post at the bottom of the image. Much easier than taking two wires from each post and trying to connect them in the middle. A single wire wrap takes like 5 seconds to complete.

Because this was going into a permanent card, I ended up soldering everything onto a circuit board. I ended up using a simple 6” circuit board from Radio Shack. The reason I chose this board is because it had the two side rails for both +5V coming from the voltage regular and a ground, had a 7mm center divide for the MCU, and to has a layout similar to the breadboard included in the Nerdkits kit, with numbered sides, so I could follow the instructions exactly. I also connected two 16 pin IC sockets back to back in place of the MCU during construction, this way I could solder everything to the board without the microcontroller in place and I wouldn’t have to worry about damaging the chip due to heat from the soldering iron. I could plug in the chip when I was done with construction.

The only modification I made to the circuit was to solder an additional four wires to act as “Posts” for the MCU programmer. I put 1 wire on the +5V rail, 1 on the ground, and 1 wire on each of the appropriate pins for the programmer to work. Then, if I needed or wanted to change anything, I would only need to flip the switch to set the chip into programming mode and attach the USB programmer to the posts using alligator clips (if I had time, I would have used some sort of connectors).

So how did it work? Initially, I ran into some issues. The code provided would cause the MCU to lock up unless I changed the delay timing. Thanks to the excellent support from Nerdkits, I was able to determine this was not a problem with the code, but with the version of AVR (the programming environment) for Windows that I was using. Once I updated to the newest version of AVR, everything worked perfectly.

So how would I rate my experience working with the Nerdkits tools. I am actually very impressed. There are a lot of Microcontroller kits out there (Basic Stamp, Adafruit). I got the USB Nerdkit for 80 dollars. A Basic Stamp kit runs about the same price at Radio Shack, the Adafruit kit run from anywhere from 30 bucks on up, depending on what you need (the equivalent to what I got from Nerdkits is about 65 bucks, minus the LCD screen and guide). So what attracted me to Nerdkits (besides the Slashdot advertisement), and what will keep me going back to them? Most importantly, the excellent support. I sent their support a message around midnight on Saturday. I had a response waiting in my Inbox on Sunday morning when I woke up. I can’t get that kind of support from most big vendors, including ones that I've spent thousands of dollars on server equipment and have "Gold" support with, so to get that from a small shop is incredible. Not only that, I had a follow up message a few hours later with the fix. In the message the guy said he played around with it and figured it out. This wasn't some dope in a call center reading from a script, but an actual guy (according to their site, an MIT engineer) who took the time to replicate and troubleshoot the issue, and on a Sunday. I can’t get that kind of support from people I know personally. I liked the included tools. The IDE used to program is an open source one, so I can play around with it, and maybe try to port the compiler over to something like Eclipse if I want to. The instructional material was easy to follow, and their are tutorial videos on their site. I had my initial kit up and running in less than 2 hours by following their instructions. The example kits are fun, and I like the fact that I can write code in C for the microcontroller. While I know BASIC and could have managed on a BASIC Stamp, I prefer C, especially for doing things like bit-shifting and working with registers on the microcontroller. And the end result is a lot simpler than the BASIC stamp modules bulky modules.

I’ve been wanting to build a few mini-moving lights for a Halloween project, and most of the components are already built in examples on their site (a combination of their Servo-Water gun (using 2 axis instead of 1) and some PWM ultra-bright Red, Blue, and Green LED’s will work for what I want). Overall, I am very impressed and happy with my experience from these guys.

And most importantly, the wife loved it :)

Figure 3. This looks a lot more complicated than it really is. I used ribbon style wire to go to each LED pair, and all green wires to connect to the LCD (hence the bundle of green wires). I lost the batter clip I bought, so I ended up using Duct Tape to hold the battery in place.

Saturday, January 03, 2009

Tech: Fixed TV for Father In-Law (DTD1363-CAR)

I spent the better part of the morning re-kindling an old love of mine, Electronics. I’ve been dabbling with electronics off and on for 15 years now. In fact, Electronics was an early career path for me before I decided to settle on programming. But, just like many other fields that I’ve had hobbies in, I find myself going back to electronics every now and then.

So this morning I tackled something I haven’t done since the early part of the decade, repairing televisions. My father-in-law brought me a television that he bought for his son a couple of years ago. This set is a Disney Cars TV/DVD combo unit (model # DTD1363-CAR). It’s a cute little unit, shaped to look like Lightning McQueen. And much like McQueen in the movie when he’s got no gas, it would run for about 5 minutes, then shut off. Kind of heart breaking for a 6 year old. Having working in a TV/VCR repair shop in college, I agreed to take a look at it for him.

What I ended up finding was some cold solder joints around the flyback/horizontal output transformer. It’s a big ugly unit with a large red cable running from the top into a suction cup in the CRT. I read a lot of silly suggestions on Fixya (http://www.fixya.com/support/p520372-disney_dtd1363_car_13_tv_dvd_combo), but the flyback cold solder joints suggestions led me to the visually inspect the joints. Sure enough, there were dull, porous solder joints around the transformer. To fix this, I removed the old solder using a solder sucker, prepped the area with small amount of solder rosin, and re-soldered the connections. I also re-soldered a few other spots on the board that looked suspect. So, what would have cost him about 70 bucks at a TV repair shop took me about 45 minutes. Hardest part was figuring out how to open up the case on this thing, which involved removing the plastic lugnuts in the tires that were hiding the screws holding the trim pieces in place. Outside of that, there wasn’t much else to fixing this. Keep in mind, this TV is indeed a piece of junk though, as are most TV/DVD combos. This is the problem with electronics manufactured in 3rd world countries where old plants are half-assed retrofitted to mass production, and there are poor QA standards, air quality standards, and so forth. I had a TV that my parents passed on to me that lasted well over 15 years, and we ended up selling it at a garage sale. I bet that thing had the tube zapped, it is still kicking out there.

Be wary of self diagnosis on TV’s. We used to pick up old sets from people who would throw out good sets because the picture on the tube would shrink to a “line in the middle” or a “dot in the middle”. So of course, people would assume the picture tube went bad and it would be too expensive to fix. 9 out of 10 times, however, it was a deflection circuit, and the part would cost about 30 bucks to fix it.

Maybe I should go pick up broken TV’s from Craigs list, fix them, and sell them for a little extra money. Not sure I want that kind of headache again.

Friday, November 07, 2008

Games: Little Big Planet

I try not to blog about gaming too much since there are far better sources for this kind of material, some better, some worse, but thats all a matter of taste. I do have to say though, as an avid gamer, Little Big Planet has knocked my socks off. I picked this game up mainly due to hype, and half expecting to be let down. Truth be told, when I got the game home and couldn't update to the latest version (at this time, 1.03), I was really disappointed since it wouldn't allow me to see user created content. So I was left with the stock levels.

I won't bore you with the details about "Sackboy" since the main character and his possible customizations are covered very well in depth by the many gaming rags on the market. I will say he is cute and clever, as is the control scheme for him. I can't help myself but to have him disco dance ala Saturday Night Fever after every level.

The stock levels showcase the games physics and themes really well. It has a very animatronic theme going, with characters being voiced by little speakers, and movements being done by something right out of a Rube Goldberg invention. They are challenging, with a very silly backstory behind many of the different levels.

Once the gremlins with the network, be in on my end or Sonys end finally cleared up and I was able to update, I got to see where LBP really shines. User created content always ends up enhancing a game, and showcasing possibilities that the developers couldn't imagine. In this case there is no exception. I played around with user levels, some of which were mere mechanisms to earn trophies, but others were clever, challenging platformers that brought me back to the yesteryears of the NES. I had one level where I was doing a mock prison break, another where I was going through the American Gladiators obstacle course, and one where I played basketball while wearing a rocketpack.

So, those are the facts, so whats my opinion. I can openly say this, and all naysayers be damned. This is the first game where I have actually smiled and enjoyed playing. I haven't even gotten into level creation, just playing, and this is easily one of the most enjoyable gaming experiences I have had in a long time. Finally, something that breaks away from the tired first person shooters, dystopian futures, survival horrors, and the getting very thin rhythm games (although I did pick up Gears of War 2, Guitar Hero World Tour, and Rock Band 2, so apparently I still have some demand for these games). Easily the best game for the PS3, and I look forward to seeing what else the community can cook up.

Thursday, November 06, 2008

GWT: Calling GWT Classes and Methods from Regular Javascript

My current project involves us working with a group who uses a custom built web framework that works on top of Dojo. Since I prefer to use GWT, even for strictly client side Javascript due to the debugger, I needed something that would allow me to create a somewhat functional Javascript based library. Out of the box, doing so in GWT is a bit of a pain, involving the need to create native JSNI mappings to the GWT methods. This is ugly, and one of the reasons I like GWT is because the code can be much prettier in development, even if it is not in deployment. I would prefer a much more efficient way, where the creation of the publically exposed methods are hidden for the most part during the development. This would normally take some function of the compiler to do this, but the clever folks behind the GWT Chronoscope project figured out a way to do this using Generators. They were even nice enough to release the fruits of that work free of charge.

Now, I will give you fair warning, download the sample code from the SVN repository. The documentation for this is non-existent, and the only way you can figure anything out is by looking at the samples. Here I will try to give you a good idea how to implement this in your own code by using a real simple proof of concept.

In the following example, I am going to create a new GWT module that will expose a simple Hello World return to a DOM object whose ID will be passed in. I will be doing so in a GWT project in Eclipse, and my runtime stuff will be done using the Instantiations GWT Designer, although you can do this in any GWT IDE you like. This will be compiled against GWT 1.5.2.

The first thing you need to do is add the library for GWT-Exporter into your projects classpath. In my case, I am using the Alpha release for version 2.

Once added, I create a simple module with a blank onModuleLoad. Normally in the onModuleLoad, we want to load and prepare out Ajax based stuff, but in this case, I don’t want anything yet, I will add a little code to jumpstart the export process in a bit. First, I want to create a class that looks like so:

package com.digiassn.blogspot.client;

import org.timepedia.exporter.client.Export;
import org.timepedia.exporter.client.ExportPackage;
import org.timepedia.exporter.client.Exportable;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;

@Export
@ExportPackage("helloworld")
public class UpdateWithHelloWorld implements Exportable {
@Export
public void UpdateMyDomObject(String id)
{
DOM.getElementById(id).setInnerHTML("Hello World");
}
}

Also note the annotations for package and Export. This tells the generator that this class is to be exported, and that it is accessible by that package name.

With that class there is a slight problem. The Exportable class is not set up properly in the GWT Modules XML file. So I need to modify my ExporterExample.gwt.xml file to contain the Exportable class. Be sure to add in the option to export, otherwise it won't export your classes.


<module>
<inherits name="com.google.gwt.user.User"/>
<inherits name='com.google.gwt.user.theme.standard.Standard'/>
<entry-point class="com.digiassn.blogspot.client.ExporterExample"/>

<inherits name="org.timepedia.exporter.Exporter"/>
<set-property name="export" value="yes"/>
</module>


Now I can go ahead and update my onModuleLoad to tell the GWT-Export to export UpdateWithHelloWorld for outside Javascript to use.


package com.digiassn.blogspot.client;

import org.timepedia.exporter.client.Exporter;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class ExporterExample implements EntryPoint {
public void onModuleLoad() {
try {
Exporter export = (Exporter)GWT.create(UpdateWithHelloWorld.class);
export.export();
} catch (Exception e) {
Window.alert("Exception " + e.toString());
}
}
}


For now, I want to open my public HTML page, and create two things, the DIV tag that will be updated, and a small Javascript method that will call my GWT class. I use the following as a small test.


<html>
<head>
<title>Wrapper HTML for ExporterExample</title>
<link type="text/css" rel='stylesheet' href='ExporterExample.css'/>

<script language="javascript" src="com.digiassn.blogspot.ExporterExample.nocache.js"></script>

<script language="javascript">
function updateText()
{
var hello = new helloworld.UpdateWithHelloWorld();
hello.UpdateMyDomObject("updateMe");
}
</script>
</head>

<body>
<div id="updateMe">I show nothing right now</div>
<input type="button" onclick="updateText();">
</body>
</html>


And whala, when I click on my button, it will update my DOM code using the GWT. This demonstrates that a GWT based class and method are called from and outside Javascript.

You got to hand it to the guys over at Timepedia, this is a pretty cool little library. Now it is possible for me to do any Javascript based development, debugging, and re-use of components with my development in GWT for reliability and cross-browser support, and use in non-GWT based calls.

Thursday, October 30, 2008

General: Using VI to Insert String Before a Line and Append Strings After a Line

I am an avid supporter of VI in the tex editor wars. Always have, always will. With that said, I have to admit, I was a bit embarrassed when I needed to a simple task of adding a string to the front and end of a whole list of lines (tables names I had pulled from a database) and I couldn’t remember the VI commands to do so. What I needed was to add “drop table “ and “;” to the front and end of each line, respectively. The commands I ended up using were:

:1,$s/ ^ /drop table /
:1,$s/$ / ; /

Strange, I’ve been using VI for years, and never needed to do that before. I am so used to my small set of commands, I never bothered to expand out and learn everything else that VI could do.

Here is a list of sites that have useful VI commands, shortcuts, and tutorials:
http://andrewfraser.wordpress.com/2007/01/12/useful-simple-vi-commands-for-dbas/
http://www.viemu.com/vi-vim-cheat-sheet.gif
http://tnerual.eriogerg.free.fr/vimqrc.pdf
http://www.eng.hawaii.edu/Tutor/vi.html#modes

Sunday, October 26, 2008

ASM: Revised Chekerboard program, now with flashing squares

I revised by checkerboard ASM program to have the white colored squares fade in and out. I wanted to do this for a few reasons. It shows a palette shift, which I have demonstrated previously, but it also shows how to do a time delay in ASM. The time delay turned out to be a little trickier than I thought. I tried using the DOS Get Time interrupt, but if you don’t test all the registers it returns and try to get away with just the DX register, you end up with unacceptably long delays when minutes change or hours change. I ended up using the system BIOS call to get the system clock ticks. The same problem will arise after midnight, but that can easily be overcome by testing the appropriate flag which gets set when midnight occurs. Again, I have included comments, and I also included the addresses that DOS Debug displays. If I do anymore ASM programs, I thing I will use an assembler going forward. Its easier than having to specify jumps by address, which change when I add something new to a program.




--initialize video mode to 320x200x256
13F2:0100 B81300 MOV AX,0013
13F2:0103 CD10 INT 10
point data segment and extra segment to video memory
13F2:0105 B800A0 MOV AX,A000
13F2:0108 8ED8 MOV DS,AX
13F2:010A 8EC0 MOV ES,AX
13F2:010C 31FF XOR DI,DI
--initialize registers with values of palette to use
--use word instead of bytes for the stosw instruction
13F2:010E B80F0F MOV AX,0F0F
13F2:0111 BB0000 MOV BX,0000
--initialize to the number of veritical squares
13F2:0114 B90800 MOV CX,0008
13F2:0117 51 PUSH CX
--start execution, logic is explained in previous post
13F2:0118 B9C800 MOV CX,00C8
13F2:011B 51 PUSH CX
13F2:011C B91400 MOV CX,0014
13F2:011F F3 REPZ
13F2:0120 AB STOSW
13F2:0121 50 PUSH AX
13F2:0122 89D8 MOV AX,BX
13F2:0124 5B POP BX
13F2:0125 59 POP CX
13F2:0126 E2F3 LOOP 011B
13F2:0128 50 PUSH AX
13F2:0129 89D8 MOV AX,BX
13F2:012B 5B POP BX
13F2:012C 59 POP CX
13F2:012D E2E8 LOOP 0117
--get ready to ramp down the white palette entry
--do this 3fh times
13F2:012F B93F00 MOV CX,003F
13F2:0132 B3FF MOV BL,FF
13F2:0134 FECB DEC BL
--initialize with the port to tell video card we are changing
--a pallete entry. This should be changed at some point to
--actually retrieve the pallete entry to change
13F2:0136 BAC803 MOV DX,03C8
13F2:0139 B00F MOV AL,0F
13F2:013B EE OUT DX,AL
--increment dx by 1, to point to the port to actually
--set a pallette entry
13F2:013C 42 INC DX
13F2:013D 88D8 MOV AL,BL
--output the new pallette entries
13F2:013F EE OUT DX,AL
13F2:0140 EE OUT DX,AL
13F2:0141 EE OUT DX,AL
--we need a slight pause so this is effect is visible, otherwise it
--just looks like ugly staticy squares, so save our registers
13F2:0142 50 PUSH AX
13F2:0143 53 PUSH BX
13F2:0144 51 PUSH CX
13F2:0145 52 PUSH DX
--we want to test this by a single clock tick, 1/18 of a second, which
--we will use DI to compare against
13F2:0146 BF0100 MOV DI,0001
--call int 1ah, function 00, which will get a clock tick, to initialize
--out testing value and set in BX
13F2:0149 B400 MOV AH,00
13F2:014B CD1A INT 1A
13F2:014D 89D3 MOV BX,DX
--cal again to get current value
13F2:014F CD1A INT 1A
--subtract stored value from current value. If it is greater, than we have
--surpassed our delay time, and we need to keep moving, otherwise, test again
13F2:0151 29DA SUB DX,BX
13F2:0153 39D7 CMP DI,DX
13F2:0155 77F8 JA 014F
--restore registers
13F2:0157 5A POP DX
13F2:0158 59 POP CX
13F2:0159 5B POP BX
13F2:015A 58 POP AX
--loop back to palette shift beginning
13F2:015B E2D7 LOOP 0134
--do the same thing as the palette shift above, but in reverse order, ramping
--the values up
13F2:015D B93F00 MOV CX,003F
13F2:0160 FEC3 INC BL
13F2:0162 BAC803 MOV DX,03C8
13F2:0165 B00F MOV AL,0F
13F2:0167 EE OUT DX,AL
13F2:0168 42 INC DX
13F2:0169 88D8 MOV AL,BL
13F2:016B EE OUT DX,AL
13F2:016C EE OUT DX,AL
13F2:016D EE OUT DX,AL
13F2:016E 50 PUSH AX
13F2:016F 53 PUSH BX
13F2:0170 51 PUSH CX
13F2:0171 52 PUSH DX
13F2:0172 BF0100 MOV DI,0001
13F2:0175 B400 MOV AH,00
13F2:0177 CD1A INT 1A
13F2:0179 89D3 MOV BX,DX
13F2:017B CD1A INT 1A
13F2:017D 29DA SUB DX,BX
13F2:017F 39D7 CMP DI,DX
13F2:0181 77F8 JA 017B
13F2:0183 5A POP DX
13F2:0184 59 POP CX
13F2:0185 5B POP BX
13F2:0186 58 POP AX
13F2:0187 E2D7 LOOP 0160
--check if there is a key press. If not, keep going, otherwise, stop
--running the program.
13F2:0189 B406 MOV AH,06
13F2:018B B2FF MOV DL,FF
13F2:018D CD21 INT 21
13F2:018F 749E JZ 012F
--reset video mode
13F2:0191 B80300 MOV AX,0003
13F2:0194 CD10 INT 10
--return to DOS
13F2:0196 B8004C MOV AX,4C00
13F2:0199 CD21 INT 21