-
Notifications
You must be signed in to change notification settings - Fork 0
/
mic.h
85 lines (62 loc) · 1.11 KB
/
mic.h
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
#ifndef MIC_H
#define MIC_H
//global variables
#define PIN 0
#define TH_SAMPLES 100
#define VOL_SAMPLES 1
long tt=0;
class Mic{
public:
int status;
int pin;
long vol;
long mic_th;
public:
Mic();
void SetPin();
long ReadVol();
long sVol();
void dyn_th();
};
Mic::Mic(){
status = LOW;
SetPin();
}
void Mic::SetPin(){
pin = PIN;
pinMode(pin, INPUT);
}
long Mic::ReadVol(){
return pow((analogRead (pin)-360),2);
}
long Mic::sVol(){
vol=0;
for(int i=0; i<VOL_SAMPLES; i++){
vol += ReadVol();
//delay(5);
tt=millis();
while(millis()-tt<10){};
tt=millis();
}
vol = vol/VOL_SAMPLES;
//vol = abs(vol);
vol = pow(vol, 4);
//vol = (int)vol;
//delay(500);
return vol;
}
void Mic::dyn_th(){
for(int i=0; i<TH_SAMPLES; i++){
mic_th += ReadVol();
//delay(5);
tt=millis();
while(millis()-tt<10){};
tt=millis();
}
mic_th = mic_th/TH_SAMPLES;
//mic_th = pow(mic_th, 2);
mic_th = (long)(((float)mic_th * 1.0f)); //aumento th del 20%
mic_th = abs(mic_th);
//mic_th = pow(mic_th, 2);
}
#endif