-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojekt_arduino.ino
139 lines (104 loc) · 3.16 KB
/
projekt_arduino.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
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
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
//sensors_event_t event;
//accel.getEvent(&event);
// global variables declaration
char val; // Data received from the serial port
float filteredX;
float filteredY;
float filteredZ;
bool state = true;
/* Get a new sensor event */
/*sensors_event_t event;
accel.getEvent(&event);*/
/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
const int buzz = 7;
const int press = A0;
void setup() {
// put your setup code here, to run once:
pinMode(press, INPUT);
pinMode(buzz, OUTPUT);
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(9600);
/* Initialise the sensor */
if(!accel.begin())
{
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
while(1);
}
/* Set the range to whatever is appropriate for your project */
accel.setRange(ADXL345_RANGE_16_G);
// accel.setRange(ADXL345_RANGE_8_G);
// accel.setRange(ADXL345_RANGE_4_G);
// accel.setRange(ADXL345_RANGE_2_G);
establishContact(); // send a byte to establish contact until receiver responds
}
void pressesing(){
if (Serial.available() > 0) { // If data is available to read,
val = Serial.read(); // read it and store it in val
if(val == '1') //if we get a 1
{
// prosesing ber om data och vi skickar den typ
}
delay(100);
}
else {
// gör ngt annat
delay(50);
}
}
void loop() {
/* Get a new sensor event */
sensors_event_t event;
accel.getEvent(&event);
int pressValue = analogRead(press);
// Serial.println(sensorValue);
float filteredX = smooth(filteredX, event.acceleration.x);
float filteredY = smooth(filteredY, event.acceleration.y);
float filteredZ = smooth(filteredZ, event.acceleration.z);
String data = "X:" + String(filteredX) + "," + "Y:" + String(filteredY) + "," + "Z:" + String(filteredZ);
Serial.println(pressValue);
if (pressValue < 10) {
// Stop the buzzer when the pressure sensor is being "petted"
delay(100);
noTone(buzz);
if (state == false){
Serial.println("not angry");
state = true;
}
}
else {
if (filteredZ > 0 || delta(30, smooth(filteredX, event.acceleration.x),filteredX ) || delta(30, smooth(filteredY, event.acceleration.y),filteredY) ) { // if upside down
tone(buzz, HIGH);
if (state == true){
Serial.println("angry");
state = false;
}
}
pressesing();
// add another if statement for when the accelerometer is being shaked quickly
}
}
float smooth(float previous, float new_value){
const float alpha = 0.7; // Filter coefficient
float filteredValue = (1 - alpha) * previous + (alpha * new_value);
return filteredValue;
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("A"); // send a capital A
delay(300);
}}
bool delta(int cutoff, float variabel, float previus_var){
float dif = variabel - previus_var;
if (dif>cutoff){
return true;
}else{
return false;
}
}