Initializing mic add-on with Elegoo smart bot V3

We are trying to add a mic to our robot so that it may be activated by sound. Here is the code, anyone have and ideas?

#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

Ideas about what? All it says is you want to add a mic. Did you try to add a mic? If so, what happened? The code you posted does not compile.

Please format your code and post it using code tags.

?
Why?

// void forward(){

Code fragment?

  pinMode(
    myservo.attach(3, 700, 2400); // attach servo on pin 3 to servo object

Huh?

    boolean sound == true;

    while sound = true {

pulseIn() returns an unsigned long. Why do you assign it to a float type?

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;
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.