I am working on a project using an HC-SR04 Ping Sensor to control a servo motor.
Basicly when an object gets within about 30 cm of the sensor the servo will move about 60 degrees then quickly return back to 0.
I have been searching for a sample sketch for this project for a few days. So far I have found a few examples that involve both the sensor and servo but haven't found the code. I am thinking it would require an If Statement but not sure.
Any help getting me in the right direction would be great appreciated!!
HC-SR04 Sensor Controlling Servo
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88088
- Joined: Sat Feb 07, 2009 10:11 am
Re: HC-SR04 Sensor Controlling Servo
We don't carry the Ping sensor, so we don't have any example code using it. But you are correct; an 'if' statement will do what you want. Something like this:
Code: Select all
if (distance < 30)
{
servo.write(60)
}
else
{
servo.write(0)
}
-
- Posts: 20
- Joined: Thu Sep 06, 2012 10:20 am
Re: HC-SR04 Sensor Controlling Servo
Thank you for the reply.
I really want to get a good grasp on understanding the code rather than searching for sketches for every project I do. Are there video tutorials out there other than youtube?
Thanks again!
I really want to get a good grasp on understanding the code rather than searching for sketches for every project I do. Are there video tutorials out there other than youtube?
Thanks again!
- adafruit_support_bill
- Posts: 88088
- Joined: Sat Feb 07, 2009 10:11 am
Re: HC-SR04 Sensor Controlling Servo
Not sure about video tutorials, but there are lots of tutorials on Instructables. And we have a ton of tutorials covering most of the products we sell here:
http://learn.adafruit.com/
http://www.adafruit.com/tutorials
http://learn.adafruit.com/
http://www.adafruit.com/tutorials
-
- Posts: 20
- Joined: Thu Sep 06, 2012 10:20 am
Re: HC-SR04 Sensor Controlling Servo
I have combined the Servo Sweep sketch, the NewPing sketch and your suggestion and I must say I am pretty pleased so far.
At first my servo vibrated and felt very warm until I tweeked the code a bit changing the position that the servo stopped at from 0 to 20.
Also, I needed the servo to move when an object reached anywhere from 60 cm or closer so I changed the If statement from "if (cm = 60)" to "if (cm >= 01 && cm <=60)". Using && did the trick!! The reason I didn't use 0 to 60 is that the ultrasonic sensor returns 0 cm if there isn't an object within 500 cm. That would cause the servo to activate when there is not need.
At first my servo vibrated and felt very warm until I tweeked the code a bit changing the position that the servo stopped at from 0 to 20.
Also, I needed the servo to move when an object reached anywhere from 60 cm or closer so I changed the If statement from "if (cm = 60)" to "if (cm >= 01 && cm <=60)". Using && did the trick!! The reason I didn't use 0 to 60 is that the ultrasonic sensor returns 0 cm if there isn't an object within 500 cm. That would cause the servo to activate when there is not need.
Code: Select all
#include <Servo.h>
#include <NewPing.h>
Servo myservo; // create servo object to control a servo
int pos = 20; // variable to store the servo position
#define TRIGGER_PIN 13 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup()
{
Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
delay(100); // 29ms should be the shortest delay between pings.
int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters.
Serial.print("Ping: ");
Serial.print(cm); // Print the result (0 = outside the set distance range, no ping echo)
Serial.println("cm");
if (cm >= 01 && cm <=60) // If the cat sneaks up within 60cm he gets it!!
{
myservo.write(40); // the servo moves to trigger the trap
}
else
{
myservo.write(20); // return to starting position and wait for the cat
}
}
Please be positive and constructive with your questions and comments.