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.


t0ne said...

Cool. How are you planning on using the accelerometer with this ?
Some pics of what you made would be nice to see too :)

aj said...

Hi, check out the next post it probably gives you an idea where I am heading. Probably should have mentioned that in the first post somewhere, This might be a bit waffly and disjointed at the moment. May look at more a tutorial type arrangement when finished, when I can look back with some authority on the subject. Will put some pics up at some stage, dismantled the first stage before taking any, will rebuild for a pic or two.