Hi,
I want to control my servo with 2 sound sensors (1 for left and 1 for right).
Components Used :
Arduino Uno
Sound Sensor LM393 x 2
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...
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.
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.
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.
#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.
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.