Wednesday, June 23, 2010

CNC Machine Build - Part I



I've started a 'little' project that promises to take up most of my free time (what little there seems to be of it at the moment) - I've decided to build a CNC machine.

To be honest, I've had gadget lust for a CNC machine for some time now... all the talk on the blogs about Cupcakes and Makerbots and RepRaps got me looking into CNC machines some time ago. I could never bring myself to spend the money on one though since they're a little pricey. The cheapest options that looked any good were the Zen Toolworks machines, which came with stepper motors but no power supply, controller boards or router/spindle. The cheapest of these can be got from Amazon for under 350 dollars. Still, it only works on a 7"x7"x2" area. The next machine up is around $600 and works on a larger area of 12"x12"x2". At this price I get a little scared and only being able to carve a foot is a little disapointing.

Anyway, I held off for ages but then came across these plans at solsylva.com which detailed the construction of a CNC machine that would cost a little bit more than the larger zen toolworks one (all inclusive), would use mostly materials from Lowes or Home Despot and also promised to be pretty easy to follow (a definite bonus for me, since I chose to do needlework and cookery rather than woodwork and metalwork when given the choice at age 13...). I've seen lots of online instructions for building various types of CNC machine (like this one, and this one, and this one... well, you get the idea), but they all left me a bit confused and unconfident about being able to build something worthwhile.

The promise of the solsylva plans were enough to make me order the 25"x25"x7" plans to see if they were idiot proof enough to give me the confidence to get started.

I received the plans a few weeks ago, read them over and then excitedly started to spend money on wood, bolts, washers, screws, pipes, all thread (threaded rod), forstner bits and skate bearings. So, yes, they appeared to be detailed and simple enough for me to follow from start to finish.

I like the fact that the solsylva plans allow for someone who doesn't have a truck. The wood measurements are for planks 8' and smaller, which fit fine in my little Nissan disappointment (with the back seats down). Recently I've been taking advantage of craigslist in order to pick up a few power tools on the cheap. I got hold of a drill-press ($50) and a miter saw ($60), which have been very useful so far. You'll laugh at me, but they were a bit intimidating to use at first - especially the miter saw. I kept having these images running through my mind of flying fingers and trips to the ER (in my defense, the miter saw is very loud). After a bit of practice I've become flamboyantly reckless much more confident. I'm definitely enjoying learning the new skills involved.

The pictures at the start of this post are the main base of the machine, put together over a few hours on the weekend. In the picture below I've added the X-rails and X-stepper motor support (this was done on the following weekend).



So far, it seems that things are going well. I'll keep you posted.

Wednesday, June 16, 2010

Upcycling: wine rack to doll bed



We found this little wine rack in the local thrift store (savers). Lin thought it'd be cool to turn it into a bed for the kids baby dolls. We bought an American Girl doll crib ages ago at a garage sale that was made of plastic and it inevitably fell to pieces... So the idea of the girls playing with a more sturdy one that I'd made was appealing, especially since they had a lot of make believe fun with the old one.

The idea was very simple: Take the rack apart, stick it back together again with some wood-glue for added strength, cut out a base, screw this on and then paint.

I was going to make a crib by adding doweling bars between the cross beams, but the way the rack was put together made this a bit too fiddly so I decided to just replace the bottom beams and make a baby bed instead.

I sanded the pieces to get rid of some of the murky dirt. Then I painted on some wood-glue to the ends of the beams, reattached them and left it all to dry overnight (with a toolbox weighing it all down from above so that it'd set straight).



I used one of our "new" purchases (a very old scroll saw) to cut out a base. Very straightforward really, just measure the distances on the wine rack, mark those out on some wood and then cut.



Ooo look, a rectangular bit of wood ;)

I drilled out holes at the points where the new base board met the cross beams and then fixed the base to the frame using some wood screws.



Then I let the kids paint on a layer of primer. I think they quite enjoyed it.




After the primer had dried I painted on a coat of gloss. Hopefully that'll keep it in good condition for a while.

I wasn't sure if the kids were taken with it or not until the next morning when a couple of their friends came over for a play date. "Dylan! Dylan! Come and see! I've got something AMAZING to show you!" cried Carys. To be honest I wasn't sure what she was going to show off, but was very touched to find out that it was the baby bed we'd made. Poor Dylan was a little bemused as to why she was so excited ;)

Monday, May 17, 2010

Pixie-Dust Bottles




It occurred to me whilst making some RGB nightlights for the kids that it'd be fun if they could make the lights change to whatever colour they wanted; kinda like the Philips LivingColors lamp, but about $100 cheaper. Lin suggested that having a few of these (say 5 - 7) would give the kids more room for imaginative fun, so I set about making a prototype and then a few more when that was up and working.

The kids love them (which is always good) and have been claiming that the bottles contain pixie dust (hence the title of this post).

Can you guess what Ffion's favourite colour is? She claims that this is the 'right colour' for pixie dust.


The hardware is pretty straight forward. I used a potentiometer with an in-built on/off switch, like this one, to simplify things. I thought it'd look much nicer than having a separate switch. The rest of the parts are the same as for the RGB nightlights project. The trickiest part was the coding, mainly because I'm doing it late at night when my brain isn't working quite so well...

Ingredients:



1. Potentiometer with switch
2. ATtiny45
3. 8-pin IC socket
4. coin cell battery holder
5. RGB LED (common anode) - I bought mine from EMSL. These are 10mm diffused RGB LEDs.
6. hookup wire, solder etc.

Methods:

I soldered the coin cell holder onto the potentiometer first.

Then I hooked up the RGB LED to the DIP socket. I'm using common anode LEDs so the long LED lead is soldered onto pin 8 of the IC socket. I have the RGB channels/leads connected to pins 5, 6 and 7 and the potentiometer output (middle pin from the pot) connected to pin 3.




After that, all you have to do is solder on the potentiometer's middle terminal to the IC socket and finish off connecting the ground and power connections.




And there we have it. Some kind of strange Borg eye ready to be put somewhere.

We had some plastic kids drink bottles sitting in the recycling bin - they're made from white plastic so I thought they'd diffuse the light nicely. I drilled a hole in the bottle cap for the potentiometer shaft and fitted it all together. Finally, I added a knob (radioshack purchase) to make it a bit prettier and easier to use.



Coding:

I'd already worked out how to do the software PWM (pulse width modulation) and how to measure a variable voltage input using ADC (analogue to digital conversion) so all that needed to be figured out, for this project, was how to map the ADC input values to the desired range of colours.

I wanted to let the kids cycle through the entire spectrum in much the same way the previous night-lights cycled automatically.

So, in the RGB nightlights project there were 6 transitions/steps:
stepRed channel valueGreen channel valueBlue channel valuechanging channelcolour range
125500increase GreenRed to Yellow
22552550decrease RedYellow to Green
302550increase BlueGreen to Cyan
40255255decrease GreenCyan to Blue
500255increase RedBlue to Purple
62550255decrease BluePurple to Red


If we use a 10-bit ADC we have 1024 values (2^10) available for mapping to colours in the above steps. This means that each step can contain 170 values/colours (1024/6 = 170.666...).

So, to map the ADC value to a colour we first assign it to one of the 6 steps and then use it to determine the value of the varying channel (Red/Green/Blue) for that step.

There are 6 steps and each step can have 170 ADC values associated with it. So I binned the ADC values into steps like so:
stepADC value
1< 170
2< 342
3< 512
4< 683
5< 854
6>= 854


Here's an example to clarify: Say the potentiometer is set just over half way, then the ADC value should be between 512 and 683; this would put us in step 4 where the red channel is off, the blue channel is completely on (255) and the green channel is varying (in this step the green levels are decreasing from 255 -> 0 as the ADC value increases from 512 -> 683). We calculate the green level by translating the ADC range to 0 - 170 (in this case by subtracting 512) and then multiplying by a scaling factor (255/170) to transform the 0 -> 170 range to a 0 -> 255 range. If we're increasing the level of the channel (steps 1, 3 and 5) then we just use this scaled value as the channel value. If we're decreasing the level of the channel (as is the case for step 4) then we inverse the scaled value by subtracting it from 255.

Here's the bit of code that sets the RGB values (see the complete listing here: selectableColourLight.c):


#define SCALING_RANGE 170
#define SCALING_FACTOR 255/SCALING_RANGE

...

void setRgbLevels(uint16_t pValue)
{
if(pValue < SCALING_RANGE)
{
mRgbValues[RED_INDEX] = 255;
mRgbValues[GREEN_INDEX] = pValue * SCALING_FACTOR;
mRgbValues[BLUE_INDEX] = 0;

}
else if(pValue < 342) //SCALING_RANGE * 2
{
mRgbValues[RED_INDEX] = 255 - ((pValue - SCALING_RANGE) * SCALING_FACTOR);
mRgbValues[GREEN_INDEX] = 255;
mRgbValues[BLUE_INDEX] = 0;
}
else if(pValue < 512) //SCALING_RANGE * 3
{
mRgbValues[RED_INDEX] = 0;
mRgbValues[GREEN_INDEX] = 255;
mRgbValues[BLUE_INDEX] = (pValue - 342) * SCALING_FACTOR;
}
else if(pValue < 683)//SCALING_RANGE * 4
{
mRgbValues[RED_INDEX] = 0;
mRgbValues[GREEN_INDEX] = 255 - ((pValue - 512) * SCALING_FACTOR);
mRgbValues[BLUE_INDEX] = 255;
}
else if(pValue < 854)//SCALING_RANGE * 5
{
mRgbValues[RED_INDEX] = (pValue - 683) * SCALING_FACTOR;
mRgbValues[GREEN_INDEX] = 0;
mRgbValues[BLUE_INDEX] = 255;
}
else
{
mRgbValues[RED_INDEX] = 255;
mRgbValues[GREEN_INDEX] = 0;
mRgbValues[BLUE_INDEX] = 255 - ((pValue - 854) * SCALING_FACTOR);
}
}

Monday, March 22, 2010

Saving a Disney Princess Remote Controlled Car

During a trip to Savers I spotted this Disney princess car:

I noticed the sensors in the head and tail lights, they looked like IR receivers. At first I thought the LED in the body of the car was an IR LED and that the IR receivers detected the IR light bouncing off objects in the cars path (not that the tail lights would make much sense in this context), but when I got it home and put batteries in it, the LED turned out to be just a colour changing LED put there for decoration.

A while back I headed over to Noisebride on a Monday night to check out their circuit hacking/soldering workshop (hosted by Mich Altman). During the workshop I put together one of Mitch's kits - "The Trippy RGB Waves" kit (here's my Noisebridge post if you're interested in reading about the experience). The point is that this gizmo uses an IR LED and an IR receiver to detect overhead objects. The LED pulses IR light at 38KHz (not to be confused with the actual frequency of IR light which is ~ 3THz or 3 x 1012 Hz) which the IR receiver detects if there's an object above the LED reflecting the pulses back down.

I wondered if the car has a similar set-up and was just missing the remote control (which would have a pulsing IR LED). First off I tried a regular remote control, which had no effect so then I tried pointing the Trippy RGB LED kit at the car and voila! It reacted to the light. So, I set about creating a wand/gun for the kids to use to interact with the car.

First off I tried setting up a 555 timer to pulse an IR LED at 38KHz. It worked but the signal was weak (maybe I got the values off a little). I decided to use a microcontroller instead. The ATtiny range are pretty cheap - I bought some ATtiny45s for $1.20 each which is ~3x the cost of a 555. The hardest part was finding/creating a housing for the circuit. I looked around and decided to make my own out of PVC piping. They look like tiny guns and work a treat :)

video

Methods:


Here are the final "guns".



Ingredients:


The pipe is 1/2" PVC piping from Lowes (Home Depot have it too).
  • PVC elbow joint
  • PVC pipe (cut to a v.small length 1 1/2")
  • PVC pipe cap
  • Coin battery holder (3v, 20mm)
  • 8-pin DIP socket
  • momentary push button
  • IR LED
  • ATtiny45 (originally made with an ATtiny13).

The plastic dome is the case from a 25c toy from our local taqueria. The base fits snuggly on the end of the elbow joint and I've used it to cover the battery holder in the final 'product'. The piping paraphernalia was all left over from the marshmallow-gun fun.

The wiring is all very straightforward. The hardest part was adjusting the elbow joint so that my coin cell holders fitted snuggly inside (and getting the pushbutton in place).

So, first off is to drill some holes: 1 in the end cap for the LED and one in the elbow joint for the pushbutton. Then I used a rotary tool (Proxxon) to carve out some of the innards of the elbow joint until the battery holder fitted snuggly inside.

Then I soldered one short and one long lead to the pushbutton and fitted it into the elbow joint (see below).



Then I soldered a long ground lead to the battery holder and connected the short positive wire from the pushbutton and placed the battery holder in the end of the elbow joint.



Solder on the DIP socket. Using a socket was a great choice for me because it turns out that the code I was running was not getting the best results from the car. Having the socket let me replace the uC after I'd worked out the kinks. I guess it'll also let me easily recover the uC when the kids are bored of this toy. I stripped a little bit of insulation from near to the end of the ground lead and soldered it to the ground pin (rather than adding a couple of wire ends at that point).



Then it's just a matter of connecting the LED, placing the uC in the socket and fitting the remaining piping.





I made two so both girls could play. Although that also opens up a huge opportunity to fight over who's in control as well...



And here's the code (for some reason the car reacted best if the IR was pulsed for ~170 microseconds with a 400 microsecond pause before the next set of pulses):
/*
* IrLedPulse.c
*
* Distributed under Creative Commons 3.0 -- Attib & Share Alike
*
* Created on: Dec 26, 2009
* Author: Paul
*/
#include <avr/io.h>
#include <avr/delay.h>

#ifndef F_CPU
#define F_CPU 1000000UL
#endif


// Use Timer 0 to pulse the IR LED at 38KHz
void pulseIr()
{
TCCR0A = 0 | (1 << COM0A0) | (1 << WGM01); // COM0A0=1 to toggle OC0A on Compare Match

TCCR0B = 0 | (1 << CS01); // 1/8 prescale
OCR0A = 104; // to output 38KHz on OC0A (PB0, pin 5)

_delay_us(170); // delay 170 microseconds

// turn off Timer0 to stop 38KHz pulsing of IR
TCCR0B = 0; // Stop Timer0 (turn off IR emitter)
TCCR0A = 0; // Disconnect OC0A from PB0 (pin 5)
}

int main(void)
{
DDRB |= (1 << PB0); // set PB0 to output
PORTB = 0xFF; // all PORTB output pins high (LED off).
while(1)
{
pulseIr();
_delay_us(400);
}
}

Sunday, February 28, 2010

The Amazing Dr Boardman's Colour Conundrum

"Roll up! Roll up! Try your hand at The Amazing Dr Boardman's Colour Conundrum!"





This is a little game I've been planning on making for quite some time. I finally got round to learning the necessary microcontroller details to pull it together. The basic idea is that that there are two, full colour 'bulbs'. One bulb lights up with a random color and the player tries to match this color by adjusting the RGB values of the second bulb (the bulbs are actually RGB LEDs with ping-pong ball diffusers). When the colors are similar enough then the player 'wins'.

This was quite hard to do for the younger kids, so I added a speaker to the mix & set it so that the sound frequency increased the closer the player got to the correct colour. It's still pretty hard, even with the speaker on.

I added a difficulty switch so that the daring/cocky can turn the speaker off.

Here's a little video of it in action. My 5 year old daughter kindly agreed to walk us through it.

video


How it was put together

Ingredients:


  • 3 potentiometers (20K ohm - but the absolute value isn't very important)
  • 3 knobs for the potentiometers
  • 8 ohm speaker
  • wooden box (I found this one at Savers)
  • perf board
  • 2 baby food jars
  • 2 ping-pong balls
  • 28 pin DIP socket
  • ATmega8
  • 2 RGB LEDs (common anode)
  • 2 toggle switches (for power and speaker on/off)
  • hook-up wire
Methods

First off drill holes in the ping pong balls, the box and in the baby food lids. Obviously, the size of the holes will depend on what you want to put through them so you'll have to work this out yourselves). Here's the layout I used:



There are 3 holes in a row for the potentiometers, two holes for the LEDs, one hole in the middle for the difficulty switch and one on the side for the power switch.

Next up is to solder leads to the pots. I used speaker hookup cable for the +ve and GND connections and then soldered 3 different coloured wires to the center terminals (I would have used blue instead of black, but I don't have any blue wire). These are going to control the red, green and blue levels of the 'player' RGB LED.



I sanded the LED lenses to make them diffused (I could have just bought diffused lenses... doh!) and then soldered the leads in place. I used some speaker cable again here too to keep things simple (and some heat shrink tubing to prevent shorts).



Then solder the DIP socket onto the perf board. Add the potentiometer leds and the power supply. All the wires were cut to be a little generous in length - I wasn't sure how everything was going to fit so I figured I'd play it safe and allow for wiggle room in the box.

I used one of the IC perfboards because I figured having the central Vcc and GND rails would make life easier as well as the already connected pads around each pin of the uC.



Attach the LEDs to the board:



Then add the speaker and difficulty switch (this just connects/disconnects the speaker from GND rather than being directly controlled by the microcontroller) and test the set-up for shorts, cold solder joints etc.

The LEDs are poked through the holes in the box without the ping-pong balls on (no surprise there). I had them on in the above photos to see how well they worked.

Place everything into the case and hot glue it all securely - I glued the switches and the LEDs in place.

I positioned the uC within easy reach so that I can tweak the software and update easily. Next time I think I'll just add programming headers to the board.



If you're very lucky then you're either artistically gifted or you have a talented partner in crime who can make your project look a hell of a lot better. Lin (my lovely lady and the hostess of filthwizardry.com) had the fabulous idea of creating a Coney Island theme for this game. I take no credit for how good it looks, that was all down to Lin. I think I was going to put it in some old Tea box and leave it at that. Thanks, hon! She was also responsible for the game name. I'm far too British to call myself amazing. Oh, and the American spelling of colour she claims was for symmetry purposes.



I drilled a few holes in the back to increase the apparent volume of the speaker. Also, it took me a while to work out how to keep the lid/base on the box (there were no fasteners built in, the lid was meant to be kept on by gravity). I'd almost given up and was going to attack it with nails when I realised I could simply drill some holes in a couple of washers and screw them into the base... I'm stupidly proud of that little idea. Ah, the little things...



Software

The code is available here on code.google.com.

The output from each potentiometer is read via ADC. These values are used as the 'player' RGB values and are compared with the 'game' RGB values. I started off using the euclidean distance of the RGB vectors as the difference metric, but - with kid friendly values - it meant that you could sometimes match red to green (and green to blue etc.). I ended up requiring each colour be within a defined distance for a successful match.

When a match is detected we turn off interrupts, flash the LEDs and modulate the speaker sound. Then switch interrupts back on and assign a new colour to the system RGB LED.

It uses double buffered software PWM for the LEDs and standard PWM for the speaker.

The only other trick is that the random seed is stored in EEPROM and incremented each time the uC is booted up. The incremented seed is fed to srand and from there we just use the rand() function. This way the game sequence is different each time (well, different for 256 games in a row anyway).

#include <avr/eeprom.h>
/*
* Use a variable stored in EEPROM to ensure the random color
* sequence changes from one game to the next.
*/
void initRand()
{
uint8_t vSeed = eeprom_read_word(0); // load last stored seed
srand(++vSeed); // increment and use value as seed
eeprom_write_word(0, vSeed); //store the new seed for next time
}