-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalProject
286 lines (237 loc) · 6.5 KB
/
FinalProject
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Final Project
Karina Kurmanbayeva, Mudassir Iqbal, Parth Soni
DIGF 2B03 Physical Computing
OCAD University
Created on April 8, 15
Based on:
Capacitive Sensing Library,Paul Badger,http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense
SimpleTimer Library for Arduino, Marcello Romani , http://playground.arduino.cc/Code/SimpleTimer
*/
#include <SimpleTimer.h>
#include <rgb_lcd.h> // Grove seeed RGB LCD library
#include <Wire.h>
#include <Servo.h>
#include <CapacitiveSensor.h> //capacitive sensor library
/// Timer variables
SimpleTimer timer;
/// Capasitive sensor
CapacitiveSensor cs_4_2 = CapacitiveSensor(4, 2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
long sensorValue;
int thresholdValue = 500;
int sensorStatus;
int pSensorStatus;
int touchCount = 0;
int pTouchCount = 0;
bool isTouching = false;
bool alreadyTouched = false;
// servo
Servo myservo;
const int ledPin = 12; //LED
/// Sound sensor/ mic
const int pinSound = A0;
int SoundSensorValue;
int characterState = 0;
bool imAngry = false;
rgb_lcd lcd;
void setup() {
Serial.begin(9600);
// timer
timer.setInterval(15000, RepeatTask);
timer.setTimer(1200, TenTimesTask, 10);
myservo.attach(9);
pinMode(ledPin, OUTPUT);
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); ////capasitive sensor callibration
/// set asleep mode as a default when program starts
DisplayAsleep();
}
void loop() {
///start reading capasitive sensor value
long start = millis();
sensorValue = cs_4_2.capacitiveSensor(30);
// Serial.print(millis() - start); // check on performance in milliseconds
// Serial.println("\t"); // tab character for debug window spacing
// Serial.print(sensorValue); // print sensor output 1
// Serial.println("\t");
sensorStatus = sensorValue;
CheckIfTouched();
DisplayAngry();
}
void CheckIfTouched() {
///if sensor is touched, start counting touches
if (sensorStatus > 10 && pSensorStatus <= 10 && isTouching == false) {
touchCount = touchCount + 1;
isTouching = true;
Serial.println("isTouching");
}else{
isTouching = false; /// sensor is not touched
}
/// if touched more than 5 times, reset counter back to 0
if (touchCount > 5) {
touchCount = 0;
}
/// if not touched yet, put it asleep
if (touchCount == 0 && alreadyTouched == true ) {
//Serial.println("once");
alreadyTouched = true;
TouchedZero();
}
// First Touch
if (sensorStatus > 10 && touchCount == 1 && alreadyTouched == false ) {
//Serial.println("once");
alreadyTouched = true;
TouchedOnce();
}
// Second Touch
if (sensorStatus > 10 && touchCount == 2 && alreadyTouched == true ) {
alreadyTouched = false;
TouchedTwice();
}
// Third Touch
if (sensorStatus > 10 && touchCount == 3 && alreadyTouched == false) {
alreadyTouched = true;
TouchedThree();
}
// Fourth touch
if (sensorStatus > 10 && touchCount == 4 && alreadyTouched == true) {
alreadyTouched = false;
TouchedFour();
}
//Fifth touch
if (sensorStatus > 10 && touchCount == 5 && alreadyTouched == false) {
alreadyTouched = true;
TouchedFive();
}
/// run timer when it is not sleeping, when it was touched at least once
if(isTouching == false && touchCount <= 5 && touchCount > 0){
timer.run();
}
pSensorStatus = sensorStatus; /// reset previos sensor value back to its output value
}
/// Touched Zero times
void TouchedZero(){
DisplayAsleep(); /// asleep
}
//// Touched Once
void TouchedOnce() {
//Serial.print("Woke Up");
DisplayAwake();
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
myservo.attach(9);
myservo.write(100);
delay(700);
myservo.detach();
}
/// Second Touch
void TouchedTwice() {
//Serial.print("Fully Awake");
DispalyHappy();
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
myservo.attach(9);
myservo.write(360);
delay(1000);
myservo.detach();
}
/// touched three times
void TouchedThree() {
//Serial.print("Irritated");
DisplayIrritated();
myservo.attach(9);
myservo.write(360);
delay(250);
myservo.detach();
}
/// Four
void TouchedFour() {
//Serial.print("Angry");
imAngry = true;
lcd.begin(16, 2);// set up the LCD's number of columns and rows:
lcd.setRGB(250, 0, 0); /// set RGB color
lcd.setCursor(0, 0);
lcd.print("I am ANGRY!");// Print a message to the LCD.
lcd.setCursor(0, 1);
lcd.print("=-[");
delay(500);
myservo.attach(9);
myservo.write(360);
delay(2000);
myservo.detach();
}
void TouchedFive() {
//Serial.print("OFF");
myservo.attach(9);
myservo.write(360);
delay(500);
myservo.detach();
}
/// LCD Speeping Mode
void DisplayAsleep(){
lcd.begin(16, 2);// set up the LCD's number of columns and rows:
lcd.setRGB(240, 240, 250); /// set RGB color
lcd.print("Zzz...Zzz..zZZ"); // Print a message to the LCD.
}
/// LCD Awake mode
void DisplayAwake(){
lcd.begin(16, 2);
lcd.setRGB(227, 208, 98);
lcd.print("I am awake");
}
/// LCD happy mode
void DispalyHappy(){
lcd.begin(16, 2);
lcd.setRGB(38, 255, 0);
lcd.print("I am happy!");
}
/// LCD irritated mode
void DisplayIrritated(){
lcd.setCursor(0, 0);
lcd.begin(16, 2);
lcd.setRGB(0, 21, 156);
lcd.print("You are annoying!");
lcd.setCursor(0, 1);
lcd.print("-_-");
}
// LCD angry mode
void DisplayAngry(){
/// read values from sound sensor
int SoundSensorValue = analogRead(pinSound);
//Serial.println(SoundSensorValue);
delay(100);
/// its is angry, cool it down by blowing on the sensor
if (SoundSensorValue > thresholdValue)
{
if(imAngry == true){
digitalWrite(ledPin, HIGH);
/// display on LCD
lcd.begin(16, 2);
lcd.setRGB(166, 169, 189);
lcd.print("Cooling Down");
delay(2000);
imAngry = false;
/// go back to happy mode when cooled down
TouchedTwice();
touchCount = 2;
}
}
}
///if it is not being touched for more than 15 seconds, fall back asleep
void RepeatTask() {
touchCount = 0;
isTouching = false;
CheckIfTouched();
//Serial.print("repeat");
}
// print timer
void TenTimesTask() {
static int k = 0;
k++;
Serial.print("called ");
Serial.print(k);
Serial.println(" / 15 times.");
}