Skip to content

Instantly share code, notes, and snippets.

@B45i
Last active January 5, 2018 10:21
Show Gist options
  • Select an option

  • Save B45i/5510fb6aaac644e06b80c6895e2aec34 to your computer and use it in GitHub Desktop.

Select an option

Save B45i/5510fb6aaac644e06b80c6895e2aec34 to your computer and use it in GitHub Desktop.
// Maurice Ribble
// 6-28-2009
// http://www.glacialwanderer.com/hobbyrobotics
// This app just probes two external sensors (ADXL330 and IDG300) and
// then sends that data over a serial connection. I used a sparkfun
// breakout board to make using these chips easier.
// http://www.sparkfun.com/commerce/product_info.php?products_id=741
// I wrote a processing.org app that reads in this data and graphs it
// on a pc.
#define X_ACCEL_APIN 0
#define Y_ACCEL_APIN 1
#define Z_ACCEL_APIN 2
void setup()
{
Serial.begin(115200);
}
// If this is defined it prints out the FPS that we can send a
// complete set of data over the serial port.
//#define CHECK_FPS
void loop()
{
int xAccel=0, yAccel=0, zAccel=0;
unsigned int startTag = 0xDEAD; // Analog port maxes at 1023 so this is a safe termination value
int loopCount;
#ifdef CHECK_FPS
unsigned long startTime, endTime;
startTime = millis();
#endif
// Can't do more than 64 loops or could overflow the 16 bit ints
// This just averages together as many sensor reads as we can in
// order to reduce sensor noise. Might want to introduce add
// a smarter filter her in the future.
loopCount = 12; // 12 gives a little over 100 FPS
for(int i = 0; i< loopCount; ++i)
{
// It takes 100 us (0.0001 s) to read an analog input
xAccel += analogRead(X_ACCEL_APIN);
yAccel += analogRead(Y_ACCEL_APIN);
zAccel += analogRead(Z_ACCEL_APIN);
}
xAccel /= loopCount;
yAccel /= loopCount;
zAccel /= loopCount;
Serial.print(xAccel);
Serial.print(",");
Serial.print(yAccel);
Serial.print(",");
Serial.println(zAccel);
#ifdef CHECK_FPS
endTime = millis();
Serial.print(" - FPS: ");
Serial.println(1.f / (endTime-startTime) * 1000);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment