-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoiseMode.ino
52 lines (41 loc) · 1.06 KB
/
NoiseMode.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
float minNoiseAmplitude = 0.1;
float maxNoiseAmplitude = 1.0;
float noiseAmplitude = 0.0;
int minNoiseSpan = 9; //milliseconds;
int maxNoiseSpan = 13; // milliseconds;
unsigned long noiseSpan = 0;
unsigned long noiseMaintenanceTimestamp = 0;
void startNoise()
{
setNoiseLevels();
}
void maintainNoise()
{
unsigned long now = millis();
if ((now - noiseMaintenanceTimestamp) >= noiseSpan)
{
setNoiseLevels();
}
}
void setNoiseLevels()
{
// Set the maintenance timestamp
noiseMaintenanceTimestamp = millis();
// Set the noiseSpan
noiseSpan = random(minNoiseSpan, maxNoiseSpan);
// Get an amplitude
noiseAmplitude = randomFloat(minNoiseAmplitude, maxNoiseAmplitude);
// Set the output peak level, from 0 (off) to 1.0. The default is off. Noise is generated only after setting to a non-zero level.
pinkNoise.amplitude(noiseAmplitude);
}
void stopNoise()
{
pinkNoise.amplitude(0);
}
float randomFloat(float min, float max)
{
int randInt = random(0, 256);
float range = max - min;
float result = ((randInt/256) * range) + min;
return result;
}