Friday, 28 November 2008

Not So Smooth

While waiting for the rest of the parts to turn up I decided to play with the colour mixing logic to produce a colour for each orientation of the accelerometer. The aim is to get six colours, Red, Green, Blue, Cyan, Magenta and Yellow.

In my first attempt I was just using absolute values from the accelerometer this allowed for mapping to Red, Green and Blue on each axis. E.g. X and -X (Upside down) both mapped to blue. To get the six colours, X and -X must be different.

The following table shows how I think this should work.

Axis Colour RGB(Hex)
X Red FF0000
Y Green 00FF00
Z Blue 0000FF
-X Magenta FF00FF
-Y Cyan 00FFFF
-Z Yellow FFFF00

The readings I am getting from the accelerometer range from -1024 -> 1024, actually they sometimes fall outside this.

So for each axis reading I convert to the hex value based on the following logic.

if (reading > 0)
reading_avg = map(reading, 0, 1024, 0, 255)
reading_avg = map(reading, -1024, 0, 0, 255)

The map function maps the given reading from one range to another.

I am taking the average of the last 10 readings to smooth the transition, as the accelerometer in a resting position gives varying readings, hence reading_avg.

This works in a fashion, but is not as nice and smooth in the transition as it is when dealing with three colours. It often jumps to another colour rather than a smooth fade.

I think this is caused by a reading flipping into a negative value, causing a sudden change from a low value.
So when X and Y are giving readings near FF, and Z is or should be 0 a slight movement will cause it to go negative and produce a reading near the FF range. So I need to make the logic a bit smarter around the boundary cases.

I have just noticed as documenting this that I am using the average values in the calculations which may not be helping.

Looking back at the code now I am thinking less haste more DOCUMENTATION :)

So some success in was achieved with this, have something to work on.
Need to recheck code logic, look at adjusting the smoothing function to average across more values, and maybe adjustments in the mapping logic around the boundary cases.

But now the new bits are here, I think some wireless transmission tests might be the go.

Thursday, 27 November 2008

Arduino Pro Mini - More Bits.

After i got the basics working with the Arduino Diecimila, I had to work out what I think I will need to complete the project. The following is the list of components I ordered, this time from, all but the additional Mini Bread board.

Arduino Pro Mini - 3.3V/8MHz
RF Link Transmitter - 315MHz
RF Link 4800bps Receiver - 315MHz
LED Light Bar - Tri-Color
Triple Output LED RGB
Break Away Female Headers
Battery Holder - 4xAA Square
Mini Bread Board

I used last time and was happy with the service and delivery etc, the reason I opted to try spark fun was the range of LED components they had suited my needs better. The BlinkM that Cool Components stock looks quite cool, but has too much intelligence built into ot for my needs. Was plesantly supprised by the shipping costs, about 50p less than UK based orders, I expected it to be a bit more expensive.

The Arduino Pro Mini comes in two flavours, a 5v/16MHz and 3.3v/8MHz, The only reason I went for the 3,3v/8MHz option is because they were out of stock of the 5v one. These things are tiny!
There was also the Arduino Stamp option which as far as I can tell is the 5v/16MHz with the header pins already soldered. This is about twice the price, so figured it is worth a crack at soldering this myself.

While waiting for the bits to arrive I have been reading up on the RF Communication components and how this is achieved. I have found a mixed bag results among others related to this subject, so I figure I getting this to work will fall into one of the following experiences;

1 - Easy just set it up and it will work like magic using the Serial Communication Api.
2 - It is a Blark Art, Voodoo type operation, requiring the Sacrificing of Goats on sacred alters.

I also read somewhere that for timing critical applications e.g. serial communication the 16MHz model with the external crystal is better than the 8MHz with the less accurate internal crystal. So I am hoping my choice of board does not create problems. I guess time will tell...

Thursday, 20 November 2008

XYZ - Time to play with the 3 Axis Accelerometer

What could possibly go wrong!

Being a bit more cautious now, for a while I thought I had cooked a couple of the outputs of the Arduino, I left the LEDs running while I was playing with other bits and pieces, tidying code etc. Ran another test and only 1 LED was working! The board had been shuffled about the desk a bit, and two of the resistors leads were crossed. I was a bit gutted, took a while before I checked my code changes and found a bug.

Arduino Decimila
LIS3LV02DQ Triple Axis Accelerometer

First of all I had to solder some connector pins to the Accelerometer breakout board.
So out came the soldering kit, another recent purchase since getting into this game, had a bit of a practice to get my eye in before attacking the breakout board, soldered an 8 pin male header to the board.

Found the following FANTASTIC article which showed me exactly what I needed to do to get this up and running, along with some sample code.

Initially the WHO_IS command was returning FF not the 0×3A expected, a bit of a wiggle soon sorted that problem out. Then added the code to send the reading from each axis to the laptop. One axis was giving sensible readings, Was a little concerned as the other two were giving me -32768, Integer overflow! From what I understood, I should have been getting (-1024... 1024).Checked the variable types they were, so next thought was I had cooked the chip when I soldered the header pins.

Double checked the wiring as described in the article, reset the accelerometer into the bread board, powered it back up and all was well, sensible readings from X,Y, and Z :) happy days.

Then cobbled some code into the Arduino to covert each axis reading into a value to represent a RGB colour. For now I decided to use absolute values and map into 0..255 range.
the readings from the accelerometer vaired quite a bit even while resting on the desk. So I decided to use an average of the previous 10 readings so the colour would not flicker. I am guessing the Smoothing function idea was a result of reading through the many great samples on the Arduino site.

To visualise, I added some code to the ColourChooser component I had written to test the LEDs. Just the code parses the data into RGB values and updates the background of the ColourChooser app.

The result, BIG GRINS when that all came together and worked :)
Am very pleased to have progressed this far so quickly. If I had not found examples online I would probably still be reading the data sheet for the ATmega168, trying to work out how to make an LED blink. Who would have thought such a tiny bit of plastic, silicon or what ever it is made of could have so much going on inside it, 376 pages is the Data sheet that explains its innards!

Its about now I am regretting not buying all the bits at once....

Wednesday, 19 November 2008

LED there be Light...

The plan to control the brightness of the LED.

Arduino Board
Mini Bread board
Some Jump lead wires to connect it all up.
3 LEDs (Reg, Green, Blue)
3 resistors.

Based on the Fading example on the Arduino site, and provided in the Arduino development environment, I wired up the 3 LEDs to the Arduino board. Then modified it to control 3 LEDs.

To get a bit more cleaver I wanted to control the light from the computer, the Dimmer example provided the basics for the implementation from the Arduino board side of things.

For the coding on the laptop I decided to implement this in Java. The reason for this is Java is something I know my way around well. With the Arduno code which is based on C/C++ I am just winging it at the moment.

If this is the first time you want to communicate over a serial port in Java code you'll need to install some Operating System dependant libraries. This surprised me a bit, but found the following link, which provided the info required. Go with the rxtxSerial libraries not SUN's javax.comm package, its not good gave me some grief.

The link for installing the libraries also had some sample code for implementation which I happily blagged and modified in order to knock up a bit of Java to display a JColorChooser component, hooking the code to send the selected colours RGB values to the Aruino to change the colour of each of the LEDs.

After some playing/searching/thinking decided on a go with the format o #RRGGBB, with colours represented as HEX, and pad if the value is less than 10, to get a consistent 2 digits for each colour value.

On the Arduino side the following function takes care of reading the data in. The serInStr array and dataLength, are sized to the expected 7 so that takes care of reading a complete value each time.

//read a string from the serial and store it in an array
int readSerialString () {
int i=0;

//Serial.println("No Data");
return -1;
while (Serial.available() && i < dataLength)
int c =;
serInStr[i++] = c;
return i;

Works really well so am pleased with progress.
Next on the the scary bit, trying not to cook the accelerometer. LEDs about £1 not bothered if i cook them, the accelerometer £27, that would annoy me somewhat.

Tuesday, 18 November 2008

Stans Plan...

After having a play with the samples provided with the development environment, thought I had better work out a few details to get me where I want to be.

So I decided as a basic course of action.

Step 1
Configure the board and write code to control the LEDs
Specify a RGB colour on the laptop send to the Arduino to update the LEDS

Step 2
Configure the board and code to talk to the sensor, and output the readings to the laptop.
Convert the output of the accelerometer into RGB values.

Step 3
Combine the above so the LEDs and Accelerometer are wired up to the board and the LEDs updated directly from logic on the board.

Step 4
If I get this far and have not lost interest or smashed the tiny bits into tinier bits out of frustration, order the rest of the bits required for the job.
  • Wireless transmitter and receiver
  • Another Arduino board
  • A proper RGB LED unit.
And make it work wireless!

Sounds straight forward enough.

The postman only knocks once...

The doorbell is broken!

Was expecting the delivery so was keeping an eye out, saw the postman on his way up the other side of the street so was halfway down the stairs when he knocked.

The package was surprisingly small, well not so if I had put 2 and 2 together, the image from the web site the specifications that said it is 75x55mm, the accelerometer on the breakout board 20x20mm, the chip itself 5x5mm, glad I didn't think I would 'have a go' a soldering that up myself.

I had spent a bit of time looking around the Arduino site,, and was surprised to find installation instructions for OpenSUSE Linux, that is rare! And find I am jumping through hoops to get things working following some other flavour of Linux's usually Ubuntu based instructions that just don't work the same.

Anyway after extracting the Adruino development environment, installing the following dependencies,
  • avrdude
  • avr-libc
  • cross-avr-binutils
  • cross-avr-gcc
all was well. Went through the basic tutorials, Blinking LEDs etc, so was happy that the board arrived intact and in working order. I put the small expensive bit (Accelerometer) to the side, to avoid an over egar half arsed attempt at wiring that up and cooking something.

One stop shop

In my comfort zone buying things I know about, from sites i have used before, I quite like the 'magic' of internet shopping. This is a completely different kettle of fish, don't know really what I am buying and looking on all new sites for the bits.

While I only wanted a couple of main bits I wanted to get them somewhere that had all the bits I would need for the project, I am always a bit dubious about handing over my my credit card details to a new web site, then the wait and wonder if anything will turn up. While the internet banking gets frequent random spot checks on the Credit Card balance.

Turns out all accelerometers are not created equal, there are a few quite options to choose from, I decided on the LIS3LV02DQ Triple Axis Accelerometer mounted on a breakout board. At close to 30 quid not the cheapest option, but was thinking it would be more versatile if required for some yet to be dreamed up future project.

Went to local(ish) electronics shop to get a few bits and bobs,
Jump leads
Red/Green/Blue LEDs - I want as one unit but these are cheap and will do for the first stage.
3x 1KOhm Resisters (I think)
Header pins to solder to the breakout board of the accelerometer.

And ordered the Arduino Diecimila and Accelerometer, and mini board from

The other site I found very useful was is has a wider range of components, but opted for Cool Components as it is UK based.

Then in for the down side to Internet Shopping for physical objects... the wait

Where it started...

The goal of this project:
Think, holding a Rubik's Cube, whatever side is facing up, is the colour the lamp on the other side of the room will change to. As the cube is rotated the light will change.

The plan:
To build this using an Arduino Diecimila open source board(x 2), RGB LEDs, 3 Axis Accelerometer, and wirless transmitter/reciever.

Floating around in the back of my head for a while has been the idea to play with intelligent or automated control systems, without a any specific plan or project in mind.

While fossicking around the internet a while back I came across a project on the web site (had not heard of this before), which I looked at and thought was pretty cool, it showed a RGB LED being controlled wirelessly using an Accelerometer to control the colour, see here. I was holding a Rubik's cube at the time, and was thinking how cool would it be to rotate the cube so that the colour facing up would be the colour the LED would emit and while rotating, would gradually change between these colours.

One of the components was was an 'Arduino wee' Searching for this I found discovered the Arduino site
and the 'Arduino Diecimila' an Open Source electronics development platform,

A few days passed and the idea kept growing on me and the 'thats pretty cool' factor hadn't waned. So I went shopping for the bits, as this is something I know very little (nothing) about I decided on a run before walk approach, so I only ordered a couple of core components, the 'Arduino Diecimila' board, and the Accelerometer.

I started this a week or so ago, so will post a couple in quick succession in line with my progress, rather than one huge post.

So it begins...