#define BAUD_RATE 9600 #define GROUND 18 #define POWER 19 #define X 3 #define Y 2 #define Z 1 #define FIVE_VOLTS 0x01 #define GND 0x00 #define SMOOTH_LIMIT 10 #define BLUE "\033[34m" #define LINE1 "\033[10;5H" #define LINE2 "\033[11;5H" #define LINE3 "\033[12;5H" #define NORMAL "\033[0m" /* --------------- The Sparkfun ADXL3xx Accelerometer ---------------- Original code and tutorial at http://www.arduino.cc/en/Tutorial/ADXL3xx created 2 Jul 2008 by David A. Mellis modified 26 Jun 2009 by Tom Igoe modified 23 Feb 2010 by Roy Vanegas A note about the very clever use of pins 4 and 5 as a power line. By setting both pins up as outputs, then keeping pin 4 low (for ground) and pin 5 high (for 5 volts), the pins become a power rail for the accelerometer. Very, very smart. This modified version of the original code (found on the Arduino Web site linked above) generates some text in blue for easier output viewing in the terminal on a Mac or Linux. It also stations the output in one location on the terminal, as opposed to scrolling the data continuously on the screen. To run this program in the terminal on a Mac: 1. Launch the terminal. 2. Inspect the devices connected to the USB ports on your machine: ls /dev/tty.* Your Arduino will appear as a serial port. For example, mine is /dev/tty.usbserial-A10?15f8. 3. Run the screen program with the Arduino USB port as an argument to the screen command. For example, I run screen as: screen /dev/tty.usbserial-A10?15f8 This program also adds a 10-point smooth to the x, y, and z readings. To modify the amount of smoothing, change the text to the right of the symbolic constant SMOOTH_LIMIT. For example, if I want a 5-point smooth, I'll change 10 to 5. Roy Vanegas r.o.y@nyu.edu */ void setup() { Serial.begin( BAUD_RATE ); pinMode( GROUND, OUTPUT ); pinMode( POWER, OUTPUT); digitalWrite( GROUND, GND ); digitalWrite( POWER, FIVE_VOLTS ); } void loop() { static unsigned int count; static int x, y, z; x += analogRead( X ), y += analogRead( Y ), z += analogRead( Z ); if( count++ % SMOOTH_LIMIT == 0 ) { Serial.print( BLUE LINE1 "X : " NORMAL ); Serial.print( x / SMOOTH_LIMIT ); Serial.print( BLUE LINE2 "Y : " NORMAL ); Serial.print( y / SMOOTH_LIMIT ); Serial.print( BLUE LINE3 "Z : " NORMAL ); Serial.print( z / SMOOTH_LIMIT ); x = y = z = 0; } delay( 5 ); }