Reality Blows: Strange LCD Problem and Strange Solution.

I spent the better part of two hours struggling with your average HD44780 driven 16x2 character LCD, even though I was using the LiquidCrystal library that comes with the arduino environment. My problem was the text I was printing out looks like this:

I tried a lot of things, like adding in delays here and there, even going so far as to use a subclass of LiquidCrystal which inserts a 2ms delay after each write to no avail. Then by a process of elimination, I found that if I reverse the order in which the lines were printed, i.e. print line 1 then line 0, the problem goes away:

Whiskey Tango Foxtrot.


Order Matters With XCode's Build Phrases

I use a custom script to insert the current git commit into GeoNote, so when I get bug reports I have a better idea of which version the user is running. The script is as below:

import os
from Foundation import NSMutableDictionary

version = os.popen4("/sw/bin/git rev-parse --short HEAD")[1].read()
info = os.environ['INFOPLIST_FILE']
print info
plist = NSMutableDictionary.dictionaryWithContentsOfFile_(info)
print plist
plist['revision'] = version[:-1]
plist.writeToFile_atomically_(info, 1)

This was added as a Run Script Build Phrase. The problem I noticed was that the commit short hash inserted into Info.plist was always one commit behind. After some head scratching, I realised this was because by default the new build phrase is inserted last, and the order matters! It isn't actually possible to reorder build phrases by drag and dropping the children nodes around. You have to do a head-insert by dragging a child to the parent, which inserts it at the top.

Voila, problem solved.



Don't Worry About the Pirates.

So you are an indie musician, and you are worried about pirates – those naughty people who are allegedly stealing food off your table and from the mouth of your children.

Well I am here to tell you there is no need to worry – pirates may just be the best thing to ever happen to you.

First, let me tell you something that you probably don't want to hear – you are nobody. You are nobody in the sense that 99.9999% of the people on this planet has never heard of you. The only way to change this is to get your music into the ears of as many people as possible

Big artists do this – they have TV spots, radio spots, newspaper reviews. These are all ways to reach listeners without them paying anything.

This is also what you need to do, and pirates do this very well. If a single pirate distributed your music to two other pirates, and they in turn distribute to two more pirates and so forth, in a very short time you would have reached all of western civilisation.

Now I can see you getting puffed up, about to yell at me how this doesn't make you a dime. Just give me a second, will you?

The second thing I am going to tell you is that to a first approximation, you make money only from your fans. Remember that the word "fan" comes from fanatic – they are people who bought the Fellowship of the Ring when it first came out on DVD, then bought the Director's Cut, then bought the Special Limited Collector's Tin Box Edition , and then the Super Mega Ultra Edition that came with a Gandalf bust, then they did it again for the Two Towers, Return of the King, then for the whole damn series. These are people who have 12 versions of each Lord of The Rings trilogy, and will purchase the anniversary edition when it comes out in 2012 anyway.

The point is: your fans love you, they will buy your music, your merchandise, and go to your concerts even if they can get it for free – that is just how fans are.

Now the only way to get fans is to have people listen to your music, and as long as you have a non-zero conversion rate of joe-listener-to-fan, you come out on top. The more people you reach, the more fans you get, and the more money you make.

Just in case you don't get it yet, here it is in point form:

  • no one has heard of you
  • if no one has heard of you, you have no fans
  • you make money from fans
  • people become fans from listening to your music
  • pirates distribute your music to millions, even billions of people
  • this increases your fanbase
  • a larger fanbase means more money
  • a large enough fanbase means you have Made It Big

Got it?


Latching Power Supply With Electronic Turn Off

This circuit has (as far as I can tell), 0 off current, and 17mA on current. It is latched by the momentary push button, and can be turned off by applying >0.7V at the input as shown. It is intended for use with embedded interactive installation (e.g. an arduino) where the user pushes the button to turn the device on, and the device will turn itself off after some time to conserve battery.

It is a modified version of this circuit.




Nice Work Australia Computer Society

You came to me during my first year in university, and sold yourself as the paragon of virtue and integrity - the kind organisation I would be foolish not to be associated with if I want to get anywhere in Australia doing software, or "Information Technology" as they call it now days.

I didn't join then, because I didn't have the money. I didn't join later because my career focus shifted away from software. I won't join now ever, because you have sold out.

I am referring to the honorary membership you awarded to none other than one Stephen Conroy, Internet Villain of the Year, 2009.

You had such nice things to say about him too:

“We are very pleased to honour Senator Conroy’s contribution and support of the significance of ICT to the economy and the key role of ICT professionals in Australia’s future,” said Mr Wells.

If I am so inclined as to read between the lines, I can't help but get the feeling you are thanking Mr. Conroy for pushing the Internet filter scheme, and in the process provided jobs for the programmers and technicians involved in the various trials and consultations.

“Senator Conroy has always encouraged the ACS in its role as the independent voice of the ICT profession, welcoming our input to various enquiries and working groups, and regularly attending key ACS events. We are grateful for his on-going support,” Mr Wells said.

I don't think Mr. Conroy is listening to your input, or learning from your events - he continues to believe filtering the Internet is doable, and not a waste of time and resources.

I reject you, Australia Computer Society, as "voice of the ICT profession". Your actions are deplorable and shows a lack of integrity. If I was a member, I would be ashamed.




Yet Another Arduino Float Print Function

Note: in arduino-0017, floating point printing is supported by default. The function below is not necessary.

void floatPrint(float f, int places=6)
        int _d;
        if (f < 0)

        _d = (int)f;
        if (!places)

        Serial.print(_d, DEC);

                _d = (int)f;
                Serial.print(_d, DEC);

void floatPrintln(double f, int places=6)
        floatPrint(f, places);
Why another float print function? The ones I found wasn't too nice, one of which required long integers. Yuck. It was also fun, and now I know where to look for one in the future :P


A subtle source of linker errors under XCode

If one source file is sourcecode.c.objc and another is sourcecode.cpp.objcpp, you will have problems if you try to call function defined in one file from the other. The resolve this either make them the same source type, or follow this guide.

This drove me nutty because the template I was working off has code set to sourcecode.cpp.objcpp, but XCode adds new classes as sourcecode.c.objc! To check the file type, use "Get Info" in the source file's context menu.





I generated a gpg keypair for myself today, and I was looking for a fairly safe way to back it up. I don't particularly trust DVD/CDs, and keeping it on flash is even more worrying. I wanted a means of backup I can see and touch.

Paperbak would be great if it was ported to something not windows. Since it wasn't, I settled on QR Code.

Thus qrbackup was born. It will base32 encode a file, then encode it into QR codes using google chart service.

I have tested it from backup to restoration, and it works. YMMV, more instructions available after the jump.



P.S. Pardon my python.


git-daemon on debian vserver

Annoyingly git-daemon-run requires runit on debian, but runit will fail to install properly in a debian vserver because it doesn't have init.

One solution is to reconfigure vserver to use plain init style.

However I didn't want to this because I don't want to take down my vserver just yet. So here is the required line for /etc/inetd.conf:

git stream tcp nowait nobody /usr/bin/git git daemon --inetd /var/git-repos




Homeopathic logic

On Dr Karl on Triple J Podcast recently, and a lady called in (lets call her B) to say homeopathy works for her. She said her children were never vaccinated, and only receives homeopathy treatments, just like all her friend’s children, and that they are all healthy. B thus concluded that homeopathy works, and to show she isn’t the only one who thinks so, she presents the Royal family, who practices homeopathy, as a supporting fact.

It would appear at first she is right: homeopathy works and the facts are compelling - but a little critical thinking goes a long way.

Firstly, the only logical conclusion one can draw from the facts presented isn’t homeopathy works, but homeopathy isn’t fatal.

To arrive at B’s conclusion one would need to:

  • give one of her children placebo

  • give one of her children prescribed medicine

  • give one of her children nothing

  • give one of her children homeopathy

Further, one would need to observe the child given homeopathy doing better than the other children to show homeopathy:

  • isn’t a (costly) placebo

  • works better than prescribed medicine

  • isn’t harmful

These kind of things are done in clinical trials, and no clinical trial to date has shown that homeopath has anything more than a placebo effect.

So what about the Royal Family? Surely, you think to yourself, this lends homeopathy the weight of authority. However this is a logical fallacy known as “argument from authority” or “appeal to authority”. Simply put, just because some one in authority says it is true, doesn’t mean it is. After all, we were told there were weapons of mass destruction in Iraq...

It would appear most adults do not engage critical thinking: to look at what facts are presented, evaluate how reliable they are, and judge claims based on those facts. Even a cursory examination of the principles of homeopathy shows it runs contrary to, and has no basis in, reality. Yet this makes no difference to some people, who latch on to any fanciful tale as the truth as long as it makes them feel better, or it aligns with their world view.

In a world populated by scientologists, perhaps it is unsurprising we find fervent supporters of homeopathy. I can only hope that natural selection does its job.




Dealing with rkhunter warnings

rkhunter often warns on file property changes after upgrade and such, and sometimes you just aren't sure whether it is due to recent upgrades, or because you really were compromised. The following script was written to compare the checksum of all files rkhunter warns about against the originals in a debian repository.

The latest version of this is available in my script.git respos.

This script will verify whether files for which rkhunter has logged a
warning for is still valid. It does this by finding which debian package
it came out of, and downloads them, unpacks them, then checks
the checksums.

Run it by supplying a rkhunter log file as first argument


function find_suspect_files
echo "parsing $1 for suspect files" 1>&2
grep -1 Warning "$1"| grep File | sed 's|.*File: ||'

function find_packages
echo "finding packages" 1>&2
for suspect_file in $1
package=$(dpkg -S $suspect_file|awk '{print $1}'|sed 's/.$//')
echo "suspect file $suspect_file found in $package" 1>&2
echo $package


function make_aptitude_args
echo "generating aptitude arguments" 1>&2
for package in $1
version=$(dpkg -p $package | grep Version | awk '{print $2}')
echo $package=$version

function cleanup
echo "cleaning up"
rm -rf tmp
exit $1

function setup
echo "setting up"
rm -rf tmp
mkdir tmp
pushd tmp

if [ $# -ne 1 ];
echo "$desc"
exit 1

suspect_files=$(find_suspect_files "$1")

packages=$(find_packages "$suspect_files" | sort | uniq)

if [ -z "$packages" ];
echo "***WARNING****"
echo "No packages contain any of the suspect files!"
cleanup 1

aptitude_args=$(make_aptitude_args "$packages")


echo "downloading packages"
aptitude download $aptitude_args 1>/dev/null
if [ $? -ne 0 ];
echo "aptitude download failed!"
echo "args=$aptitude_args"
cleanup 1

echo "unpacking"
for deb_file in *.deb
ar -x $deb_file
tar zxf data.tar.gz
rm -rf data.tar.gz control.tar.gz

for suspect_file in $suspect_files
if [ ! -f ".$suspect_file" ]
echo "***WARNING****"
echo "For some reason .$suspect_file does not exis!"
echo -n "verifying $suspect_file... "
suspect_sum=$($HASHER $suspect_file | awk '{print $1}')
clean_sum=$($HASHER ".$suspect_file" | awk '{print $1}')
if [ $suspect_sum == $clean_sum ];
echo "OK"
echo "***WARNING****"
echo "Checksum mistmatch for $suspect_file!!!"
echo "Should be: $clean_sum"
echo "Is: $suspect_sum"




microbric viper review

The microbric viper is neat. Good quality parts and unique idea. Makes a decent robotics platform if you get the wheel add-on. However, you gotta have small fingers to get some of the parts in place. Despite this, the hardware is solid, I like it. The one thing I would ask for however is more short-nuts and a printed manual, not a CDROM with a PDF. Take a leaf from LEGO and their construction manuals.

While the hardware is decent, the microbric viper is sadly let down by the software.

The microbric viper uses the basicAtom (by basicmicro), a PIC 16F87{6,7} with a custom bootloader. Now there is nothing wrong with this - arduino uses a custom bootloader too. However the custom bootloader uses a proprietary programming protocol. This is pretty fail, but what really fails is the programming software only runs under windows (or wine under ubuntu, but only for now).

IMHO the basic-esque language used by basicAtom is no better than what picaxe offers. I am completely at a lost as to why companies would use the basicmicro's products and lock themselves to a single supplier. Think about it: if basicmicro goes bust, your products using the basicAtom will not longer have a supported development environment.

Robotics companies need to seriously consider how their selection of controller will affect their customers - specifically those customers who aren't going to be running windows and staying with in the limits of whatever custom language designed by the controller vendors.

Arduino would be the best choice IMHO. Open hardware, open software. You don't have to pay premiums for the bootloader, and the number of people who will consider your product increases to include people like me.

I bought the microbric viper because it was on sale: reduced to $29 from $199. If I had known I could only program it under windows or that it used such a closed platform, I won't have bought it, even for that price.




New addition to the work bench

Thanks to Anthony from Area I.T. & T, I am now a happy owner of a dual channel 40Mhz oscilloscope.

I am happier than a pig in mud!



Sketch to calibrate SEN-08663

Got my hands on a ADJD-S371 on a breakout board from Sparkfun. The code below can be used to calibrate it. The most up-to-date version of the code can be found at my git repository under colour_sensor_calibration.

#include <Wire.h>
/* Calibrates the sensor to get white balance. Pin 2 should be connected
* to LED on the breakout board's LED pin. Calibration is done by placing the
* breakout board inside a pin pong ball, and using the built-in LED for
* illumination.
* Amount of light is measured by charging N capacitors for time X then
* reading off the voltage. (Conjecture)
* N is controlled by CAP_XXX
* T is controlled by INT_XXX
* Calibration is done by adjusting the integration time. No real reason.
int _slave_id = 0x74;
int _LED_pin = 2;

uint8_t read_register(uint8_t addr)
i2c_send(_slave_id, &addr, 1);
return i2c_read(_slave_id);

void write_register_int(uint8_t addr, int data)
write_register_multibyte(addr, (uint8_t*)&data, 2);

/* write data[i] = register+i */
void write_register_multibyte(uint8_t addr, uint8_t* data, uint8_t bytes)
for (int i = 0; i < bytes; ++i)
write_register(addr+i, data[i]);

void write_register(uint8_t addr, uint8_t data)
uint8_t bytes[] = {addr, data};
i2c_send(_slave_id, bytes, 2);

uint8_t i2c_read(uint8_t id)
Wire.requestFrom(_slave_id, 1);
for(int i = 0; i<10 && !Wire.available(); ++i, delay(10));
if (!Wire.available())
return 11;
return Wire.receive();


void i2c_send(uint8_t id, uint8_t * data, uint8_t len)
for(int i = 0; i < len; ++i)

#define CTRL 0x00
#define CONFIG 0x01

#define CAP_RED 0x06
#define CAP_GREEN 0x07
#define CAP_BLUE 0x08

#define INT_RED_LO 0x0A
#define INT_RED_HI 0x0B
#define INT_GREEN_LO 0x0C
#define INT_GREEN_HI 0x0D
#define INT_BLUE_LO 0x0E
#define INT_BLUE_HI 0x0F

#define DATA_RED_LO 0x40
#define DATA_RED_HI 0x41
#define DATA_GREEN_LO 0x42
#define DATA_GREEN_HI 0x43
#define DATA_BLUE_LO 0x44
#define DATA_BLUE_HI 0x45

int read_colour(uint8_t low_addr)
int lo = read_register(low_addr);
int hi = read_register(low_addr+1);

return lo|(hi<<8);

int red_integration_time = 2048;
int green_integration_time = 2048;
int blue_integration_time = 2048;

void set_integration_times(int red, int green, int blue)
write_register_int(INT_RED_LO, red);
write_register_int(INT_GREEN_LO, green);
write_register_int(INT_BLUE_LO, blue);

void setup()
pinMode(_LED_pin, OUTPUT);
digitalWrite(_LED_pin, HIGH);

Wire.begin(); // join i2c bus (address optional for master)
Serial.println("Setting up...");

// datasheet says, wait 10us for hardware reset, so lets wait 1000

// gain setup
write_register(CAP_RED, 0x08);
write_register(CAP_GREEN, 0x08);
write_register(CAP_BLUE, 0x08);


// ask for colour data and offset
write_register(CTRL, 0x01);

void loop()
if (read_register(CTRL))

int red, green, blue;
red = read_colour(DATA_RED_LO);
green = read_colour(DATA_GREEN_LO);
blue = read_colour(DATA_BLUE_LO);

Serial.print("red: ");Serial.println(red);
Serial.print("green: ");Serial.println(green);
Serial.print("blue: ");Serial.println(blue);

Serial.print("red_int: ");Serial.println(red_integration_time);
Serial.print("green_int: ");Serial.println(green_integration_time);
Serial.print("blue_int: ");Serial.println(blue_integration_time);

// have to calibrate against blue, because LED has a blue bias otherwise
// it would look like blue has high gain than it does
float P = 1;
int reference = blue;
red_integration_time += (reference - red)*P;
green_integration_time += (reference - green)*P;
blue_integration_time += (reference - blue)*P;

// set the new integration times

// ask for colour data again
write_register(CTRL, 0x01);




Facebook python authentication gateway

Edit: it occurred to me what I have below is the basics of a thin facebook api wrapper. I might make it into one at some point in the future.

If you don't know what this does, you don't need it. Hope this helps some one. Written because pyfacebook is broken, always returns error 100.


def get_session(auth_token):

sorted = params.items()
sorted.sort(key=lambda x:x[0])

str_to_hash = ''.join(["%s=%s"%(x[0], x[1]) for x in sorted])
str_to_hash += FB_API_SECRET

md5 = hashlib.md5()

sig = md5.hexdigest()

params["sig"] = sig

encoded_params = urllib.urlencode(params)
headers = {

conn = httplib.HTTPConnection(FB_API_HOST)
conn.request("POST", FB_API_PATH, encoded_params, headers)

response = conn.getresponse()

print response.status, response.reason
return response.read()

This Works For Me when I use it with iphone facebook-connect client:

[FBSession sessionForApplication:myApiKey getSessionProxy:myURL delegate:self];




Stripping trailing whitespace from XCode

Firstly, my usual approach of s/\s+$//g doesn't work since it eats up the newline too. s/\s+$/\n/g doesn't work either because the Replace: field in the find dialogue doesn't escape the \n.

Final solution is as shown: s/[ \t]+$//g




Family Planning

YLDE comic: half as funny half as often.

because imitation is the best form of flattery.

title="and the product of their age will be my private key"/>




Futurlec Ultrasonic Sensor Note

Part number US1440, these sensors are not dual use, which means the transmitter and receiver is not exchangeable. They also look identical from the outside, so if you build my arduino ultrasound ranger with these (as I did) and find you can't see echoes, try swapping your transmitter and receiver around! It cost me no small amount of headach this morning.



Arduino ultrasound ranger

Code to drive it is avaliable under GPL.

An updated version of the schematic as a .sch file is available also.




The libertarian argument against vaccination

Goes like this:

I have a right to decide what happens to my children!

To that I say, we have the right to not be a victim of your stupidity. Further, laws represent social contracts that all members of society enter into for everyone's benefit. Getting vaccinated is one such contract which serves to prevent epidemics of infectious diseases.

If you really don't want to vaccinate your child, then please ensure your child never comes into physical contact with the rest of society for our sake. I would not welcome them without vaccinations.





What is the period if you want to generate a 40KHz signal? It certainly isn't 250E-6 seconds. The same calculator returns 2500E-3 for 1/4 which is 2.5, not 0.25.

The calculator is PEMDAS.




Auto leveller

A simple 2 DoF setup with an accelerometer (LIS302DL) attached to the effector. The seeeduino attempts to drive the servos so the Z and Y axis measure zero acceleration. The sketch is available at the usual place.

The algorithm employed is a simple PD controller with pseduo-gradient stepping to determine how to control each servo without any knowledge of how the servos are arranged. To deal with sensor noise from the accelerometer, a schmitt trigger mechanism is employed along with a moving average.

The code also allows the system to be calibrated to deal with accelerometer miscalibration.

It is cute, but that is about it :-) Coupled with a tripod and better construction ut might be useful for some DIY surveying or construction work.




Turn a servo into 1-wire-control bi-directional motor

It is often a pain building H-bridges or getting a motor shield just because you want to control a few motors. This is where servos come in: they are cheap(ish), have motor and gearbox, and they are controlled ysing only a single wire.

The only problem is of course, servos aren't designed for continuous motion. Servos are designed to respond to PWM pulses which encode the desired position for the servo to be in, not a desired speed or direction. So some modification is required.

Now there are many articles on the intertubes on how to do this, but they mostly concern themselves with making the servo into little more than a normal motor with a gearbox - you still need a H-bridge to control the direction. This modification is a little different: it leaves you with a servo that behaves like a bi-directional motor where the direction is controlled by a single wire.

Due to the variety of servos out there, I will present only the general idea, which applies to servos which use a potentiometer to determine position.

Firstly, a quick explanation of how these servos work. The principle behind these servos is that the potentiometer is connected to the output gear such that the voltage of the sweeper (connected to the middle pin usually) depends on the angular position of the output gear. The onboard controller converts the input PWM signal into a target voltage, then it powers the motor so it turns the output gear until the voltage of the sweeper matches the target voltage. This is kind of control action is relatively easy to achieve using an operational amplifier.

The basis of the modification is essentially this: if we fix the sweeper voltage, then any input asking the servo to move away from the sweeper voltage will result in the onboard controller continuously attempt to match sweeper voltage to target voltage. By making the target voltage higher or lower than the sweeper voltage, we get directional control.

To fix the sweeper voltage, the potentiometer will need to be disconnected and replaced with a voltage divider. I assume that when the output gear is in centre position, sweeper voltage is half the supplied power because the potentiometer will be in its centre position. Since I wanted 0 degree rotation to map to stop, I fixed the sweeper voltage at this value by using 2x1.5K resistors in series. This is all you need to do electronically. There should be enough room to fit in 2 extra resistors, even in small 8g servos (what I had).

The last thing to do is to remove any physical obstructions which prevent the output gear from completing a full revolution.

Once the electronic and physical modifications are complete, you should have a servo which can turn in either direction continuously and controlled via single wire. Two of these would be perfect for say powering a small rover...




Tips for interfacing a electret mic with an arduino

  1. The arduino ADC is 10bit, which means the range is 0-1023 and this is mapped to 0-5V most of the time, unless you do funky things to AREF.

  2. Most amplifier circuits on the net (using op-amps or LM386s) will have a DC bias of 2.5V, this is to allow the output to swing both ways. So on the arduino side even with complete silence you will read a value ~512 or so.

  3. The resistor connecting the electret mic and power also adjust the sensitivity of the microphone in addition to providing power to the mic (which includes an internal pre-amp, by the way). This resistor is very important - if you find your microphone isn't responsive enough, then adjust this resistor first before debugging your amplifier circuit. For me this "fixed" my microphone's responsiveness. It is best of this was a variable resistor which makes adjustment trivial.

  4. I was making a simple volume meter, and I did the following: clamp upper values to about 600, then using map map this to a range between 0-7 (since I had 8 LEDs). This was because I found the value never went above 600 very often, so the upper few LEDs were essentially going to waste sitting around. YMMV.