Untitled
raw download clone
ARDUINO
views 18
,
size 3142 b
#include <Wire.h>
#include <ADXL345.h>
#include <Servo.h>
Servo myServo;
ADXL345 adxl;
int pos;
int delayTime;
int deg;
int trigPin=12;
int echoPin=11;
long duration,dist;

void setup(){
  Serial.begin(9600);
  adxl.powerOn();
  adxl.setActivityThreshold(75); //62.5mg per increment
  adxl.setInactivityThreshold(75); //62.5mg per increment
  adxl.setTimeInactivity(10); // how many seconds of no activity is inactive?
  adxl.setActivityX(1);
  adxl.setActivityY(1);
  adxl.setActivityZ(1);
  adxl.setInactivityX(1);
  adxl.setInactivityY(1);
  adxl.setInactivityZ(1);
  adxl.setTapDetectionOnX(0);
  adxl.setTapDetectionOnY(0);
  adxl.setTapDetectionOnZ(1);
  adxl.setTapThreshold(50); //62.5mg per increment
  adxl.setTapDuration(15); //625us per increment
  adxl.setDoubleTapLatency(80); //1.25ms per increment
  adxl.setDoubleTapWindow(200); //1.25ms per increment
  adxl.setFreeFallThreshold(7); //(5 - 9) recommended - 62.5mg per increment
  adxl.setFreeFallDuration(45); //(20 - 70) recommended - 5ms per increment
  adxl.setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT,   ADXL345_INT1_PIN );
  adxl.setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT,   ADXL345_INT1_PIN );
  adxl.setInterruptMapping( ADXL345_INT_FREE_FALL_BIT,    ADXL345_INT1_PIN );
  adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT,     ADXL345_INT1_PIN );
  adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT,   ADXL345_INT1_PIN );
  adxl.setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
  adxl.setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
  adxl.setInterrupt( ADXL345_INT_FREE_FALL_BIT,  1);
  adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,   1);
  adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
  myServo.attach(5,500,2400);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  pos=90;
  myServo.write(pos);
  delayTime=10;
  delay(1000);
}

void loop(){
  pos=myServo.read();
  //Serial.println(pos);
  int x,y,z;  
	adxl.readXYZ(&x, &y, &z); //read the accelerometer values and store them in variables  x,y,z
	Serial.print("x: ");
	Serial.println(x);
	digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  duration = pulseIn(echoPin,HIGH);
  dist = (duration / 2) / 29.4;
  if(dist<15){
    delayTime=10;
  }
  else{
    delayTime=40;}
  Serial.print("delay: ");
  Serial.println(delayTime);
	double xyz[3];
	double ax,ay,az;
	adxl.getAcceleration(xyz);
	ax = xyz[0];
	ay = xyz[1];
	az = xyz[2];
  Serial.print("ax: ");
	Serial.println(ax);
  //Serial.print(" ");
    //Serial.print(ay);
  //Serial.print(" ");
   //Serial.print(az);
  //Serial.println(" ");
	deg=(ax*90)+90;
  deg=constrain(deg,0,180);
  Serial.print("deg: ");
  Serial.println(deg);
  pos=myServo.read();
  if(deg-pos>0){
  for(double i=1;i<deg-pos;i++){
    myServo.write(pos+i);
    pos+=i;
    delay(delayTime);
    }
  }
  else if(deg-pos<0){
    for(double i=1;i<pos-deg;i++){
      myServo.write(pos-i);
      pos-=i;
      delay(delayTime);
    }
  }  
  //myServo.write(deg);
	delay(50);
}
close fullscreen
Login or Register to edit or fork this paste. It's free.