Pages: [1]   Go Down
Author Topic: Servo and Ultrasonic sensor  (Read 3581 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: October 05, 2012, 03:10:18 am by AWOL » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Jr. Member
**
Karma: 2
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
  .....
« Last Edit: October 04, 2012, 09:10:26 pm by Denbo » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Uncompiled, untested
Code:
#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);
}
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}

Logged

Pages: [1]   Go Up
Jump to: