Sunday, July 7, 2013

Atmel Microcontroller Plays the Tic-Tac-Toe Game

A couple of years ago I made an electronic Tic-Tac-Toe game with  Pololu baby Orangutan controller as the central component. Pololu  is an excellent device  but for Tic-Tac-Toe game it was an overkill. Its motor drivers were of no use and  it required  at least 5 V power source, so I had to add an adapter to make device workable on two batteries. Recently I decided to gain some experience with bare bone Atmel controllers and repeated this project with ATmega 168. Setting  this controller to the lowest possible frequency (1 MHz) allowed me to eliminate the power adapter. (Two AAA rechargeable batteries provides 2.4 V, which is more than enough for ATmega 168 at this frequency). Here is the circuit diagram of this toy.



Let us look at the list of components:


1. All electronics were mounted on a RadioShack component PC Board 276-168.
2. Central processor Atmega 168 (do not remember where exactly I bought it, many online providers sell it)
3. Nine Bi-polar bi-color 5mm Green/Red LEDs. (again I did not keep the record of purchase,  some online providers sell it)
4. RadioShack 2 AAA battery holder  270-398
5. Nine resistors 51 Ohm 0.25 W (0.125 W is OK as well)
6. Panasonic  EVQ-WTEF2515B Encoder (goldmine-elec-products.com; alas, looks like they do not sell it anymore)
7. Ceramic capacitor 0.47 uF.
8. RadioShack slide switch 275-409
9. Male headers 2.54 mm to make six contacts ISP header. SItes pololu.com or hobbyengineering.com sells various types of them.
10. RadioShack 2-pack 14-PIN IC socket (optional, just to have the possibility to reuse controller  on other projects).
11.Hook-up 26 gauge wire ( hobbyengineering.com ) and 30 gauge precut wire  (Frys electronic store).





As  an enclosure  I used a box from Johnson&Johnson emergency kit. I had to treat edges of the board with file to make it fit into the enclosure.


As you can see the density of project components on the boards is not high.  Soldering elements  to the board and connecting it with 26 gauge wire was not that hard. To find out how LED was oriented on the board follow the simple rule: top of any LED on my diagram corresponds to the longer LED leg.  Contacts of panasonic encoder are very fragile  so I used 30 Gauge wire here. The PC board layout does not provide the proper place to solder ISP header. So I have to separate  three contact plates with utility knife and solder wire directly to contact plates occupied by headers.




Of course, the main part here is the program. For machine to choose the right move there is a well known algorithm: game tree search.   There are many sources to look for. In particular I have used this Berkeley lecture: http://www.cs.berkeley.edu/~jrs/61bf10/lec/18.pdf and strictly followed the described here game three search with simple pruning algorithm converting pseudo code into the C language. The only deviation from the algorithm  I did during programming  of the first  computer move. If computer starts the game it chooses randomly among central and four corner squares. If computer does not start the game it makes its first move in to the center if the center is  free  or chooses randomly any corner square if center is occupied. There are two reasons for such deviation. Making a move when the board is  empty means too many choosing attempts (hundred thousands) and it will take a lot of time  for a low frequency controller to calculate it. In addition introducing some randomness makes the game on the computer side less predictable and boring.


Source code for the toy is available here https://bitbucket.org/jumbleview/three-in-a-row/src/


After compiling code with free Atmel Studio 6.1, the resulting hex file was written into the controller flash memory with the help of Atmel ISP MKII (I bought  mine at mouser.com )


Let us look at the compiler printing regarding the program memory allocation:

Program Memory Usage : 3548 bytes   21.7 % Full
Data Memory Usage : 57 bytes   5.6 % Full


Based on that we can conclude that  after recompilation the code will work on any of controllers from the line:  Atmega48, ATmega88, ATmega168, ATmega328.


And finally here is how the Atmel controller plays the Tic-Tac-Toe game:



Saturday, June 22, 2013

Competition in Uselessness

It is believed that the idea of the Useless Machine was first formulated by Marvin Minsky and implemented by Claude Shannon more than half a century ago. Nowadays this concept is quite a popular one (1). The Useless machine in one sentence may be defined as automaton with only two possible states: 

  1. It is turned off. 
  2. It is turned on.

Transition from state 1 to state 2 happens as a result of external force, and the transition back from state 2 to state 1 is provided by the machine itself. If this machine is electrical (as Claude Shannon built it) energy for the transition is taken from the battery charge. For a purely mechanical machine it is either gravity or spring tension. In its classical implementation the electrical circuit looks like this:
I first got knowledge about that device from Make Magazine (2). Until the time of publication there were tons of videos on YouTube with various versions of this device. While browsing them I paid attention to the fact that most of the machines use a toggle switch as the machine control element. A rocker switch was never used. But it is absolutely clear that a rocker switch may provide the same effect. Why does nobody use it? Good opportunity to contribute something new to an old concept, right?

Using a rocker switch instead of toggle means that the force applied to the switch by the machine arm should go not in a horizontal direction (from side to side) but rather in a vertical direction (from top to bottom). While playing mentally with kinematics to make it workable one more thought came to my mind: why not eliminate the working arm? Let the box lid do the job. This way the machine will be truly different from other animals in the herd: rocker switch instead of toggle and no arm at all.


It took me some time and several attempts to create a two dimensional cardboard model like this:


As soon as my model proved the concept worked, the actual implementation with wood and wires was not so hard :


And here are some technical details:

  • Motor: Solarbotics GM2 gear motor
  • Rocker switch: Radioshack DPDT Switch ( 275-695 ),
  • Micro switch: Radioshack SPDT Switch with Roller Lever (275-017)
  • Wooden box and other wooden parts from the local Michael’s store.
Among the comments left on this YouTube video was a comment from the user xsolarwindx: “this is f*ing stupid”. For any other occasion such a comment would be insulting but for the useless machine maker that could be considered as highest degree award. I think everybody would agree, right?

Updated 10/27/2015.
I have published more detailed description of this project updated version here: http://www.instructables.com/id/Useless-Machine-in-the-Pumpkin-Mask/



References:

1. Here is the site of the company entirely dedicated to useless machines :


2.  Brett Coulthard. '"The most useless machine" (Issue 23 of Make Magazine). 2010

Sunday, May 5, 2013

How did I Join the Uprising



Updated: 01/12/2015. New version of this project you can see here .

In March 2013 letter from Make magazine there was summary: why people are concerned with DIY. Among reasons economy  was mentioned: “Making is often cheaper than buying”. From my experience it is seldom true. In our highly specialized society making things cheaper than you can buy them is quite a rare case. But it still happens sometimes. Project described here is an example.


People who work in the office or from home spend most of their active time sitting: while working, in the car or in the bus/train during commute. It would be much healthier to switch from sitting to standing position from time to time while working . I looked what kind of furniture can help me to do that. Indeed there are some vendors producing sit-n-stand stations. Especially I liked the advertisement from Ergotron company The Uprising: JustStand! .  The ad  is pretty smart and funny,workstation is doing exactly what’s needed, allowing to change from sitting to standing position without job interruption. While such a  workstation is not cheap (~330$ - 420$) it is still affordable. But when I evaluated  its functionality I came to conclusion that it is possible to  implement it differently with almost the same user experience and for less money. I did  couple of visits to the local hardware stores, revised what I have in the attic, and dedicated two weekends to make adjustable monitor/keyboard station .


These photos show my station at the bottom position and when it moved to the top.



Here is the drawings which reveals details about  inner construction. Monitor desk is integrated with keyboard desk. To the sides of the desk attached inner parts of sliders. Outer parts of sliders attached to the side walls of the tower. The desk can move into vertical direction up and down together with  LCD monitor and keyboard on it. Tower is set on the top of the table and  walls of the tower is tied to the table with metal corners providing the needed stability. When desk is at the bottom position it rests on the table, two hinges attached to the edges of tower walls are turned outside.  While at the top position desk rests on the two hinges which now are turned inside.

 
Important part of the design is counterweight system. Weight of the desk with monitor, keyboard and mouse reaches 17 pounds. It is too heavy to easily move it (especially down). Four pulleys together with rope and 5 pounds weight  creates system which compensates most part of the desk weight. Pulleys 1,2 and 3 creates  3 times mechanical advantage . Pulley 4 changes force direction. As a result 5 pounds weight creates 15 pounds counterweight.  This is not really a high tech but what worked for Archimedes 2300 years ago may work for me as well, right?

And here is how actually pulleys 1 and 3 mounted:


Let us calculate price of the project.

1. Pair of draw sliders: $14.99 (Ace Hardware)
2. Two pairs of metal patio door wheels (to use as  pulleys): $6.99*2=$13.98 (Ace Hardware )
3. Fourteen metal corners: $0.69*14=$9.66
4. Two shelf tablette 35” long to make tower walls: $8.99*2=$17.98 (Eames local hardware store)
5. One shelf tablette 23” long to make  keyboard desk: $5.29 (Eames local hardware store)
6. Pair of hinges: 2.99 (Ace hardware)
7. Two C-clamps to tie the LCD monitor to the monitor desk: $1.99*2=$3.98 (Ace hardware)
8. Pair of wooden knobs for hinges: $1.99 (Ace hardware)  
9. Five pounds training weight (used): $2.99 (Play-It-Again store).

So the whole price of the project would be:

$14.99+$13.98+$9.66+$17.98+$5.29+$3.98+ $2.99 + $1.99 = $72.87

Some stuff I found in my attic (leftover from previous projects),namely:
a) wood for tower roof and monitor desk;
b) metal rods to attach keyboard desk to monitor desk;
c) soft material to wrap up weight so it will not hit room wall while moving (I have used old mouse pads here);
d) some screws and Elmer glue.  
I am not sure what will be the cost of this stuff if I need to buy it,  probably $25-$30.

I would tell that for DIY this project is rather  expensive but anyway the whole price is under $100. Saving compare to Ergotron workstation (which is cheapest among the available on the market)  exceeds $200.


And eventually here is the video which shows the process of bringing the  monitor desk from the top position to the bottom and then back to the top.


I have used this station for more than month and I am quite satisfied with it.  Maybe it does not look so neat as Ergotron but it meets my expectations. Having sit-n-stand station at home give me a reason to telecommute more often (Can do that,  I do not work for Yahoo).