Code to control 1 servo with 2 sound sensors

Hi,
I want to control my servo with 2 sound sensors (1 for left and 1 for right).
Components Used :

  1. Arduino Uno
  2. Sound Sensor LM393 x 2
  3. 9g Micro Servo

I have connected the OUT pin of the sensors to A2 and A1. I have connected the servo to Pin 8 of the Arduino Uno.

Here's My Code:


#include <Servo.h>

int sound2 = A2;
int sound1 = A1;
  
Servo myservo;

int pos = 0;

void setup()
{
  pinMode(sound1, INPUT);
  pinMode(sound2, INPUT);
  myservo.attach(8);
  Serial.begin(9600);
}


void loop()
{
  myservo.write(90);
  
  int data = digitalRead(A2);
  if (data == 1) {
    myservo.write(0);
    delay(5000);
   }
  {
  int data = digitalRead(A1);
  if (data == 1) {
    myservo.write(180);
    delay(5000);
  }
 }
}

When I complete the connection and upload the code, the servo doesn't move to 90 degrees. Also, it moved from 0 to 180 degrees when there is a sound in any of the sensor. I want the servo to turn 90 degrees left when it hears a sound from sensor 1 and turn 90 degrees right when it hears a sound from sensor 2. Please provide me a proper code and proper instruction on my mistake.

Looking forward for your help.
Thank you in advance...

A random user

Welcome to the forum

You have 2 different variables named data in your sketch, each with its own scope. Fix that first

Did you by any chance add the braces round the second section of code to make an error go away ?

When loop() starts, the servo is commanded to move to 90 degrees but that will take some time and you don't give it any time to complete the move if one of the sensors is activated. A better approach (but a bit more complicated) would be a state machine so you can track which state or position you are in and then adjust as needed based on either the sensor inputs or the amount of elapsed time in a given state.

sir, thank you for helping out, but can you guide me on how to do the connection as well as provide me the code if possible?
Thank You!

sir, thank you for helping out, but can you guide me on how to do the connection as well as provide me the code if possible?
Thank You!.

How are the sounds and sensors isolated from one another to prevent a sound from being "heard" by both sensors?

I'd change the variable "data" (used twice) to two separate variables, say "leftAudio" and "rightAudio", then put Serial prints after detection of either so you can see what's going on in the serial monitor. I mean, you opened the serial connection in setup() but then never used it, what's up with that?

Also, you created global variables called "sound1" and "sound2" but then did didn't use them.

sir, if u don't mind, can you provide me a proper code for my project??

There is a section of the forum where you pay people to write your code. In the rest of the forum it is a learning environment and, in general, you will get pointed in the right direction but will have to write the code yourself. Take on board the points made, make an attempt and post it.

oh k.. sorry!

Hi,
I am working on a project where the servo turns according to the sound it receives from the sound sensors.

Components - Arduino Uno, Servo 9g, Sound Sensor LM393

Code -

#include <Servo.h>

Servo servo;

//Sound variables
const int sampleWindow = 250; // Sample window width in mS (250 mS = 4Hz)
unsigned int sound;


void setup()
{
  servo.attach(5);
  
  Serial.begin(9600);
}


void loop()
{
  int position = 0;

  unsigned long start = millis(); // Start of sample window
  unsigned int peakToPeak = 0;    // peak-to-peak level
  unsigned int signalMax = 0;
  unsigned int signalMin = 1024;

  // collect data for 250 miliseconds
  while (millis() - start < sampleWindow)
  {
    sound = analogRead(A0) && (A1);
    if (sound < 1024)  //This is the max of the 10-bit ADC so this loop will include all readings
    {
      if (sound > signalMax)
      {
        signalMax = sound;  // save just the max levels
      }
      else if (sound < signalMin)
      {
        signalMin = sound;  // save just the min levels
      }
    }
  }
  peakToPeak = signalMax - signalMin;
  double volts = (peakToPeak * 3.3) / 1024;
  Serial.print("Volts:");
  Serial.print("\t");
  Serial.println(volts);
  if (analogRead(A0) == volts >= 1.0)
  {

    for (position = 0; position <= 75; position += 5)
    {
      servo.write(position);
      delay(20);
    }
    for (position = 75; position >= 0; position -= 5)
    {
      servo.write(position);
      delay(20);
    }
    for (position = 0; position <= 75; position += 5)
    {
      servo.write(position);
      delay(20);
    }
    for (position = 75; position >= 0; position -= 5)
    {
      servo.write(position);

      delay(20);
    }
    delay(800);
  }




  if (analogRead(A1) == volts >= 1.0)
  {

    for (position = 75; position <= 0; position += 5)
    {
      servo.write(position);
      delay(20);
    }
    for (position = 0; position >= 75; position -= 5)
    {
      servo.write(position);
      delay(20);
    }
    for (position = 75; position <= 0; position += 5)
    {
      servo.write(position);
      delay(20);
    }
    for (position = 0; position >= 75; position -= 5)
    {
      servo.write(position);

      delay(20);
    }
    delay(800);
  }

}

The code shows no error, but in the serial monitor, there is no detection of sound (The volt is 0.00). Also the servo doesn't turn when there is a sound.

THANK YOU FOR YOUR HELP (IN ADVANCE)

Read the documentation. You can't read two analog ports simultaneously.

so should it be in next line?

What are you trying to do, add them?

    sound = analogRead(A0) + analogRead(A1);

should I add it?

Realize now, your combined max is now 2046 not 1023.

add what?

im just a beginner.. can you just tell me what to do? pls

You haven't told me what you are trying to do.

I want my servo to turn 90 degree right when it receives data(sound) from A1 and 90 degree left when it receives data from A0. So i have kept two analog read command. I am confused. can u just edit my code so it would be correct.

can u just edit my code so it would be correct.

No, and I don't like needy people who want their work done for them. Goodbye.