I need to make my accelerometer, a Memsic2125, take a reading and then use that to change the pitch of a piezo speaker. I have almost no idea what I'm doing though. I started with the example for the Mesmic and I think I need to make the accelerationX and accelerationY values into one value to use as an input into the speaker and then use the map function? Thanks for the help!
Here's what I have right now:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
const int speaker = 4;
void setup() {
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(speaker, OUTPUT);
}
void loop() {
// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;
// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,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; // does * mean multiplication?
accelerationY = ((pulseY / 10) - 500) * 8;
int val = accelerationX * accelerationY;
int speaker = map(val, -1250, 1250, 100, 1000); // 1250 was the about highest value i could get by shaking it