More Accelerometer questions / Geocache viability

Hello,

I've been tinkering with an accelerometer (http://www.robotsimple.com/MMA7341L_3_Axis_Accelerometer_3_11g?keyword=MMA7341L) with an Arduino Uno and was hoping I could get some answers to a few questions.

I've adapted one of the tutorials found here to add a Z axis and tweaked it a little.

/*
  
   
   The circuit:
    * X output of accelerometer to digital pin 2
    * Y output of accelerometer to digital pin 3
    * Z output of accelerometer to digital pin 4
    * +V of accelerometer to +3.3V pin
    * GND of accelerometer to ground
    
   Adapted slightly from this tutorial
   http://www.arduino.cc/en/Tutorial/Memsic2125
   
 
 */

// these constants won't change:
const int xPin = 2;     // X output of the accelerometer
const int yPin = 3;     // Y output of the accelerometer
const int zPin = 4;

void setup() {
  // initialize serial communications:
  Serial.begin(9600);
  // initialize the pins connected to the accelerometer
  // as inputs:
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(zPin, INPUT);
}

void loop() {
  // variables to read the pulse widths:
  int pulseX, pulseY, pulseZ;
  // variables to contain the resulting accelerations
  int accelerationX, accelerationY, accelerationZ;
 
  // read pulse from x- and y-axes:
  pulseX = pulseIn(xPin,HIGH);  
  pulseY = pulseIn(yPin,HIGH);
  pulseZ = pulseIn(zPin,HIGH);
 
  // convert the pulse width into acceleration
  // accelerationX and accelerationY are in milli-g's:
  // earth's gravity is 1000 milli-g's, or 1g.
  accelerationX = (((pulseX / 10)  -500) * 8)-716; //I added the last value (-716) to try and calibrate the milli-g reading to 0
  accelerationY = (((pulseY / 10) - 500) * 8)-720; //I added the last value (-720) to try and calibrate the milli-g reading to 0
  accelerationZ = (((pulseZ / 10) - 500) * 8)-712; //I added the last value (-712) to try and calibrate the milli-g reading to 0

  // print the acceleration
  Serial.print(accelerationX);
  // print a tab character:
  Serial.print("\t");
  Serial.print(accelerationY);
   Serial.print("\t");
   Serial.print(accelerationZ);
  Serial.println();

  delay(100);
}

Everything is “working” but I don't quite understand the numbers. When it's just sitting at rest the readings from the serial port are hovering around 0, but whenever I reach to touch the Arduino, the numbers jump to around -300 without my actually touching the Arduino. I'm still a novice with this thing, but recently when doing a tutorial on buttons I learned that buttons had to be debounced. Is that same thing happening here? If so, I could use some guidance on that, I can't find many tutorials on accelerometers.

For a project I'm working on (more about it below if you're interested) I would like the accelerometer readings to be a little more predictable. Right now I can set the arduino down in place or one certain position (propped up against a wall or on a book etc...) and the readings are never quite the same two times in a row. Is the accelerometer taking too fine of readings? And if that's the case is there a way to calibrate it so it takes more coarse readings?

Like I said, I'm a novice with these things. I'm primarily a geocacher with a lot of ambition and ideas, and not a whole lot of electronics skill. (Just enough to get myself in trouble, but I'm learning!) Here's what I want to happen eventually: I want a cube with an on/off switch and an LED that lights either green or red and a buzzer and a servo. The finder of my geocache turns the box on with a toggle switch and the LED lights red (indicating that they need to turn it to the correct side). The finder then rotates the box so a certain side of the box is up (each side has a different color, the finder will first have to solve an online puzzle that will give them the correct procedure/hints on how to open the box), and then they tap a predetermined number of knocks on that side, (ie. blue side 5 knocks, red side 3 knocks etc..) and if they knock the right number of times on the correct side the LED flashes green, if not red. Then the finder rotates the box again to the next correct side and taps again. After 4 or so times of the finder turning and tapping in the right order etc... the box chimes a nice DING and a servo moves a bar and allows the box to spring open. If during the procedure the finder gets something wrong the box sounds off with a negative sound and flashes the LED red. I have all the parts and I'm assembling them now, but the learning of the code is holding me back.

Any help or advice is greatly appreciated.

Cheers, DJ

Can you post your schematic as well? Hard to say what software will do not knowing how hardware is hooked up.

Here's how I've wired it.

Can you make the picture a little larger so we can read it?

You may want to look into using a digital accelerometer instead, as some of them have built in algorithms for orientation detection and tap detection which could cut down the amount of code you need to write.

Thanks for the heads up. I resized the pic again. I noticed it earlier but for some reason photobucket didn't like it when I resized it earlier, I had to change the name of the photo. The less code I have to write the better.

I would start by connecting the second GND pin on the accelerometer.

Also, that sketch won't work for this accelerometer. The accelerometer you are using is analog and your sketch is written for an accelerometer that has a PWM output.

Need to do this too:
"The sleep pin, Sleep, is internally pulled low, which puts the board into low-power sleep mode by default. You must drive this pin high to use the board. This can be accomplished with a microcontroller I/O line if you want selective control of sleep mode, or you can make a solder bridge across the SMT jumper pads labeled “ON” on the silkscreen side of the board to connect the sleep pin to VDD and enable the board by default."

Yes indeed, need to read inputs on the analog lines:
"The accelerometer X, Y, and Z outputs are three separate analog voltages centered at VDD/2. Positive accelerations along an axis increase that axis’s output voltage above VDD/2 and negative accelerations decrease the output voltage below VDD/2. The outputs will always be within the range of 0 to VDD."