I shot near the highest aperture I think.
So what settings should I be changing?Thanks.
Start in fill flash mode, adjusting flash power (I generally have to go to -.3 to -1.3 for most Nikon flash units) and then when you start getting good results, switch to manual mode and start by adding 1.5-2 stops of exposure with your shutter speed. Once you've done that, you can start playing around with opening up the aperture or bumping the ISO instead.
Here are some good generic links:
http://planetneil.com/tangents/flash-photography-techniques/1-natural-looking-flash/
http://planetneil.com/tangents/flash-photography-techniques/2-flash-ambient-light/
http://planetneil.com/tangents/flash-photography-techniques/3-dragging-the-shutter/
http://planetneil.com/tangents/2008/02/29/dragging-the-shutter-revisited/
http://planetneil.com/tangents/flash-photography-techniques/6-flash-outdoors/
http://planetneil.com/tangents/flash-photography-techniques/7-metering-techniques/
http://planetneil.com/tangents/flash-photography-techniques/8-flash-exposure-comp/
They're all variations on a theme, but it's worth going through them to see the effects of balancing flash power with ambient light. The only issue you'll have with the D40 is the inability to bounce the built-in flash off a wall or ceiling, especially behind you- I'd recommend looking at a speedlight, flash bracket and something like a better bounce card if you plan on doing a lot of portraiture work as a beginning step (and multiple speedlights (a la strobist.blogspot.com) or strobes (IMO a much better solution if not nearly as portable) if you're in it for the long haul.)