Problems with programming ultra sensor to servo.

Hello,

I am kinda new so will need some help.

I want my servo to open (move 90 degrees) when my ultrasonic sensor reads anything between 1 and 10 centimers. And when the object is removed it want it to close again so move 90 degrees.

This is the code I have right now.

#include <NewPing.h>
#include <Servo.h>

const int Servopin = 11;
const int Triggerpin = 3;
const int Echopin = 2;

//100 = maxDistance
NewPing sonar(Triggerpin, Echopin, 100);
Servo servo;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo.attach(Servopin);
}

void loop() {
int cm = sonar.ping_cm();
Serial.println (cm);

int angle = map(cm, 1, 10, 15, 90);
servo.write(angle);

delay(3000); // put your main code here, to run repeatedly:

}

Please let me know what i have to do since I am out of luck.

Thanks guys! :slight_smile:

Hi,

Here's an example that does what you want:

http://arduinoinfo.mywikis.net/wiki/RobotCatDoor

I use this as an example of what Arduino can do:

Yes that is exactly what I mean!! thank you very much!

But now when my ping is reading 0 it opens the servo again. What code do I add so even when it reads zero it stays closed? But only when it happens when it was already closed.

If you change your code, post the new version so that we can keep up.

This is what I have right now. But when the door is closed it reads less than 22 cm but sometimes a 0 as well. I understand that it is a misread but would like the door only to open when it is above 25 cm.

#include <Servo.h>
#include <NewPing.h>

/-----( Declare Constants and Pin Numbers )-----/
#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define DOOR_OPEN 90 // Angle for Open Door

#define SERVO_PIN 11 // Servo plugs into Pin 11

/-----( Declare objects )-----/
Servo myservo; // create servo object to control a servo
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

/-----( Declare Variables )-----/
int pos; // variable to store the servo position

unsigned int uS;
unsigned int DistanceCm;

int DoorIsOpen;

void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
myservo.attach(SERVO_PIN); // attaches the servo on pin 11 to the servo object
DoorIsOpen = 0;
myservo.write(10); //Door Start Position
}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
delay(500); // Wait 500ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
DistanceCm = (uS / US_ROUNDTRIP_CM);
Serial.print(DistanceCm);
Serial.println("cm");
if (DistanceCm == 0 ) DistanceCm = 88; // Fix if bad reading

if (DistanceCm <= 22 and !DoorIsOpen)
{
Serial.println("???");
OpenDoor();
delay(1000);
}

if ((DistanceCm >= 25) && (DoorIsOpen))
{
Serial.println("!!!");
CloseDoor();
delay(1000);
}

}//--(end main loop )---

/-----( Declare User-written Functions )-----/

int OpenDoor()
{
for (pos = 10; pos <= DOOR_OPEN; pos += 5) // goes from 10 degrees to 160 degrees
delay(100);
{

myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(69); // Wait 500ms between pings
}
DoorIsOpen = 1;
}

int CloseDoor()
{
for (pos = DOOR_OPEN; pos >= 10; pos -= 5) // goes from 160 degrees to 10 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(69);
}
DoorIsOpen = 0;
}

  for (pos = 10; pos <= DOOR_OPEN; pos += 5) // goes from 10 degrees to 160 degrees
  delay(100);

Why is it necessary to delay 100 milliseconds 16 times? The comment is crap, since DOOR_OPEN is NOT 160.

The code in the (useless) curly braces after this snippet is unconditionally executed ONCE, and the servo will be commanded to 95 degrees.

When you post code, you need to tell us what it actually does, and how that differs from what you expect. "It doesn't work" is useless noise.

I assume that your intent on the OpenDoor() function is to gradually open the door. But as PaulS points out, your braces are in the wrong place.

One way to help identify improperly placed curly braces is to auto format the code.
CTRL-T will do so. Try it and see what it does.

Also, when you post your code to this forum, please use code tags. It helps us read your code so it helps us help you. And it fixed the issue where the forum software mis-interprets some code as smile faces.