Tuesday, June 15, 2010

The funniest code comments I ever read!



These have got to be the funniest code comments I have ever read in a while.

// Dear maintainer:
// Once you are done trying to 'optimize'
// this routine,and have realized what a
// terrible mistake that was, please increment
// the following counter as a warning to
// the next guy:
// total_hours_wasted_here = 16


Stay Tuned, and as always, Your comments make my day, so leave them. Cheers!!

Tuesday, May 18, 2010

Quick Update: Linux or CE6


As promised, I’ve got a quick update about the system. I got my board CE6 on the Mini2440out and flashed a build of Windows CE 6.0 that I had lying around. I’m currently evaluating how well CE6 would work for my idea. At this point of time I’m rather inclined towards throwing in a build of Linux like Qtopia or OpenEmbedded and avoid the whole .NETCF thing I had planned. One of the reasons for that is, I’m unable to get a good driver for my OV9650 based CMOS camera module, and I don't want to use the (slow) C# I/O to write a new one. I do already have a driver and DLL written in eVC++ for the module but that is giving me erratic results in CE6 (works great in CE5), so I reason: If I’m using C++, I’d rather use it with Linux than CE6.

So now the question, Qtopia (I think it’s being called QTextended now)or OpenEmbedded? The answer is also a question, which has better community support? Hmmm…

That’s all for now folks. As always, leave your comments, it makes my day. Wave

Tuesday, May 11, 2010

Mini2440 – ARM9 Board


The thing about blogging on your spare time is that you tend to have long absences from it when the spare time gets more sparse. Striaght Face

So, lets pretend that I was too busy to blog the last couple of months (Liar).

Moving on to the more important things, I’ve been having an urge to build a robot for a while now, not just any ol’bot; something which really blows minds out! I mean, I’ve built line followers, maze solvers, wall huggers (really!! that’s a term!), image/object trackers, arms, legs, hexapods and Sumos, but I’m talking something different here.

So, as it happens I’ve got an idea too! I could of course keep talking about what a genius I am Winking, but I’d rather pen my idea down. I propose building a robot, which uses a very small form factor brain, does many (all?) of my common household tasks like fetch the soda, vacuum the room, feed the cat, wake me up from my slumber, play my music, water my ferns and kill the ladybugs in my 7th floor apartment (damned bugs!).

It just so happens, I recently acquired a Mini2440 ARM9 based board (only 100mmx100mm) which should be just right for the job. The reasons I chose my Mini2440 over my trusty and much easier to program NXP mbed module (ARM Cortex M3) is:

  1. While the mbed is great and really easy to program with the mbed library, I need something with some real metal on this one, and the Samsung S3C2440 running at 400MHz just can’t be beat by my 100MHz mbed.
  2. I want to go high level on this one, and when I said high level I’m definitely understating how high I’m talking about. I’m talking Windows CE, and Webcam and possibly .NETCF.
  3. This one was simply the decider. My mbed isn’t available. Yup, its occupied, busy, engrossed, tied up, engaged, industriously absorbed in other tasks (you get the point). My mbed module is currently the part of my object tracking algorithm research project (more on that later), and simply cannot be spared for another six to eight weeks.

So that’s all for now folks, Stay tuned for another installment soon, I’ll post the finalized specifications of the bot very soon (I swear!). As always, leave your suggestions, ideas, comments, rants, death threats or anything else. It always makes my day.


Wednesday, September 2, 2009

Slow on Updates

So, I admit it. I've been slow on the updates this past month or so. I could blame the fact that since I'll be leaving the country soon, I was really busy tying up loose ends (at the office or socially), but why lie. I've been lazy and no excuse. Got lots to write about, currently in the chute are:

+ Tweet-a-heat (tweets the temperature)
+ YAUO (Yet Another USB Oscilloscope)
+ Snake Game Ported to a Lego Mindstorm NXT
+ 3D Scanner using a distance sensor.

Pretty cool, huh? Well unfortunate as it may be, but to write entries for each of these would take some time. So one by one I hope to write about these babies ASAP.

Peace, I'm out.

Saturday, July 18, 2009

Classic Snake Game


Another simple weekend project, the Classic Snake Game. This one should serve as an example for anyone who wants to learn the anatomy of a snake game (or its clones: Nibbles, Worms etc.). The code is open source and available under GNU GPLv3 for use and reuse. Both, the code and the binaries are available for download at the projects Google Code repository. Be sure to leave your view and comments, they always make my day.

Thursday, April 23, 2009

n00b Translator


A simple fun project, which translates L337 to n00b readable English. Open source project, the code is available in the project's Google Code Website. This can serve as a great example for Java text processing.


Stay tuned. :D

Thursday, January 1, 2009

Digital Dice Using ATTiny 2313


Not too long ago, during a game of Monopoly, I thought to myself; "How the hell do I always end up loosing? I bet these dice are loaded!", and right then the geeky idea of building my own digital dice was born.

The Idea was to build a dice that:

  • Produced 100% random result
  • Easy to use, Simple button press and ... result
  • Small enough to carry around with a board game
  • Low power consumption, should run on battery power for long
  • Ensure that every one knows that you are a geek ;)

schematic The hardware is based on an ATTiny 2313 processor, the main features of the hardware:

  • Processor: AVR, ATTiny 2313.
  • 7-Segment LED display (common anode) to display the digits.
  • Thumb Switch to take input from the user
    Thumb Switch
  • Battery as source of power.

So, first I designed the hardware as per the schematics above onto a simple breadboard.

No Problem, simple circuit. I used headers to connect the 7-Segment LED display, this way, I could remove the display as and when required.
Ok, That's done! Now to work on the battery and regulator circuit. Here I decided to use a LM7805 regulator, along with a 1000uF and a 100uF capacitor, to deal with the casual drop in voltage when the circuit starts drawing current. I used common 9V 6LR61 batteries to power the regulator.
For the casing, I got some thin and light ABS Plastic pieces from my throw away waste.
I cut and filed them to a somewhat desirable size.
Drilled a nice hole for thumb switch.

I wrote the code for the processor in assembly, what I am showing here is only the skeleton structure. The complete code is available in the download package.

First is the Reset Vector ,GetButtonPress and the ButtonPressed routines:

   1: .include "tn2313def.inc"
   3: .org 0x0000
   4:     RJMP Reset    //Code Starts from Address 0x0000
   6: Reset:            //Reset Vector
   7:     RJMP Init
   8:     RJMP GetButtonPress
   9: RJMP Reset
  11: GetButtonPress:
  12:     SBIC PORTD, 3        //Skip if Bit is Clear on PD3
  13:     RJMP GetButtonPress    //Relative Jump to Label
  14:     RJMP ButtonPressed    //Relative Jump to Label
  15: RJMP GetButtonPress
  17: ButtonPressed:
  18:     SBIC PORTD, 3      //Skip if Bit is Set on PD3
  19:     RJMP ShowResult      //Relative Jump to ShowResult
  20:     INC    R17                //Increment Register R17
  21:     INC    R16                //Increment Register R16
  22:     CPI    R17, 0xFF       //Compare Immediate R17 and 0xFF
  23:         BREQ ToggleDP //Branch if Equal to ToggleDP
  24:     LDI R18, 0x06     //Load R18 with with Immediate 0x06
  25:     CPSE R18, R16      //Compare, Skip if Equal
  26:     RJMP ButtonPressed//Relative Jump to ButtonPressed
  27:     CLR    R16              //Clear Register R16
  28: RJMP ButtonPressed

The skeleton routine above does not take into account the debounce of the thumb switch. The thumb switch being used is spring loaded, and has a magnetic catch inside it, so the typical debounce would be around ~80ms. Now, running at 8MIPS (internal RC oscillator), in terms of processor cycles, that would be;  1 sec = 8,000,000 instructions; 1ms = 8,000 instructions; 80ms = 64,000 instructions; That means we need to call nop 64,000 times to debounce the switch correctly.

Next we have the ShowResult routine. This basically displays on our 7-Segment display the value of the roll.

   1: ShowResult:
   2:     LDI    R18, 0x01    //Load Immediate R18, with 0x01
   3:     CP    R16, R18    //Compare R16 and R18
   4:         BREQ ShowOne//Branch if Equal to ShowOne
   5:     LDI    R18, 0x02    //Load Immediate R18, with 0x02
   6:     CP    R16, R18    //Compare R16 and R18
   7:         BREQ ShowTwo//Branch if Equal to ShowOne
   8:     LDI    R18, 0x03    //Load Immediate R18, with 0x03
   9:     CP    R16, R18    //Compare R16 and R18
  10:         BREQ ShowThr//Branch if Equal to ShowOne
  11:     LDI    R18, 0x04    //Load Immediate R18, with 0x04
  12:     CP    R16, R18    //Compare R16 and R18
  13:         BREQ ShowFou//Branch if Equal to ShowOne
  14:     LDI    R18, 0x05    //Load Immediate R18, with 0x05
  15:     CP    R16, R18    //Compare R16 and R18
  16:         BREQ ShowFiv//Branch if Equal to ShowOne
  17:     LDI    R18, 0x06    //Load Immediate R18, with 0x06
  18:     CP    R16, R18    //Compare R16 and R18
  19:         BREQ ShowSix//Branch if Equal to ShowSix
  20: RJMP ShowResult

One more important consideration here is Low Power Mode, and automatic turn off (We are running on batteries here, after all). In the final code, I have included a low power and auto turn of routine. This checks to see if the dice is not used for 4 Seconds or more, and then turns off the display and puts the processor and other peripherals in a low power state. See the video of operation below to get a good idea of how this works.

Ok, thats it!! All Done now!!
And it Works!! :D

Video of Operation:


Thats all folks, Untill Next Time!! If you like this entry, Leave a message and Digg this article. Messages always make my day. :)

Sunday, November 9, 2008

The heights of geekiness

Geek noun \ˈgēk\: Geek  Used in a nice way it is someone who seems to know everything there is to know about computers.

If I were to define myself as a geek, I would say:

   1:    -----BEGIN GEEK CODE BLOCK-----
   2:    Version: 3.12
   3:    GED/J d-- s:++>: a-- C+++(++++) 
   4:    ULU++ P+ L++ E---- W+(-) N+++ 
   5:    o+ K+++ w---  O- M+ V-- PS++>
   6:    $ PE++>$ Y++ PGP++ t- 5+++ 
   7:    X++ R+++>$ tv+ b+ DI+++ D+++
   8:    G+++++ e++ h r-- y++**
   9:    ------END GEEK CODE BLOCK------

(don't understand the above? refer to The Geek Code v3.12, Yup, we have one) smile_nerd

Even as a geek, there comes a point, where you need to draw the line. One such point came today. I found myself facing a dilemma, unable to decide, "How to get bored today". After much deliberation I found that I simply could not decide between five things to do. Under these circumstances I decided a coin toss would be ideal. This of course brought up another question how best to decide between 5 choices using a coin. At that time a brilliant geeky idea came to me, and I decided to whip together a "Random Choice Maker"


Took me 20 minutes to whip together this simple utility, I wrote a simple RNG function it went like:

   1: class myRandomizerClass
   2: {
   3:     public int generateRandomValue(int uValue)
   4:     {
   5:         int millisecondsNow = DateTime.Now.Millisecond, breakIndex = 1000/uValue;
   6:         return millisecondsNow/breakIndex;
   7:     }
   8: }

It uses a simple uValue for checking quantile density.

So, that's it. Once again I ended up prooving that "Once a geek, always a Geek"

Cheerio. smile_tongue

Sunday, September 28, 2008

Digital Thermometer



Not too long ago, I took the plunge into the world of Linux. Only to find myself constantly requiring to skip down to the command line to perform even some of the simplest of tasks. So when I learned about KontrollerLab, I was itching to get creative with it, and the "Digital Thermometer Project" is born.

Well to be quite honest, building a digital thermometer is a no-brainer. But I really had to do some building, or die of dull boredom.

Those of us familiar with Unix/Linux (and other Posix based OSes) would understand why I felt frustrated when I had to keep getting down to the command line to do a simple $avr-gcc -mmcu=atxxxx -O0 -c /Projects/xxxxx/xxx.c. I wrote a few scripts to automate a few tasks, but that wasn't sufficient. When I learned of KontrollerLab IDE I wasn't too hopeful, I tried it not expecting much. Frankly, I was blown out of my mind. It was excellent! It is comparable to almost any good commercial Microcontroller Development IDE. Since it works with avr-gcc, it would be compatable with most of your existing code for the AVR.


Though it may seem that way, but this post is not about the development tool I have been emphasizing about so far! So lets discuss what we this post is really about. This weekend I'm building a Digital Thermometer.

Here is what we need.


* Soldering Iron - Check.
* Random Wiring - Check.
* Some Discreets (Resistors, LED's etc.) - Check
* Headers and Sockets - Check.
* Microcontroller (ATMega32) - Check.
* LCD - Check.
* LM35 - Check.

* Geeky Brain - Check. ;)

OK, Good to go :D.


Now, Some may be wondering why I preferred the National Semiconductor LM35 over the much easier to use Dallas DS18x20. Well, for starters, I have a huge load of LM35's lying with me (remnants of an old project :P), and the second reason is, the LM35 is analogue, and pre-calibrated (yes, so is the DS18x, I know). And lastly since the LM35 is linear (10.0 mV/°C) it makes for easy calculation of the temperature from the sensor voltage. The sensor voltage is sampled using the ADC of the ATMega32. I wrote a C function to sample the ADC, it goes like:

void readAdcValue(int channel)
while ((ADCSRA & 0x10)==0);
adcVal = ADCW;

For those not in the know, basically the function is using the ADMUX register to select the ADC channel that we are using, then we trigger the ADC Conversion using the ADSC (ADC Start Conversion) bit (bit 6) of the ADC Control and Status register (ADCSRA, we OR it with 0x40 i.e 0b 0100 0000). We then wait for the ADIF (ADC Interrupt Flag) to get set, then we sample the ADC value using the ADCW (ADC Word). ADCW is *not* a register. It is basically a combination of the ADCH and ADCL register. Remember the ATMega32 is an 8-bit processor, so all registers are 8-bit, and since the ADC is 10-bit, the ADCH (ADC High Value) and ADCL (ADC Low Value) registers are used.

Ok, Now we have an ADC reading, we have to convert that into a Sensor Voltage value and then convert that into a temperature.

void showSensorVoltage(void)
char* buff;
lcd_puts("Sensor Voltage:");
sensorVoltage = (adcVal * 1024)/5;
buff = intToString(sensorVoltage);

void showSensorTemp(void)
char* buff;
sensorTemp = (sensorVoltage * 500)/1024;
buff = intToString(sensorTemp);

I use the equation,

sensorVoltage = (adcVal * 1024)/5;


sensorTemp = (sensorVoltage * 500)/1024;

Ok, the code is divided into the following functions:

void init(void);
void showWelcome(void);
void showAdcValue(void);
void showSensorVoltage(void);
void showSensorTemp(void);
void readAdcValue(int);
char* intToString(int);
int main(void);

You can get a more detailed Idea by looking at the attached code.

Ok, Time for the hardware assembly. I designed a schematic for the whole system:


After hours of assembly and debugging:



And it works!!!dsc00027


Case Modding:

Ok, so now I need to make a case for this thing. I got a standard SMPS casing (again, leftovers from an older project) lying around.


The size is more or less right, but the insides are a bit messy


Anyhow, after some work, I managed to fit it all.... Not too neat. But not unacceptable either.





Thats all folks!!

Stay Tuned! :)

Non-Geek: My Friends Blog



Well Guys long time since I posted anything around here, Its time to clean out the cobwebs and move in again. :)

To start out, my Friend, and Clan member the one and only Mr. Riteek "VoLdEmOrT" Arora is on Blogger now, I'm sure all the Geeks and even the Non-Geeks (do we get that kind around here? ;) ) would enjoy his blog. His interests involve around F1, Linux (apparently Harry Potter Characters ;) ), well....You read his blog.

Pictured above: Mr. Riteek "VoLdEmOrT" Arora

Visit his blog: RiTeEk "VoLdEmOrT" aRoRa'S Blog


Cheerio!! PS. A geeky entry is coming soon....Stay tuned!

Sunday, April 20, 2008

TicTacToe Bot (Part 2)

PART-2 : The TicTacToe Robot Controller


Now, that we have an algorithm working, what's next? Good question. Lets take a look again at the block diagram of our electronics:


Roughly based on the above I whipped together a Schematic:


Basically a Atmel (AVR) ATMega32, with a 2x16 Character LCD,  a Keypad Matrix,  and A set of headers for interfacing with the motion driving circuitry and sensors.

Step 1: Preparing the firmware.

This was main part, apparently porting the windows 'C#' code to plain 'C' wasn't all that simple, after all. Well it would have been , except:


So, I did a bit of optimization, and then some more, and then a LOT more. A poem would explain better:

"Till I had reduced each int to byte,

and byte to bit,

Till each hardware register was consumed,

Till each register could be used,

or Re-Used no more,

and Lo... I present to you the 'Optimized Code-Size'" smile_shades


This I'm particularly proud of. clap

Anyway, the code is divided into 5 Files:

  • main.c
  • game.h
  • game.c
  • UserInterface.h
  • UserInterface.c

Program Structure

Step 2: Preparing the hardware.

After getting the code "Out of the way", it was time to do what I really wanted to do this weekend. Time to get some Solder Iron Burns and some spit, wire and breadboards, together!!

Lets See what we need first. Hmmm....


-Solder Iron. Check.

-Bread Board. Check.

-LCD. Check.

-Microcontroller. Check

r step -Random Headers and Stuff. Check.

-Some Discreets. Check.

-Lots-a-Buttons. Check.

All clear. Lets go....thumbs_up

One Hour Later:


Everything assembled to my satisfaction, and ready for testing. fingerscrossed


It Works!!! It Works!!!

Some random screens:



Scrolling Up and Down the Menu:




IMG_0126 IMG_0127

IMG_0131 IMG_0133 IMG_0134

Preparing the Outer Box: (I wish I had taken my art and crafts class in High school seriously smile_thinking


Ooohhh the guts and wires.....IMG_0136

The Front Panel: (Before I got creative with a felt pen smile_wink)

IMG_0137  AAaahhh Finished.... clapthumbs_upbeer




Still Works....


Under the hood: Easy Access to the Microcontroller, and I/O Headers. I know, I'm a genius. smile_teeth

IMG_0138  So, Thats it for now. What about the rest of the bot? Well the main part is over, I mean, the Code works on the Mega32, is now optimized, quite a bit, and I spent an entire long weekend (Long Weekend = Friday + Saturday + Sunday) on it, so it may be atleast another 3-4 weekends that this project will be on hold, so that I can work on other interesting projects. Nothing to be sad about... Next week, I'm working on Panasonic Servo based gantry (Ball Screw), with a Xilinx Spartan XC3S400. Nice....


If you like this post, leave a message, it always makes my day.smile_regular