1
17
18int X_axis = A0;
19int Y_axis = A1;
20int Z_axis = A2;
21const int buzzerPin = 9;
22const int LEDpin = 8;
23const int btnPin = 7;
24const int deg_acc = 3;
25boolean trigAlarm = false;
26int x, y, z;
27
28int STILL[4];
29
30void setup() {
31 analogReference(EXTERNAL);
32 pinMode(buzzerPin, OUTPUT);
33 pinMode(LEDpin, OUTPUT);
34 pinMode(btnPin, INPUT);
35 Serial.begin(9600);
36 while (digitalRead(btnPin) != HIGH) {}
37}
38
39void loop() {
40 if (digitalRead(btnPin) == HIGH) {
41 Serial.print("Calibrating");
42 delay(500);
43 int i = 0;
44 while (i < 3) {
45 Serial.print(".");
46 STILL[0] = analogRead(X_axis);
47 STILL[1] = analogRead(Y_axis);
48 STILL[2] = analogRead(Z_axis);
49 delay(500);
50 i++;
51 }
52 digitalWrite(LEDpin, HIGH);
53 tone(buzzerPin, 2000);
54 delay(100);
55 digitalWrite(LEDpin, LOW);
56 noTone(buzzerPin);
57 delay(100);
58 digitalWrite(LEDpin, HIGH);
59 tone(buzzerPin, 2000);
60 delay(100);
61 digitalWrite(LEDpin, LOW);
62 noTone(buzzerPin);
63 trigAlarm = false;
64 }
65
66 x = analogRead(X_axis);
67 y = analogRead(Y_axis);
68 z = analogRead(Z_axis);
69
70
71 if ((x > (STILL[0] - deg_acc)) && (x < (STILL[0] + deg_acc)) && (y > (STILL[1] - deg_acc)) && (y < (STILL[1] + deg_acc)) && (z > (STILL[2] - deg_acc)) && (z < (STILL[2] + deg_acc)) ) {
72
73
74
75
76
77
78
79 }
80 else {
81 trigAlarm = true;
82 }
83
84 if (trigAlarm == true) {
85 digitalWrite(LEDpin, HIGH);
86 tone(buzzerPin, 2000);
87 delay(100);
88 digitalWrite(LEDpin, LOW);
89 noTone(buzzerPin);
90 }
91 delay(100);
92}
93