How to connect an accelerometer ADXL345 to 2 servos


#1

In this short video we explain how to connect an accelerometer to two servo generic metal gears with the help of circuito.io.
After you complete the wiring, you can use the code below to make a project like the one in the video.
Replace the test code you got in the original reply from circuito.io with the one below (firmware tab in Arduino IDE)

#include "Global.h"

const int ServoRestPosition  = 20;	//Starting position
const int ServoTargetPosition = 150;	//Position when event is detected
#define THRESHOLD_ADXL345	30

const int numServo1readings = 10;
const int numServo2readings = 10;

int Servo1readings[numServo1readings];      // the Servo1readings from the analog input
int Servo1readIndex = 0;              // the index of the current reading
int Servo1total = 0;                  // the running Servo1total
int Servo1average = 0;                // the average

int Servo2readings[numServo2readings];      // the Servo1readings from the analog input
int Servo2readIndex = 0;              // the index of the current reading
int Servo2total = 0;                  // the running Servo1total
int Servo2average = 0;                // the average


/* This code sets up the essentials for your circuit to work. It runs first every time your circuit is powered with electricity. */
void setup() {
    // Setup Serial which is useful for debugging
    // Use the Serial Monitor to view printed messages
    Serial.begin(9600);
    Serial.println("start");
     
    servo_1.attach(SERVO_1_PIN); 
    servo_2.attach(SERVO_2_PIN);
  for (int thisReading = 0; thisReading < numServo1readings; thisReading++) {
    Servo1readings[thisReading] = 0;
  }
    

}

/* This code is the main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop. */
void loop() {

  Servo1total = Servo1total - Servo1readings[Servo1readIndex];
  Servo2total = Servo2total - Servo2readings[Servo2readIndex];
  // read from the sensor:
  Servo1readings[Servo1readIndex] = adxl.getX();
  Servo2readings[Servo2readIndex] = adxl.getY();
  // add the reading to the Servo1total:
  Servo1total = Servo1total + Servo1readings[Servo1readIndex];
  Servo2total = Servo2total + Servo2readings[Servo2readIndex];
  // advance to the next position in the array:
  Servo1readIndex = Servo1readIndex + 1;
  Servo2readIndex = Servo2readIndex + 1;

  // if we're at the end of the array...
  if (Servo1readIndex >= numServo1readings) {
    // ...wrap around to the beginning:
    Servo1readIndex = 0;
  }


  if (Servo2readIndex >= numServo2readings) {
    // ...wrap around to the beginning:
    Servo2readIndex = 0;
  }


  // calculate the average:
  Servo1average = Servo1total / numServo1readings;
  Servo2average = Servo2total / numServo2readings;
  // send it to the computer as ASCII digits
  Serial.println(Servo1average);
  Serial.println(Servo2average);
    delay(10);
    
    // Update current accelerometer values
    adxl.update();
    

    
  int val = Servo1average;
  val = map(val, -130, 130, 10, 170);
  servo_1.write(val);  // 1. turns servo to target position. Modify target position by modifying the 'ServoTargetPosition' definition above.
    int val1 = Servo2average;
  val1 = map(val1, -130, 130, 10, 170);
  servo_2.write(val1);  // 1. turns servo to target position. Modify target position by modifying the 'ServoTargetPosition' definition above.

}