Wednesday 10 December 2008

Blink like a Pro ( Arduino Pro Mini )

I have seen a couple of youtube videos posted showing the most basic Arduino test program, BLINK, and thought these were pretty lame, well not really worth the effort of posting to youtube anyway.

But here I am with an update of my success getting the very same code working on the Arduino Pro Mini.

Before ordering Pro Mini I had a hunt around to see what was needed in order to program it, as unlike the Diecimila board it does not have the USB to Serial connection components. I found a link that contained a diagram that used the older Arduino NG board as the programmer for the Pro mini. So figured I would not need to buy a programmer, or the USB to Serial connector. For some reason I neglected to bookmark this little gem of a link. It took a few days to find again.

I was a little frustrated by this and set about the task of working out the best option for programming the Pro Mini. A propper programmer like this will let me program a chip without the bootloader, not an immediate requirement, but possibly useful in the future when I have a better understanding of what is going on. The other options were the Arduino Serial USB Board, or the FTDI Breakout Board.

While hunting around for this I rediscovered the link showing how to use the Diecimila to program another board. Still not sure I found the correct one as the one I followed showed the Arduino Stamp, similar but not exactly the same.

After some hesitation I decided to have a crack at soldering the pins to the board and program it from the Diecimila.

So that is the current state of development, I removed the ATMega168 chip from the Diecimila board, wired it up to the Pro Mini board, in in a similar fashion shown in the link above. From the Arduino Development environment I selected the Arduino Pro Mini (8MHz) as the target, it is now blinking away quite happily.

I am not sure how long this setup for programming the Mini will last. When I need update both chips regularly, I am sure I will get sick ripping the chip out of the Diecimila each time I need to update the Mini.

I have taken a couple of pics but they did not come out very well, will try again in daylight hopefully that will be better.

I have had a bit of a go at implementing the send and receive through the transmitter/receiver using just the Diecimila board, without success, I think the main issue is the chip only supports the UART Serial connection via the rx/tx pins 0 and 1. I tried to implement this using the SoftwareSerial library, but this has a few limitations as there is no input buffer, so the read() method will only return if being called when the data is actually received. After reading about this and a few other limitations I figured I would just be making life more difficult for myself proceeding down this path.

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)
else
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 www.sparkfun.com, 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 www.CoolComponents.co.uk 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.

Components:
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.

Componets:
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;

if(!Serial.available())
{
//Serial.println("No Data");
return -1;
}
while (Serial.available() && i < dataLength)
{
int c = Serial.read();
serInStr[i++] = c;
}
Serial.println(serInStr);
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, http://www.arduino.cc, 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.