accelerometer - v4 — Arduino Nano simulation

(No buttons)

An interactive Arduino Nano circuit simulation you can run free in your browser on Velxio, by a36274595.

Components

Sketch code

#include <SparkFun_ADXL345.h>

ADXL345 adxl = ADXL345();

int range = 16; // Range: 2g, 4g, 8g, 16g

void setup() {
  Serial.begin(9600);

  adxl.powerOn();
  adxl.setRangeSetting(range);
}

void loop() {

  int x, y, z;
  adxl.readAccel(&x, &y, &z);

  switch (range) {
    case 16:
      x = x * 3.9;
      y = y * 3.9;
      z = z * 3.9;
      break;

    default:
      Serial.println("Please specify a range of 2, 4, 8, or 16 g");
  }




  // Make H, V and D the opposite of X, Y and Z
  //Making the -180 to 180 evenly spread across the X, Y and Z (which is divided byy 11)
 int horizontal = -x / 11;
 int vertical = -y / 11;
 int depth = -z / 11;


// making a limit to 10 and -180 
//(constrain means limit) important note for later
horizontal = constrain(horizontal, -180, 180);
vertical = constrain(vertical, -180, 180);
depth = constrain(depth, -180, 180);


//Printing It all out
 Serial.print("X: ");
  Serial.print(x);
  Serial.print("     Horizontal: ");
  Serial.print(horizontal);

  Serial.print("         Y: ");
  Serial.print(y);
  Serial.print("     Vertical: ");
  Serial.print(vertical);

  Serial.print("         Z: ");
  Serial.print(z);
  Serial.print("     Depth: ");
  Serial.println(depth);


  delay(250);
}

More projects by a36274595