Hi Everyone!
I am about to recreate this project - https://create.arduino.cc/projecthub/lbf20012001/sound-location-finder-92e6b0?ref=search&ref_id=sound%20sensor&offset=1
But I want to use my servo instead.
The Code Given by Him is -
const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution for your motor
const int numberOfSteps = stepsPerRevolution/8; //45 degree turns
const int dirPin=12;
const int stepPin=13;
const int rightSensorPin=7;
const int leftSensorPin=8;
const int enablePin=5 ;
boolean rightVal = 0;
boolean leftVal = 0;
void setup()
{
pinMode(leftSensorPin, INPUT); //Make pin 8 an input pin.
pinMode(rightSensorPin, INPUT); //Make pin 7 an input pin.
pinMode (stepPin, OUTPUT); //Make pin 13 an output pin.
pinMode (dirPin, OUTPUT); //Make pin 12 an output pin.
pinMode (enablePin, OUTPUT); //Make pin 5 an output pin.
digitalWrite(enablePin, LOW); //Enable is active low
Serial.begin (9600); // initialize the serial port:
}
void loop ()
{
//poll inputs for signal
rightVal =digitalRead(rightSensorPin);
leftVal =digitalRead(leftSensorPin);
// when the sensor detects a signal above the threshold value set on sensor, turn finder to the direction of sound
if (leftVal==LOW && rightVal==HIGH)
{
Serial.println("Turning Right");
digitalWrite(dirPin,LOW); //turn counter-clockwise
//turn finder in the direction of the sound
for(int steps = 0; steps < numberOfSteps; steps++)
{
//create pulse to turn motor one step at a time
digitalWrite(stepPin,HIGH);
delayMicroseconds(10000);
digitalWrite(stepPin, LOW);
delayMicroseconds(10000);
}
delayMicroseconds(100000);
rightVal = 0;
leftVal = 0;
}
else if (leftVal==HIGH && rightVal==LOW)
{
Serial.println("Turning Left");
digitalWrite(dirPin,HIGH); //turn clockwise
//turn finder in the direction of the sound
for(int steps = 0; steps < numberOfSteps; steps++)
{
//create pulse to turn motor one step at a time
digitalWrite(stepPin,HIGH);
delayMicroseconds(10000);
digitalWrite(stepPin, LOW);
delayMicroseconds(10000);
}
delayMicroseconds(100000);
rightVal = 0;
leftVal = 0;
}
else
{
//Do nothing
rightVal = 0;
leftVal = 0;
}
}
SO, to change stepper motor to servo, we must use
servo.write(θ) // where theta is the angle required
Now I have changed to code to
#include <Servo.h>
const int rightSensorPin=7;
const int leftSensorPin=8;
boolean rightVal = 0;
boolean leftVal = 0;
int pos = 90;
Servo myservo;
void setup()
{
myservo.attach(9);
pinMode(leftSensorPin, INPUT); //Make pin 8 an input pin.
pinMode(rightSensorPin, INPUT); //Make pin 7 an input pin.
Serial.begin (9600); // initialize the serial port:
}
void loop ()
{
//poll inputs for signal
rightVal =digitalRead(rightSensorPin);
leftVal =digitalRead(leftSensorPin);
// when the sensor detects a signal above the threshold value set on sensor, turn finder to the direction of sound
if (leftVal==LOW && rightVal==HIGH)
{
Serial.println("Turning Right");
for (pos = 90; pos <= 180; pos += 1) { // goes from 90 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1000);
}
}
else if (leftVal==HIGH && rightVal==LOW)
{
Serial.println("Turning Left");
for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 90 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1000);
}
}
else
{
//Do nothing
rightVal = 0;
leftVal = 0;
}
}
There is no error while compiling, but I don't wanna damage my arduino and sensors.. so could anyone tell me if there's a mistake cuz I am new to this....
Also, here's my schematic diagram -
And also, I want the servo to turn from 90 to 180 degree if the sound is received from left sensor and 90 to 0 degree if the sound is received from left sensor. And then return back to the initial position, which is 90 degree after a delay of 1000 ms.
Looking forward for your help!