Chromatic noise/PNG/compass sensor - usb/adapter power

This works with the 2x16 serial display from parallax. The function playnote takes any number between one and 150. I also have a compass connected so I can tell which direct is that I'm pinging, but the out is incomprehensible. This is designed to work with png sensor. For some reason my project only works when the device is connected through USB and not when connect to a 9 volt adapter.

I'm using..
1 Arduino Uno rev 3
1 2x16 serial display from parallax
1 PING))) Ultrasonic Distance Sensor from parallax
1 Compass Module 3-Axis HMC5883L

photo is attached.

const int TxPin = 6;
const int pingPin = 11;
unsigned int scale, duration, inches;
const int sensitivity = 3;
#include <SoftwareSerial.h>
#include <Wire.h>
#define Addr 0x1E // 7-bit address of HMC5883 compass
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

void setup() {
int scale = 0;
mySerial.begin(9600);
delay(100);
Wire.begin();
// Set operating mode to continuous
Wire.beginTransmission(Addr);
Wire.write(byte(0x02));
Wire.write(byte(0x00));
Wire.endTransmission();

pinMode(TxPin, OUTPUT);
digitalWrite(TxPin, HIGH);
mySerial.write(17); // Turn backlight on

}

void playNote(int myDistance){
int note = floor(myDistance/sensitivity);
// String sa3;
if(note>=1&&note<=12){
scale = 0;
}
if(note>=13&&note<=24){
scale = 1;
}
if(note>=25&&note<=36){
scale = 2;
}
if(note>=37&&note<=48){
scale = 3;
}
if(note>=49&&note<=60){
scale = 4;
}

int playNote = 219 + (note-(scale12));
int offset = note-(scale
12);
int playScale = 215 + scale;
mySerial.write(playScale); //bass low octave
mySerial.write(208);
mySerial.write(playNote);
mySerial.print(inches); // 64th

}

void loop() {
int x, y, z;
mySerial.write(12);

// Initiate communications with compass
Wire.beginTransmission(Addr);
Wire.write(byte(0x03)); // Send request to X MSB register
Wire.endTransmission();

Wire.requestFrom(Addr, 6); // Request 6 bytes; 2 bytes per axis
if(Wire.available() <=6) {
// If 6 bytes available
mySerial.print("Wire av");
x = Wire.read() << 8 | Wire.read();
z = Wire.read() << 8 | Wire.read();
y = Wire.read() << 8 | Wire.read();
}

// Print raw values
mySerial.print("X=");
mySerial.print(x);
mySerial.print(", Y=");
mySerial.print(y);
mySerial.print(", Z=");
mySerial.println(z);

pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
digitalWrite(pingPin, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 mi1crosecond burst
digitalWrite(pingPin, LOW); // End ranging
pinMode(pingPin, INPUT); // Set pin to INPUT
duration = pulseIn(pingPin, HIGH); // Read echo pulse
inches = duration / 74 / 2;
int inverseIN = 150 - inches;
playNote(inverseIN);

}

project1.png

It would help if you could say more than "only works when connected through USB". Exactly what doesn't work on 9V? Does the power LED light up?

Well the Arduino board itself lights up. There's a little orange LED that comes on which an L next to it and a second green LED thats says on next to it.
When it's plugged into the USB port though everything comes on and the all the devices work as expected.

I was curious if this has something to do with do with the pins I was connecting to. DO need to connect to 0 and 1 for the serial display to work away form the computer?
Neal

You aren't using the hardware Serial port as far as I can see, so pins 0 and 1 are unused.

At the moment you don't have any way to get diagnostic info out, except via the display. So if the display doesn't work, you're stuck.

I suggest you use the LED connected to pin 13 to provide diagnostic info. To start with, just run a blink sketch which toggles pin 13 at regular intervals and make sure the Arduino is actually running OK on your power supply. If so, add some code to setup() to output a recognisable sequence of flashes so you can tell when the power has reset. Since you have several things going on in setup I would put a different flash sequence at the end of each significant step so you can tell how far through the setup sequence it has got. For example, one flash at power up, two flashes when you have completed the wire setup, three flashes when the serial port has been initialised. I suggest you add code to display a test message on your serial display during setup, with a unique flash sequence afterwards, so you can find out (a) whether your sketch gets as far as running that code, and (b) whether the display outputs what you expect when it runs.

Remember that after outputting a flash sequence you will need a delay to separate it from the following flash sequence. If I were you, I'd write a function that takes a number argument specifying the number of flashes to produce, which just loops that number of times flashing the LED on and off once per loop, with a delay afterwards.