Arduino Uno with a Mic Sensor

We are trying to run a robot autonomously using a microphone sensor (https://learn.sparkfun.com/tutorials/sound-detector-hookup-guide/configuration)

We have the 3 pins in the A3 slot in the expansion board. However, it isn't recognizing the microphone in the code. It's the Elegoo 3.0 robot kit if that helps. The code is below. Everything else works minus the autonomous part with the microphone.

https://preview.redd.it/jxi97jnlvzr61.jpg?width=3024&format=pjpg&auto=webp&s=ffd55cf4dff8037bb3195eae2e836f7a16674e83

#include <Servo.h> //servo library Servo myservo; // create servo object to control servo int Echo = A4; //ultrasonic sensor on pin A4 int Trig = A5; //trigger sonsor on pin A5 int soundsensor = A3; // input from sound sensor #define ENA 5 //All of motors are initialized to the motor controller all at once #define ENB 6 #define IN1 7 #define IN2 8 #define IN3 9 #define IN4 11 #define carSpeed 150 //Car speed for backwards, forwards, right and left int rightDistance = 0, leftDistance = 0, middleDistance = 0; // void forward(){ analogWrite(ENA, carSpeed); analogWrite(ENB, carSpeed); digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); Serial.println("Back"); } void back() { analogWrite(ENA, carSpeed); analogWrite(ENB, carSpeed); digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); Serial.println("Back"); } void left() { analogWrite(ENA, carSpeed); analogWrite(ENB, carSpeed); digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); Serial.println("Left"); } void right() { analogWrite(ENA, carSpeed); analogWrite(ENB, carSpeed); digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); Serial.println("Right"); } void stop() { digitalWrite(ENA, LOW); digitalWrite(ENB, LOW); Serial.println("Stop!"); } //Ultrasonic distance measurement Sub function int Distance_test() { digitalWrite(Trig, LOW); delayMicroseconds(2); digitalWrite(Trig, HIGH); delayMicroseconds(20); digitalWrite(Trig, LOW); float Fdistance = pulseIn(Echo, HIGH); Fdistance= Fdistance / 58; return (int)Fdistance; } void setup() { pinMode(A3, OUTPUT); pinMode( myservo.attach(3,700,2400); // attach servo on pin 3 to servo object Serial.begin(9600); pinMode(Echo, INPUT); pinMode(Trig, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT); stop(); } void loop() { {if (digitalRead(soundsensor) == LOW) //if sound is heard delay(100); // dealy 1/10th of a second boolean sound == true; while sound = true { myservo.write(90); //setservo position according to scaled value delay(500); middleDistance = Distance_test(); if(middleDistance <= 40) { //adjust this number if you adjust the speed stop(); delay(500); //delay for half a second myservo.write(0); //moves the servo to the 10 degree position delay(1000); //delays 1 second rightDistance = Distance_test(); //measures distance to the right delay(500); //delays half a second myservo.write(180); //moves 180 degrees delay(1000); //delays 1 second leftDistance = Distance_test(); //measures distance to the left delay(500); //delays half a second myservo.write(90); //sets servo back forward delay(1000); //delays 1 second if(rightDistance > leftDistance) { //if the right distance is greater then the left distance then its going to move the bot to the right and continue moving in that direction right(); //motors move to the right delay(110); //delays for 360 milliseconds } else if(rightDistance < leftDistance) { left(); delay(110); } else if((rightDistance <= 40) || (leftDistance <= 40)) { //if both the right and left distances are less then 20 then the bot will back up because it is probably in a corner back(); //bot moves backwards delay(110); //delays 180 milliseconds } else { back(); //if none of this evaluates then the bot will just continue to move forward } } else { back(); } } }//loop will continue to evaluate the distances and see which way to go 

Touch here for the full post on the Arduino Apprentices tumblr

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s