-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmotion_cat_gesture.ino.ino
94 lines (77 loc) · 2.68 KB
/
Emotion_cat_gesture.ino.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
// Pin definitions
const int TRIG_PIN = 4; // Trigger pin for sonar
const int ECHO_PIN = 5; // Echo pin for sonar
const int SAMPLE_SIZE = 256; // Number of samples to record
const int CATEGORIES = 3; // Number of categories: stationary, moving, fast
// Arrays to store training data
float storedVariations[CATEGORIES] = {0.0, 0.0, 0.0}; // Average variations for categories
const char* categoryLabels[CATEGORIES] = {"stationary", "moving", "fast"};
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Manually define average variations for training (or collect real training data)
storedVariations[0] = 0.5; // Stationary
storedVariations[1] = 5.0; // Moving
storedVariations[2] = 15.0; // Fast
}
void loop() {
// Record a snippet of distance data
float distances[SAMPLE_SIZE];
for (int i = 0; i < SAMPLE_SIZE; i++) {
distances[i] = measureDistance();
delay(50); // Adjust sampling rate as needed
}
// Calculate variation in the recorded data
float variation = calculateVariation(distances, SAMPLE_SIZE);
// Categorize the snippet based on the variation
const char* category = categorizeSnippet(variation);
// Output results
Serial.print("Variation: ");
Serial.println(variation);
Serial.print("Category: ");
Serial.println(category);
delay(2000); // Wait before the next recording
}
// Function to measure distance using sonar
float measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2; // Convert to centimeters
}
// Function to calculate variation in distance
float calculateVariation(float* distances, int size) {
float sum = 0.0;
float sumSquares = 0.0;
// Compute mean
for (int i = 0; i < size; i++) {
sum += distances[i];
}
float mean = sum / size;
// Compute variance
for (int i = 0; i < size; i++) {
sumSquares += pow(distances[i] - mean, 2);
}
float variance = sumSquares / size;
// Variation is the square root of variance (standard deviation)
return sqrt(variance);
}
// Function to categorize snippet based on variation
const char* categorizeSnippet(float variation) {
float minDifference = 999999; // Large initial value
int closestCategory = -1;
// Compare variation to stored category averages
for (int i = 0; i < CATEGORIES; i++) {
float difference = abs(variation - storedVariations[i]);
if (difference < minDifference) {
minDifference = difference;
closestCategory = i;
}
}
// Return the label of the closest category
return categoryLabels[closestCategory];
}