accelerometer - v3 — Arduino Nano simulation

(Included 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 = 2; // Range: 2g, 4g, 8g, 16g

// Positions all start at 0
// Angles alwys start at 45
int xPosition = 0;
int xHorizontal = 45;

int yPosition = 0;
int yVertical = 45;

int zPosition = 0;
int zHorizontalVertical = 45;

void setup() {

  Serial.begin(9600);

  adxl.powerOn();

  adxl.setRangeSetting(range);

  // Acknowledging the buttons

  pinMode(A0, INPUT_PULLUP);// x-1
  pinMode(A1, INPUT_PULLUP);// y-1
  pinMode(A2, INPUT_PULLUP);// z-1

  pinMode(A3, INPUT_PULLUP);// x+1
  pinMode(A4, INPUT_PULLUP);// y+1
  pinMode(A5, INPUT_PULLUP);// z+1

}

void loop() {

  int x, y, z;

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

  // A3 = X position +1 Horizental -1
  //x+1
  if (digitalRead(A3) == LOW) {
    xPosition++;
    xHorizontal--;
    delay(250);
  }

  //A0 = x position -1 Horizontal +1
  //x-1
   if (digitalRead(A0) == LOW) {
    xPosition--;
    xHorizontal++;
    delay(250);
  }

  // A4 = Y position +1 Vertical -1
  //y+1
  if (digitalRead(A4) == LOW) {
    yPosition++;
    yVertical--;
    delay(250);
  }

  //A1 = y position -1 Vertical +1
  //y-1 
   if (digitalRead(A1) == LOW) {
    yPosition--;
    yVertical++;
    delay(250);
  }

  // A5 = Z position +1 HorizontalVertical -1
  //x+1
  if (digitalRead(A5) == LOW) {
    zPosition++;
    zHorizontalVertical--;
    delay(250);
  }

  //30 = z position -1 Horizontal +1
  //x-1
   if (digitalRead(A2) == LOW) {
    zPosition--;
    zHorizontalVertical++;
    delay(250);
  }

//X button
Serial.print("X: ");
Serial.print(xPosition);
Serial.print("     Horizontal:");
Serial.print(xHorizontal);

//Y button
Serial.print("                  Y: ");
Serial.print(yPosition);
Serial.print("     Vertical:");
Serial.print(yVertical);

//Z button
Serial.print("                  Z: ");
Serial.print(zPosition);
Serial.print("     HorizontalVertical:");
Serial.println(zHorizontalVertical);

delay(250);
}

More projects by a36274595