Skip to content

Instantly share code, notes, and snippets.

@kobeBigs
Last active December 17, 2015 12:09
Show Gist options
  • Save kobeBigs/5607938 to your computer and use it in GitHub Desktop.
Save kobeBigs/5607938 to your computer and use it in GitHub Desktop.
arduino sketch to test ADXL3xx Accelerometer and LED
// these constants describe the pins. They won't change:
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)
const int x_max = 512;
int ledPin = 8; //pin for led
//setup
void setup(){
pinMode(ledPin, OUTPUT); // set LED on output mode
Serial.begin(9600); //start serial communication
}
//loop
void loop(){
//read values from x, y, z pins
int xVal = analogRead(xpin);
int yVal = analogRead(ypin);
int zVal = analogRead(zpin);
//compare values to x_max and trigger LED Blink
if(xVal > x_max){
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
} else{
digitalWrite(ledPin, LOW);
}
//print sensor values
Serial.print(xVal);
Serial.print("\t");
Serial.print(yVal);
Serial.print("\t");
Serial.print(zVal);
Serial.println();
delay(1000); //wait a second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment