1int mic1 = A2;
2int mic2 = A1;
3int motor1= 2;
4int motor2= 3;
5int motor3= 4;
6int motor4= 5;
7int baseline = 337;
8int amp1=0;
9int amp2=0;
10int difference=0;
11int enA = 9;
12int enB = 10;
13int start;
14int end;
15const int echoPin= 7;
16const int trigPin= 6;
17long duration;
18int distance;
19void setup(){
20 Serial.begin(9600);
21 pinMode(motor1, OUTPUT);
22 pinMode(motor2, OUTPUT);
23 pinMode(motor3, OUTPUT);
24 pinMode(motor4, OUTPUT);
25 pinMode(enA, OUTPUT);
26 pinMode(enB, OUTPUT);
27 pinMode(trigPin, OUTPUT);
28 pinMode(echoPin, INPUT);
29
30}
31void loop(){
32 digitalWrite(trigPin, LOW);
33 delayMicroseconds(2);
34
35 digitalWrite(trigPin, HIGH);
36 delayMicroseconds(10);
37 digitalWrite(trigPin, LOW);
38
39 duration = pulseIn(echoPin, HIGH);
40
41 distance = duration * 0.034 / 2;
42 start=0;
43 start= millis();
44 end = start;
45 int reading1 = analogRead(mic1);
46 amp1= abs(reading1-baseline);
47 int reading2 = analogRead(mic2);
48 amp2= abs(reading2-baseline);
49 difference= amp1-amp2;
50
51 Serial.print("mic1: ");
52 Serial.print(amp1);
53 Serial.print(" mic2: ");
54 Serial.print(amp2);
55 Serial.print(" difference: ");
56 Serial.print(difference);
57 Serial.print(" distance:");
58 Serial.print(distance);
59
60 Serial.print("\n");
61 if(difference>9 && amp1+amp2>250){
62 analogWrite(enA,90);
63 analogWrite(enB,90);
64 roboright();
65
66
67 }
68 else if(difference<-9 && amp1+amp2>250){
69 analogWrite(enA,90);
70 analogWrite(enB,90);
71 roboleft();
72
73
74 }
75 else if(difference>=-5 && difference<=5 && amp1+amp2>250 ){
76
77 analogWrite(enA,180);
78 analogWrite(enB,180);
79 roboforward();
80
82
83
84 }
85 if(distance<4){
86 stop();
87 }
88
89
90
91}
92
110void roboright(){
111 digitalWrite(motor1,LOW);
112 digitalWrite(motor2,HIGH);
113 digitalWrite(motor3,LOW);
114 digitalWrite(motor4,HIGH );
115
116}
117void roboleft(){
118 digitalWrite(motor1,HIGH);
119 digitalWrite(motor2,LOW);
120 digitalWrite(motor3,HIGH);
121 digitalWrite(motor4,LOW);
122}
123void stop(){
124 digitalWrite(motor1,LOW);
125 digitalWrite(motor2,LOW);
126 digitalWrite(motor3,LOW);
127 digitalWrite(motor4,LOW);
128}
129void roboforward(){
130 digitalWrite(motor1,LOW);
131 digitalWrite(motor2,HIGH);
132 digitalWrite(motor3,HIGH);
133 digitalWrite(motor4,LOW);
134
135}
136void robobackward(){
137 digitalWrite(motor1,HIGH);
138 digitalWrite(motor2,LOW);
139 digitalWrite(motor3,LOW);
140 digitalWrite(motor4,HIGH);
141}
142void locatesource(){
143
144}