Go Down

Topic: Servo and Ultrasonic sensor (Read 4728 times) previous topic - next topic

Oct 04, 2012, 11:43 pm Last Edit: Oct 05, 2012, 10:10 am by AWOL Reason: 1
hi, ? will make robot using servo and ultrasonic sensor. then I had write a simple code using servo with sensor.
I want from program if distance under 5 cm servo turning 90 degree. please help me.thank you
where I do wrong?
Code: [Select]

#define trigPin 12
#define echoPin 13
#include <Servo.h>

Servo myservo;

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 myservo.attach(9);
 
}

void loop() {
 int duration, distance,pos=0;
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(1000);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 if (distance<=5){
  myservo.write(90);
   delay(100);
 }
 else {
 
   Serial.print(distance);
   Serial.println(" cm");
 }
 delay(500);
}


Moderator edit: Code tags.

johnwasser

I think the default servo position is 90-degrees so moving it to 90-degrees won't do anything.

Is there a different position you'd like the servo to go to when the distance is > 5?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Denbo

#2
Oct 05, 2012, 04:01 am Last Edit: Oct 05, 2012, 04:10 am by Denbo Reason: 1
If you are using the HC-SR04 the 1000 microsecond delay is a bit long.  

The specification doc says it should 10 microseconds (delayMicroseconds(10)).
Here is the doc:
http://jaktek.com/wp-content/uploads/2011/12/HC-SR04.pdf

Also you should specifically hold the trigger line LOW first for a little bit of time before setting it HIGH (just like the PING sensor).

Try the following code:
Code: [Select]
void loop() {
  int duration, distance,pos=0;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  .....

AWOL

Uncompiled, untested
Code: [Select]
#define trigPin 12
#define echoPin 13
#define servoPin 9
#define DEFAULT_SERVO_POS    0
#define TRIGGERED_SERVO_POS 90
#include <Servo.h>

Servo myservo;

void setup()
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  myservo.write(DEFAULT_SERVO_POS);
  myservo.attach(servoPin);
}

void loop()
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  long distance = (duration/2) / 29.1;
  Serial.print(distance);
  Serial.println(" cm");
  if (distance<=5){
    myservo.write(TRIGGERED_SERVO_POS);
  }
  delay(500);
}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

thank you for helping me.
? will use  this code
Quote

#define trigPin 12
#define echoPin 13
#include <Servo.h>

Servo myservo;
 
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  myservo.attach(9);
   
}

void loop() {
 int duration, distance,pos=0,i;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
   Serial.print(distance);
   Serial.println(" cm");
  if(distance<5)
  {
    myservo.write(180);
  }
  else{
    myservo.write(0);
  }
    
  delay(100);
}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy