Hello, I have been working on a project tried several code changes but it is still not doing what I would like it to do,, I have 4 Ultrasonic sensors facing in different directions.. I am using one servo motor.
I am trying to get the servo motor to turn in the direction of the Sensor that goes high and stay in that position until another sensor goes high.. I do not want the servo to go back to 0 or a different position. Can I drop a code below each (Const int tripin) to tell the servo what to do when that pin goes high.? 90,180,270,360 degrees, one degree number for each trig pin?
#include <Servo.h>
Servo myservo; //creates servo object
const int echopin3 = 4; //sets echopin3 @pin 4
const int trigpin3 = 5; //sets trigpin3 @pin 5
const int echopin4 = 6; //sets echopin4 @pin 6
const int trigpin4 = 7; //sets trigpin4 @pin 7
const int echopin1 = 10; //sets echopin1 @pin 10
const int trigpin1 = 11; //sets trigpin1 @pin 11
const int echopin2 = 12; //sets echopin2 @pin 12
const int trigpin2 = 13; //sets trigpin2 @pin 13
const int servo = 9; //sets servo @pin 9
long Rightduration, Leftduration, Rightinch, Leftinch; //establishes the variables of the duration and sets distance in inches
int threshold = 5; //Sets the sensor threshold at 10 inches
int angle = 180; //Sets the Initial angle
void setup()
{
myservo.attach(9); //attaches the servo on pin 9
}
void loop()
{
pinMode(trigpin3, OUTPUT); //trigpin1 is set as output
digitalWrite(trigpin3, LOW); //sets the trigpin1 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin3, HIGH); //sets the trigpin1 to give high pulse
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin3, LOW);
Rightduration = pulseIn(echopin3, HIGH); //reads high pulse
pinMode(trigpin4, OUTPUT); //trigpin4 is set as output
digitalWrite(trigpin4, LOW); //sets the trigpin4 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin4, HIGH); //sets the trigpin4 to give high pulse
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin4, LOW);
Leftduration = pulseIn(echopin4, HIGH); //reads high pulse
pinMode(trigpin1, OUTPUT); //trigpin1 is set as output
digitalWrite(trigpin1, LOW); //sets the trigpin1 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin1, HIGH); //sets the trigpin1 to give high pulse
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin1, LOW);
Rightduration = pulseIn(echopin1, HIGH); //reads high pulse
pinMode(trigpin2, OUTPUT); //trigpin2 is set as output
digitalWrite(trigpin2, LOW); //sets the trigpin2 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin2, HIGH); //sets the trigpin2 to give high pulse
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin2, LOW);
Leftduration = pulseIn(echopin2, HIGH); //reads high pulse
//this will convert the elapsed time into the distance
Rightinch = microsecondsToInches(Rightduration);
Leftinch = microsecondsToInches(Leftduration);
follow(); //follows the movement
}
long microsecondsToInches(long microseconds)
{
// The speed of sound is 340 m/s or 73.746 microseconds per inch.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance traveled.
return microseconds / 74 / 2;
}
void follow() //conditions for the follow command
{
if (Leftinch <= threshold || Rightinch <= threshold)
{
if (Leftinch + 2 < Rightinch)
{
angle = angle - 2;
}
if (Rightinch + 2 < Leftinch)
{
angle = angle + 2;
}
}
if (angle > 160)
{
angle = 160;
}
if (angle < 0)
{
angle = 0;
}
myservo.write(angle);
}
Read the guide to posting on the forum at the top of every page
90,180,270,360 degrees
What type of servo do you have ? Most do not rotate through 360 degrees
Not a "continuous rotation servo" by any chance ?
Exactly what servo is it? What are the sensors? What does it do now. And most important...where's the rest of the code?
Steve
Hi,
Welcome to the forum.
Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Thanks.. Tom...
The servo motor is MG996R .. This is the following code :
#include <Servo.h>
Servo myservo; //creates servo object
const int echopin3 = 4; //sets echopin3 @pin 4
const int trigpin3 = 5; //sets trigpin3 @pin 5
const int echopin4 = 6; //sets echopin4 @pin 6
const int trigpin4 = 7; //sets trigpin4 @pin 7
const int echopin1 = 10; //sets echopin1 @pin 10
const int trigpin1 = 11; //sets trigpin1 @pin 11
const int echopin2 = 12; //sets echopin2 @pin 12
const int trigpin2 = 13; //sets trigpin2 @pin 13
const int servo = 9; //sets servo @pin 9
long Rightduration, Leftduration, Rightinch, Leftinch; //establishes the variables of the duration and sets distance in inches
int threshold = 5; //Sets the sensor threshold at 10 inches
int angle = 180; //Sets the Initial angle
void setup()
{
myservo.attach(9); //attaches the servo on pin 9
}
void loop()
{
pinMode(trigpin3, OUTPUT); //trigpin1 is set as output
digitalWrite(trigpin3, LOW); //sets the trigpin1 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin3, HIGH); //sets the trigpin1 to give high pulse
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin3, LOW);
Rightduration = pulseIn(echopin3, HIGH); //reads high pulse
pinMode(trigpin4, OUTPUT); //trigpin4 is set as output
digitalWrite(trigpin4, LOW); //sets the trigpin4 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin4, HIGH); //sets the trigpin4 to give high pulse
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin4, LOW);
Leftduration = pulseIn(echopin4, HIGH); //reads high pulse
pinMode(trigpin1, OUTPUT); //trigpin1 is set as output
digitalWrite(trigpin1, LOW); //sets the trigpin1 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin1, HIGH); //sets the trigpin1 to give high pulse
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin1, LOW);
Rightduration = pulseIn(echopin1, HIGH); //reads high pulse
pinMode(trigpin2, OUTPUT); //trigpin2 is set as output
digitalWrite(trigpin2, LOW); //sets the trigpin2 to give low pulse
delayMicroseconds(3); //duration is 3 microseconds
digitalWrite(trigpin2, HIGH); //sets the trigpin2 to give high pulse
delayMicroseconds(5); //duration is 5 microseconds
digitalWrite(trigpin2, LOW);
Leftduration = pulseIn(echopin2, HIGH); //reads high pulse
//this will convert the elapsed time into the distance
Rightinch = microsecondsToInches(Rightduration);
Leftinch = microsecondsToInches(Leftduration);
follow(); //follows the movement
}
long microsecondsToInches(long microseconds)
{
// The speed of sound is 340 m/s or 73.746 microseconds per inch.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance traveled.
return microseconds / 74 / 2;
}
void follow() //conditions for the follow command
{
if (Leftinch <= threshold || Rightinch <= threshold)
{
if (Leftinch + 2 < Rightinch)
{
angle = angle - 2;
}
if (Rightinch + 2 < Leftinch)
{
angle = angle + 2;
}
}
if (angle > 160)
{
angle = 160;
}
if (angle < 0)
{
angle = 0;
}
myservo.write(angle);
}
pmagowan:
Read the guide to posting on the forum at the top of every page
I dont see anything at the top of the page as far as a guide is concerned
The servo motor is MG996R
OK, but is it capable of moving to 90, 180, 270 and 360 degrees ?
You servo seems to be moving between write(0) and write(160) in steps of 2. Where do the 90, 180, 270, 360 come in?
And why do you read sensor4 as Leftduration and then read sensor2 and replace Leftduration? Same with sensor3 and 1 and Rightdirection?
Your code seems to have little connection with your description.
Steve
Hi,
You are pinMode in the loop(), it should be done in the setup().
Can you talk us through the logic of your code.
You use Rightduration and Leftduration for two sensors each, why doesn't each sensor have its own duration variable?
Did you write your code in stages?
Have you got code that just get signals from the sensors and serial.print to the IDE monitor?
Forget about any servo code for the moment.
Tom...
Yes I will write the code in stages at times but I have been trying so many different code which is not working. Feeling lost at this point. Not sure what else to do to get the Pan working correctly. Making me crazy
Hi,
Have you got the servo moving back and forth.
What do you want the servo to do?
- move back a forth 180Deg?
- rotate 360Deg?
Look at the example codes for Servo in the IDE Examples/
Thanks.. Tom..
ParaChaser:
Yes I will write the code in stages at times but I have been trying so many different code which is not working. Feeling lost at this point. Not sure what else to do to get the Pan working correctly. Making me crazy
Post the best code you have tried. It must at least compile, load and do something.
Then tell us what it does and we can help you to get it doing what you want. But if you don't answer questions and just ignore all the advice you are given then we can't help.
Steve