Skip to content

Instantly share code, notes, and snippets.

Created May 5, 2017 21:17
Show Gist options
  • Save anonymous/1aa25cb4c8f8525d0d818cbf09d5e3ae to your computer and use it in GitHub Desktop.
Save anonymous/1aa25cb4c8f8525d0d818cbf09d5e3ae to your computer and use it in GitHub Desktop.
//Name: Project02Arduino
//By: Claudia Escue
//Purpose: send the values from an accelerometer to a processing file
#include <Wire.h> // Must include Wire library for I2C
#include <SFE_MMA8452Q.h> // Includes the SFE_MMA8452Q library
MMA8452Q accel; // Default MMA8452Q object create. (Address = 0x1D)
//create variables for the x, y, and z acceleration
float xAcceleration;
float yAcceleration;
float zAcceleration;
char val;
void setup() {
Serial.begin(9600);
accel.init(); //initialize the accelerometer
}
void loop() {
//check if the accelerometer is available
if (accel.available()) {
accel.read(); // First, use accel.read() to read the new variables:
printCalculatedAccels();
Serial.println(); // Print new line every time.
}
}
void printCalculatedAccels() {
if(Serial.available() > 0){
char input = Serial.read();
val = Serial.read();
//display accelerations as "x, y, z" so that it can be read in processing
xAcceleration = accel.cx;
Serial.print(xAcceleration, 3);
Serial.print(",");
yAcceleration = accel.cy;
Serial.print(yAcceleration, 3);
Serial.print(",");
zAcceleration = accel.cz;
Serial.print(zAcceleration, 3);
delay(250);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment