Emacs and Arduino
31 Oct 2009
Arduino IDE is good, but if you are heavily invested in any other editor such as Emacs or Vim, it just sucks. However there is an option to use an external editor to do the coding and just use the IDE to compile and upload.
External editor option is not documented very well. What happens when you enable it is, it will disable editing on the file so you can use your favorite editor (emacs for me) to do your coding and use the IDE just for compiling and uploading.
Controlling LEDs with an Arduino
31 Oct 2009
This post will cover the process of controlling multiple LED's using an Arduino. With this hack we'll make the Arduino, send a distress signal using a LED, a second LED is used to indicate the end of SOS cycle.
Hook-up
I used the following hook-up,

Both LED's are connected the same way except to different pins (Digital 10 and 9). +5V is connected to the resistor which is connected to the + (long leg) of the LED, - (short leg) of the LED is connected to the ground.
Any resistor that is bigger than 330 ohms can be used, but keep in mind that the smaller the resistance the bigger the glow.

Completed circuit looks like the picture above.
Code
First some definitions,
int redLEDPin = 10;
int yellowLEDPin = 9;
I choose to use pin 10 for the red LED, pin 9 for yellow LED.
int shortPulse = 250;
int longPulse = 125;
int letterDelay = 1000;
Morse code uses a short element and a long element to represent letters and numbers. We will use 250 ms for long element 125 ms for short element, after each letter we'll have 1 second delay.
//letters 0 for short pulse 1 for long
int letterS[] = {0, 0, 0};
int letterO[] = {1, 1, 1};
To represent the letters, i used an array, 1 to represent a long element, 0 to represent a short element.
void blinkLetter(int* letter){
for(int i=0; i<3; i++){
int val = letter[i];
if(val == 0)
redLed(shortPulse);
else
redLed(longPulse);
}
delay(letterDelay);
}
blinkLetter takes an array as it's argument, it will iterate over the array blink the red LED accordingly.
void redLed(int time){
digitalWrite(redLEDPin, HIGH);
delay(time);
digitalWrite(redLEDPin, LOW);
delay(time);
}
redLed function takes a time variable as its input it will keep the LED on for the given time.
void yellowLed(){
digitalWrite(yellowLEDPin, HIGH);
delay(2000);
digitalWrite(yellowLEDPin, LOW);
}
yellowLed uses a fixed time to signal us that the SOS cycle is complete, we are starting over.
void loop() {
blinkLetter(letterS);
blinkLetter(letterO);
blinkLetter(letterS);
yellowLed();
}
Main loop just cycles through the letters and blink the yellow LED to signal us that the cycle is complete.
When the circuit is hooked-up and code uploaded, it should look like this.
Connecting a Photoresistor to an Arduino
29 Oct 2009
A photoresistor is a resistor whose resistance decreases with increasing incident light intensity, using a photoresistor we can make arduino sense the intensity of light around it. Let's look at the hook up,

Using the playground article as reference, resistor (100K) and photoresistor are connected in series. +5V goes to resistor, ground goes to photoresistor, the junction where the resistor and photoresistor meets goes to analog 0. Digital 13 is used for the LED.


In my room photoresistor reads around 80, when i put my thumb on it making it dark, it reads around 500. I wanted the LED to light up when its dark.
int lightPin = 0; //define a pin for Photo resistor
int threshold = 250;
void setup(){
Serial.begin(9600); //Begin serial communication
pinMode(13, OUTPUT);
}
void loop(){
Serial.println(analogRead(lightPin));
if(analogRead(lightPin) > threshold ){
digitalWrite(13, HIGH);
Serial.println("high");
}else{
digitalWrite(13, LOW);
Serial.println("low");
}
delay(100);
}
So i picked a number in between and used it as threshold, if the reading is above the threshold it turns the LED on when its below threshold it turns the LED off.
And the result is,
Connecting MySQL Client to a Remote MySQL Server
28 Oct 2009
Every now and then i would like to connect to my VPS using the gui tools. However MySQL port on the VPS is blocked. SSH provides a way to connect to a remote MySQL without exposing MySQL port to the whole world.
Port Forwarding
Route your port 3307 to the port 3306 on the remote host,
ssh -p 2200 -L 3307:localhost:3306 user@server.com -v
If your ssh is running on standard port remove -p flag
ssh -L 3307:localhost:3306 user@server.com -v
Connection
Now you can access your MySQL instance just like you would if it were to be running on localhost but on 3307.
For MySQL command line client ,
mysql -h 127.0.0.1 -P 3307 -u <mysql_username> -d <db_name> -p
Same for the gui tools use 127.0.0.1 as host and 3307 for port.
Arduino On Mac OS X
26 Oct 2009
Arduino is a physical computing platform consists of three separate tools. Bundled together they are referred to as the Arduino Toolkit.
- Arduino Controller
- Language and Compiler
- Arduino IDE
With this toolkit you can write programs that can sense and respond to the world.
If you know electronics you can build one for under 30$, schematics are available for download. Or if you are a software guy like me with no electronics knowledge you can get started in seconds with one of the available starter kits. The beauty of Arduino is that it really lowers the barrier of entry for physical computing.
If you are just getting started i would suggest getting a starter kit, turning a LED on and off is fun for only couple of hours. Starter kits include sensors such as touch, heat etc, also buttons and such so you can to get a feel of what can be achieved using an Arduino.
Following is a mini tutorial on how to setup Arduino Duemilanove on Mac OS X and getting it run a Hello, World application Arduino style.
Instalation
Of all the hardware I've installed on my machines Arduino is by far the easiest. You can download the IDE and required Drivers from Arduino web site.
After mounthing the dmg you downloaded. You need to install the drivers that let's your Mac communicate with the Arduino controller.
For Intel Macs you need to install,
FTDI Drivers for Intel Macs (2_2_10).pkg
For PPC Macs you need,
FTDI Drivers for PPC Macs (2_1_10).pkg
IDE installation is just like any other Mac application drag and drop the Arduino.app to /Applications.
Configuration
The only configuration you need to do is to select the type of board from the
Tools -> Board
And set your which serial port to use in order to connect to the board. Do not panic if you don't see the serial port listed in
Tools -> Serial Port
You will see it when the board is connected.
Hello, World
Now every programming language has a Hello, World example. For Arduino this is accomplished by turning a LED on and off.
Fire up Arduino IDE. Copy-Paste the following code.
void setup() {
pinMode(13, OUTPUT); // sets digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
setup method is only ran once thats where your initialization code goes. After that loop method will loop forever, thats where you main code goes think of it like the main method in other programming languages.
Pin 13 on Arduino is connected to the built in LED on the board. By setting pin 13 HIGH you turn it on by setting it LOW you turn it off. This will cause the LED to turn on and off continuously.
In order to verify and compile your code click Play, you should see a output such as the following,
Binary sketch size: 888 bytes (of a 30720 byte maximum)
In order to upload it to the board press Upload button on the IDE, if you see done uploading, you board should start blinking.
Regular Expressions in Clojure
25 Oct 2009
Regular expression is a formal language that will allow you to find chunks of text that matches the patterns you specify. Following are a bunch of code examples which I've put together as a mini-reference for my own use.
In clojure you can define a regular expression using,
#"<patter>"
syntax.
To search for match in a string, you can use re-find, it will return either the match or a vector of matches if you have groups.
user=> (re-find #"quick" "The quick brown fox jumps over the lazy dog")
"quick"
user=> (re-find #"(f(oo bar))" "foo bar")
["foo bar" "foo bar" "oo bar"]
Like other data structures you can threat regex's as sequences too, re-seq will return a lazy sequence of matches.
user=> (re-seq #"h" "The quick brown fox jumps over the lazy dog")
("h" "h")
If you are coming from java world one thing that will confuse you is that at first there seems to be no way to specify pattern flags such as,
DOTALL
MULTILINE
UNICODE_CASE
Instead of those flags you can use embedded flags.
- Unix lines mode can enabled via (?d).
- Case-insensitive mode can enabled via (?i).
- Multiline mode can enabled via (?m).
- Dotall mode can enabled via (?s).
- Unicode-aware case folding can enabled via (?u).
Fractals in Clojure - Sierpinski Triangle
23 Oct 2009
This fractal is called the Sierpinski triangle, after the Polish mathematician Waclaw Sierpinski.
Construction
In order to generate it we'll use the following algorithm,
- We pick a triangle on the surface.
- We shrink the triangle to half the height and width, make three copies, and position the three triangles so that each triangle touches the two other triangles at a corner.
- We repeat the above step until required depth is reached.
Code
(defstruct point :x :y)
(defstruct triangle :left :rigth :bottom)
We need two structures for representing a point on the canvas and the triangle we are drawing.
(defn midpoints [trig]
(struct triangle
(struct point
(int (/ (+ (:x (:bottom trig)) (:x (:left trig))) 2 ))
(int (/ (+ (:y (:bottom trig)) (:y (:left trig))) 2 )))
(struct point
(int (/ (+ (:x (:rigth trig)) (:x (:bottom trig))) 2 ))
(int (/ (+ (:y (:rigth trig)) (:y (:bottom trig))) 2 )))
(struct point
(int (/ (+ (:x (:left trig)) (:x (:rigth trig))) 2 ))
(int (/ (+ (:y (:left trig)) (:y (:rigth trig))) 2 )))))
Given a triangle this function will return a new triangle thats half the original triangle.
(defn create-triangles [trig step depth g]
(paint-triangle g trig)
(let [points (midpoints trig)
left (struct
triangle (:left trig) (:left points) (:bottom points))
rigth (struct
triangle (:rigth trig) (:rigth points) (:bottom points))
top (struct
triangle (:bottom trig) (:rigth points) (:left points)) ]
(if (< step depth )
(do (create-triangles left (inc step) depth g)
(create-triangles rigth (inc step) depth g)
(create-triangles top (inc step) depth g)))))
create-triangles is a recursive function that will call it self until required depth is reached. It first draws the given triangle then calculate 3 smaller triangles that will be placed inside the parent triangle.


Download code
Simple Bash Installer
21 Oct 2009
Following is a simple bash script to build a installer for any *nix system.
#!/bin/bash
SKIP=`awk '/^__DATA_BEGIN__/ { print NR +1; exit 0; }' \$0`
tail -n +\$SKIP \$0 | tar x
exit 0;
__DATA_BEGIN__
Save the script as "intaller.sh".
Put everything you need to install in a folder and tar the folder,
tar -cvvf foo.tar foo/
Concatenate everything into a file,
cat installer.sh foo.tar > setup
Make it executable,
chmod 755 setup
Now when you run setup it will extract the folder in to current directory.
Password Based Encryption Using AES
16 Oct 2009
In a recent engagement, i needed to encrypt an object then write it to disk. I wanted to use Advanced Encryption Standard(AES), but unfortunately Java does not provide a way to use password based encryption with AES.
Bouncy Castle does provide the required libraries that will allow us to use password based encryption with AES.
In this scenario, i wanted the user to provide the pass phrase.
//name of the algorithm to use
static String algorithm = "PBEWITHSHA256AND128BITAES-CBC-BC";
static char[] passPherase = "secretpass".toCharArray();
static byte[] salt = "a9v5n38s".getBytes();
static String secretData = "Very Secret Data!!";
In real world you should generate the salt randomly. We need to create a key spec based on the user input then create a cipher for encrypting or decrypting the data.
Encrypting the data
static SealedObject encrypt(String data) throws Exception{
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt,20);
PBEKeySpec pbeKeySpec = new PBEKeySpec(passPherase);
SecretKeyFactory secretKeyFactory =
SecretKeyFactory.getInstance(algorithm);
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE,secretKey,pbeParamSpec);
return new SealedObject(data,cipher);
}
Decrypting the data back again
static String decrypt(SealedObject sealedObject) throws Exception{
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt,20);
PBEKeySpec pbeKeySpec = new PBEKeySpec(passPherase);
SecretKeyFactory secretKeyFactory =
SecretKeyFactory.getInstance(algorithm);
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE,secretKey,pbeParamSpec);
return (String)sealedObject.getObject(cipher);
}
Using a SealedObject you can basically encrypt any object you want, and serialize it to a file.
public static void main(String[] args) {
try{
Security.addProvider(new BouncyCastleProvider());
SealedObject encryptedString = encrypt(secretData);
String decryptedString = decrypt(encryptedString);
System.out.println(decryptedString);
}catch( Exception e ) {
System.out.println(e.toString());
}
}
Download code
Fixing Emacs 23 on Mac OS X
14 Oct 2009
If you are a long time emacs user and upgraded to release 23 on Mac OS X. You will find that your command and option keys are changed and fn- left/right no longer functions as beginning-of-line and end-of-line respectively.
Switching them back is as easy as,
(setq mac-option-modifier 'super )
(setq mac-command-modifier 'meta )
(define-key global-map [home] 'beginning-of-line)
(define-key global-map [end] 'end-of-line)
Processing XML With Clojure
10 Oct 2009
Although XML is nice in theory, i have always hated dealing with it. It requires so much boiler plate code just to parse or create a simple XML file. Recently i needed to do some XML processing and i still can't believe how easy it was to create and parse XML in clojure.
Creating XML
Clojure contrib includes a library for creating XML called prxml. Vectors become XML tags. Such as,
(prxml [:p {:class "greet"} [:i "Ladies & gentlemen"]])
; => <p class="greet"><i>Ladies & gentlemen</i></p>
First let's define some data to turn in to XML.
(def data #{{:title "Clojure" :link "http://clojure.org"
:description "Clojure Homepage"}
{:title "Java" :link "http://java.sun.com"
:description "JVM Homepage"}
{:title "Debian" :link "http://debian.org"
:description "Debian Homepage"}})
By default prxml function outputs to the screen if you want to output to a string use prxml in combination with with-out-str.
(defn articles []
(reduce
(fn [feed v]
(conj feed
[:item
[:title (:title v)]
[:url (:url v)]
[:description (:description v)]]))
() data))
We build a list of vectors for every node in the XML.
[:item
[:title "Clojure"]
[:url "clojure.org"]
[:description "Clojure Homepage"]]
[:item
[:title "Java"]
[:url "java.sun.com"]
[:description "JVM Homepage"]]
If you wrap everything, it takes less than 20 lines of code to produce an RSS feed.
(defn xml-data []
(with-out-str
(prxml [:decl! {:version "1.0"}]
[:rss {:version "2.0"}
[:channel
[:title "The Site"]
[:link "http://site.com"]
[:description "The Site"]
(articles)]])))
Parsing XML
Parsing XML is even easier, clojure core has built in support for XML processing. clojure.xml/parse can take a File, InputStream or String naming a URI and return a tree of the xml/element struct-map. You can then treat it like any other sequence.
Such as to iterate over all the titles in the XML file,
(let [input-stream (ByteArrayInputStream. (.getBytes (xml-data) "UTF-8"))]
(for [x (xml-seq (parse input-stream))
:when (= :title (:tag x))]
(:content x)))
rss=> (["site-title"] ["Clojure"] ["Java"] ["Debian"])
Fractals in Clojure - Buddhabrot Fractal
04 Oct 2009
Buddhabrot is a special rendering of the Mandelbrot fractal. Rendering technique we will use is developed by Melinda Green.

First we create a 2D array that will map to the pixel's on the screen. Next we start picking random points on the image and apply the mandelbrot formula. We iterate through the formula for points which do escape within the chosen number of iterations.
To get started we need to get some utility functions out of the way, at first i used the complex number library in clojure-contrib but for some reason they were taking a long time to compute. So i wrote my own.
(defstruct complex :real :imag)
(defn add
"Complex addition"
[ c1 c2 ]
(struct complex
(+ (:real c1) (:real c2))
(+ (:imag c1) (:imag c2))))
(defn multiply
"Complex Multipication"
[ c1 c2 ]
(struct complex
(- (* (:real c1) (:real c2)) (* (:imag c1) (:imag c2)))
(+ (* (:real c1) (:imag c2)) (* (:imag c1) (:real c2)))))
(defn abs
"Complex Absulute Value"
[ complex ]
(Math/sqrt
(+ (* (:real complex) (:real complex) )
(* (:imag complex) (:imag complex)))))
For each point we pick on the screen we need to calculate how the point escapes.
(defn calc-path
[ x y max-iterations ]
(let [ c (struct complex x y) ]
(loop [z c
path [ ]
iterations 0 ]
(if (> iterations max-iterations)
[]
(if (> (abs z ) 2.0 )
(conj path z)
(recur (add c (multiply z z)) (conj path z) (inc iterations)))))))
If the point escapes with in the chosen number of iterations we get a list of points. For each point we get, we increment a counter in the buffer. In the end we color the fractal based on the number of iterations that passed through that pixel.
Since drawing buddhabrot is an expensive operation and takes a long time to render good looking pictures. Calculations are done on a separate thread generate function will run indefinitely, you can check picture quality using the draw function. Because there will be no output you need to run the script in REPL and call draw to look at it.
(defn generate [fractal]
(let [buffer (:buffer fractal)
rand (new Random)]
(doseq [point (iterate inc 1)]
(let [x (- (* 6 (.nextDouble rand)) 3)
y (- (* 6 (.nextDouble rand)) 3)
path (calc-path x y (:iteration fractal))]
(if (= (mod point 1000000) 0)
(println "Point: " point))
(doseq [p path] (buffer-set fractal p)))) ))
(defn start [fractal]
(.start (Thread. (proxy [Runnable] [] (run [] (generate fractal))))))
For coloring the image we use the same algorithm that we used to draw the mandelbrot set,
(defn calc-pixel-color
[ iteration max-iterations ]
(let [ gray (int (/ (* iteration 255) max-iterations ))
r gray
g (Math/min (int ( / (* 5 ( * gray gray)) 255)) 255)
b (Math/min (int (+ 40 ( / (* 5 (* gray gray)) 255))) 255) ]
(try (new Color r g b ) (catch Exception e (new Color 0 0 0))) ))
We iterate through the array paint everything on an BufferedImage then draw that on a JLabel.
(defn paint-canvas [fractal graphics]
(let [buffer (:buffer fractal)
biggest (apply max (map #(apply max %) buffer)) ]
(doseq [y (range (:height fractal))
x (range (:width fractal)) ]
(if (> (aget buffer y x) 0 )
(do
(.setColor graphics (calc-pixel-color (aget buffer y x) biggest ))
(.drawLine graphics x y x y ))))))
(defn draw [fractal]
(let [frame (new JFrame)
image (new BufferedImage
(:width fractal) (:height fractal)
BufferedImage/TYPE_INT_RGB)
canvas (proxy [JLabel] [] (paint [g] (.drawImage g image 0 0 this)))
graphics (.createGraphics image)]
(paint-canvas fractal graphics)
(.add frame canvas)
(.setSize frame (new Dimension (:width fractal) (:height fractal)))
(.show frame)))
To play with the script, define a fractal
(def fractal {:buffer (make-array Integer/TYPE 800 800)
:width 800 :height 800 :iteration 50})
Start calculations,
(start fractal)
Check the result at intervals until you are satisfied.
(draw fractal)
Below are the some shots on how the image progresses,
10 Million Points

30 Million Points

60 Million Points

400 Million Points 400 Iterations

Download code
Jump Start Your Clojure Projects
02 Oct 2009
Since i started playing with clojure, i have been jumping from one toy project to another. This required creating the same code structure, copying same jars all over again and modifying the same ant file for the new project.
After fourth or fifth time, i got bored and created a clojure project stub. By setting a few variables inside the build.xml file you can have a working Hello, World application contained in a executable jar.
Setup
To get started either clone my project or download it. Modify build.xml file and set your project properties such as project name and application jar name.
Running,
ant setup
will download the necessary jar files and will place them under extLibs/ directory. Create the src/ directory and populate it.
All Class-Path settings are handled in the build file so you can start coding right away.
By default required jars are downloaded from my repository, but you can change it and set it to point anywhere you like.
Targets
- run - will setup required class path's and run the application
- prepare - will unzip necessary jar's to create a single executable jar.
- compile - will build the application and create single executable jar.
- clean - will clean up the build folder and created jar.
- deps - can redownload needed jar's.
- setup - is ran once to create all files required to create a "Hello, World" application with your settings.