-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#1
89 lines (76 loc) · 2 KB
/
#1
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
#include <Servo.h>
//SERVO LEFT ROLL
Servo leftRoll;
//SERVO RIGHT ROLL
Servo rightRoll;
//SERVO BASE LEFT
Servo baseLeft;
//SERVO BASE RIGHT
Servo leftPen;
//SERVO LEFT
Servo baseRight;
//SERVO RIGHT
Servo rightPen;
int pos = 0; // variable to store the servo position
//MICROPHONE VARIABLES
const int sampleWindow = 250; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int mappedValue = 0;
void setup() {
Serial.begin(9600);
baseLeft.attach(9); // attaches the servo on pin 9 to the servo object
baseRight.attach(5);
leftPen.attach(10);
leftRoll.attach(6);
rightRoll.attach(3);
leftRoll.write(90);
rightRoll.write(90);
}
void loop() {
//ROLL
// rightRoll.write(180);
// leftRoll.write(180);
// delay(50);
// rightRoll.write(90);
// leftRoll.write(90);
// delay(1000);
//MICRO
microphoneRead();
//SERVOS
for (int i = 150; i < 150 + mappedValue; i += 3) {
leftPen.write(i);
delay(50); // waits 15ms for the servo to reach the position
}
for (int i = 150 + mappedValue; i > 150; i -= 3) {
leftPen.write(i);
delay(50); // waits 15ms for the servo to reach the position
}
}
void microphoneRead() {
//MICROPHONE
unsigned long startMillis = millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
//mappedValue = map(peakToPeak, 0, 1023, 0, 255);
mappedValue = map(peakToPeak, 0, 1023, 0, 30);
//Serial.println(mappedValue);
delay(3);
}