Sound tracking rover
Drives to the exact location of the sound source.
Components and supplies
1
Jumper Wire Female to Male
1
Arduino Uno Rev3
1
DC Gear Motor
2
MAX4466 microphone
1
9V Battery Supply
1
Ultrasonic Sensor - HC-SR04
1
Chassis Car 4wd
1
10 jumper wires 150mm male
1
L298N Module
Tools and machines
1
Soldering kit
Apps and platforms
1
Arduino IDE 2.0 (beta)
Project description
Code
Sound tracking rover
cpp
comments in this code,when i was testing approach
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); // Sets the trigPin as an Output 28 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 29 30} 31void loop(){ 32 digitalWrite(trigPin, LOW); 33 delayMicroseconds(2); 34 // Sets the trigPin on HIGH state for 10 micro seconds 35 digitalWrite(trigPin, HIGH); 36 delayMicroseconds(10); 37 digitalWrite(trigPin, LOW); 38 // Reads the echoPin, returns the sound wave travel time in microseconds 39 duration = pulseIn(echoPin, HIGH); 40 // Calculating the distance 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);//determine the peak 47 int reading2 = analogRead(mic2); 48 amp2= abs(reading2-baseline);//determine the peak 49 difference= amp1-amp2;//peak intensity difference 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){//steers right based on the sound 62 analogWrite(enA,90); 63 analogWrite(enB,90); 64 roboright(); 65 66 67 } 68 else if(difference<-9 && amp1+amp2>250){//steers left based on the sound 69 analogWrite(enA,90); 70 analogWrite(enB,90); 71 roboleft(); 72 73 74 }// jatin sachdev 75 else if(difference>=-5 && difference<=5 && amp1+amp2>250 ){// drives towards the source of the sound 76 // while(start-end<0.5){ 77 analogWrite(enA,180); 78 analogWrite(enB,180); 79 roboforward(); //} 80 /*stop(); 81 drivetosource();*/ 82 83 84 } 85 if(distance<4){// makes the car recognise when it has reached the sound of source 86 stop(); 87 } 88 89 90 91} 92/*void drivetosource(){ 93 analogWrite(enA, 100); 94 analogWrite(enB, 100); 95 while (amp1<335 || amp2<335){ 96 if(amp1<337 || amp2<337 && difference<5 && difference>-5 && amp1+amp2>300){ 97 roboforward();} 98 else if(difference>5){ 99 roboright(); 100 } 101 else if(difference<-5 ){ 102 roboleft(); 103 104 } 105 106 else if(amp1>335 && amp2>335){ 107 stop(); 108 }} 109}*/ 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 //do nothing 144}
Comments
Only logged in users can leave comments