-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinal Code
More file actions
143 lines (123 loc) · 4.44 KB
/
Final Code
File metadata and controls
143 lines (123 loc) · 4.44 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* This code operates the Tracker Turtle device. It reads the acceleration from the
Lightblue Bean embedded in the turtle, on sounds a buzzer and lights up LED's when
the acceleration passes a certain threshold. Additionally, the Tracker Turtle
communicates with the user's phone to turn the lock state on and off and send
a notification when the acceleration reaches the threshold*/
//Threshold for acceleration
#define THRESHOLD 135
//Defines the size and combonation of the key code to lock and unlock the Turtle
#define KEYCODE_SIZE sizeof(keycode)
const char keycode[] = {13, 14, 15, 16};
//variables for determining acceleration
AccelerationReading previousAccel;
AccelerationReading defaultAccel;
//"Theft" is used for when the Turtle is armed and the acceleration threshold is breached
boolean theft;
//Digital pins for speaker and LED's
int speaker_Low = 0;
int speaker_High = 3;
int LED1_Low = A0;
int LED1_High = A1;
int LED2_Low = 4;
int LED2_High = 5;
//Messages Turtle sends to phone
uint8_t SAFE[] = {'S', 'A', 'F', 'E', ' '};
uint8_t THEFT[] = {'T', 'H', 'E', 'F', 'T'};
void setup() {
Serial.begin(57600);
Serial.setTimeout(25);
// Sets speaker and LED's as outputs
pinMode(speaker_Low, OUTPUT);
pinMode(speaker_High, OUTPUT);
pinMode(LED1_Low, OUTPUT);
pinMode(LED1_High, OUTPUT);
pinMode(LED2_Low, OUTPUT);
pinMode(LED2_High, OUTPUT);
//Starts with speaker and LED's as off
digitalWrite(speaker_Low, LOW);
digitalWrite(speaker_High, LOW);
digitalWrite(LED1_Low, LOW);
digitalWrite(LED1_High, LOW);
digitalWrite(LED2_Low, LOW);
digitalWrite(LED2_High, LOW);
//Initial reading of acceleration is starting state of Turtle
previousAccel = Bean.getAcceleration();
//starts with theft variable as false
theft = false;
}
void loop() {
//Locking and unlocking turtle:
char buffer[10];
size_t length = 10;
static char last_value = 0;
static char index = 0;
static char lock_state = 0;
length = Serial.readBytes(buffer, length);
if( length > 0 ){
if(buffer[0] != last_value){
if(buffer[0] == keycode[index]){
index++;
if(index == KEYCODE_SIZE){
// Lock / Unlock Turtle
lock_state = !lock_state;
index = 0;
defaultAccel = Bean.getAcceleration();
Bean.setScratchData(1, SAFE, 5);
}
}else{
index = 0;
}
}
last_value = buffer[0];
}
//If Turtle is locked but not moved, the light is set to green
if(lock_state){
Bean.setLed(0, 255, 0);
}
//If the turtle is not locked, the light is off
else{
Bean.setLed(0, 0, 0);
}
// Get the current acceleration with a conversion of 3.91×10-3 g/unit.
AccelerationReading currentAccel = Bean.getAcceleration();
// Find the difference between the current acceleration and that of 200ms ago.
int accelDifference = getAccelDifference(previousAccel, currentAccel);
// Update previousAccel for the next loop.
previousAccel = currentAccel;
// Check if the Bean has been moved beyond our threshold.
//if acceleration passes the threshold while locked:
if (lock_state && accelDifference > THRESHOLD) {
if (theft == false) {
Serial.print("THEFT");
}
Bean.setScratchData(1, THEFT, 5); //Send "THEFT" to scratch 1 in light blue app
theft = true;
//Speaker sounds and LEDs light red for 2 seconds
digitalWrite(speaker_Low, LOW);
digitalWrite(speaker_High, HIGH);
digitalWrite(LED1_Low, LOW);
digitalWrite(LED1_High, HIGH);
digitalWrite(LED2_Low, LOW);
digitalWrite(LED2_High, HIGH);
Bean.setLed(255, 0, 0);
Bean.sleep(2000);
//After 2 seconds, return to normal state if LED is back in origional position
digitalWrite(speaker_High, LOW);
digitalWrite(LED1_High, LOW);
digitalWrite(LED2_High, LOW);
previousAccel = defaultAccel;
}
else {
theft = false;
}
//Sleeps every 2ms to check for accerelation difference
Bean.sleep(200);
}
// This function calculates the difference between two acceleration readings
int getAccelDifference(AccelerationReading readingOne, AccelerationReading readingTwo){
int deltaX = abs(readingTwo.xAxis - readingOne.xAxis);
int deltaY = abs(readingTwo.yAxis - readingOne.yAxis);
int deltaZ = abs(readingTwo.zAxis - readingOne.zAxis);
// Return the magnitude
return deltaX+ deltaY + deltaZ;
}