Step 7: Upload the Code

Though you don't need a computer to run the project itself, programming the Arduino requires some software on a computer.  Don't worry, it's all cross platform and easy to use.

1) Arduino software

2) Add Arduino MMA7361 Library
... or other, if you have a different accelerometer

3) Upload the following code to the Arduino board (it is based off the examples in the MMA 7361 Library):

#include <AcceleroMMA7361.h>

AcceleroMMA7361 accelero;
int x;
int y;
int z;
int pitch;

void setup()
{
  Serial.begin(9600);
  accelero.begin(13, 12, 11, 10, A0, A1, A2);
  accelero.setARefVoltage(3.3);                   //sets the AREF voltage to 3.3V
  accelero.setSensitivity(LOW);                   //sets the sensitivity to +/-6G
  accelero.calibrate();
}

void loop()
{
  x = accelero.getXRaw();
  y = accelero.getYRaw();
  z = accelero.getZRaw();
  Serial.print("\nx: ");
  Serial.print(x);
  Serial.print("\ty: ");
  Serial.print(y);
  Serial.print("\tz: ");
  Serial.print(z);

  //finalzie pitch
  pitch = (x*y/z) * 50;

  //play the tone
  tone(3, pitch, 10);

  delay(10);                                     //(make it readable)
}
 
Remove these adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!