-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccelerometerControlsPanTilt.ino
65 lines (49 loc) · 1.29 KB
/
AccelerometerControlsPanTilt.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <Wire.h> //Include the Wire library
#include <MMA_7455.h> //Include the MMA_7455 library
#include <Servo.h>
//Make an instance of MMA_7455
MMA_7455 accelerometer = MMA_7455();
//Variables for the values from the Accelerometer Ssensor
char xVal, yVal, zVal;
//Servo Instances
Servo tiltservo;
Servo panservo;
int serialcount = 0;
void setup()
{
//Activate Serial
Serial.begin(9600);
delay(1000);
// Set the sensitivity For Accelerometer
// 2 = 2g, 4 = 4g, 8 = 8g
accelerometer.initSensitivity(2);
//Calibrate Offset of Accelerometer
accelerometer.calibrateOffset(-1.5, 10.0, -11.0);
//Attach Servo
tiltservo.attach(9);
panservo.attach(10);
}
void loop()
{
//Read Values
xVal = accelerometer.readAxis('x'); //Read out the 'x' Axis
yVal = accelerometer.readAxis('y'); //Read out the 'y' Axis
zVal = accelerometer.readAxis('z'); //Read out the 'z' Axis
//Write Values to Servo
tiltservo.write(90 + xVal);
panservo.write(90 - yVal);
//Write Serial Debug Info every 50th Cycle (500ms)
if(serialcount <= 29){
serialcount++;
}
else{
Serial.print("X = ");
Serial.print(xVal, DEC);
Serial.print(" Y = ");
Serial.print(yVal, DEC);
Serial.print(" Z = ");
Serial.println(zVal, DEC);
serialcount = 0;
}
delay(20);
}