Self Opening Box using Servo Motor and Ultrasonic Sensor

Hello, my friend and I are creating a box that opens when you approach it. We are using a servo motor to open it and an ultrasonic sensor to detect when you get near. This is our first project so we don't know what's wrong with the code. The code is down below.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
Serial.begin(9600);

pinMode(9, OUTPUT);

myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

int distanceThreshold = 0;

int cm = 0;

int inches = 0;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}

void loop()
{
0.01723 * readUltrasonicDistance(0, 0);

distanceThreshold = 50;
cm = 0.01723 * readUltrasonicDistance(7, 6);
inches = (cm / 2.54);
Serial.print(cm);
Serial.print("cm,");
Serial.print(inches);
Serial.print("in");
if (cm > distanceThreshold) {
for (pos = 90; pos <= 0; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
}
if (cm <= distanceThreshold && cm > distanceThreshold - 40) {
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'

}
}
delay(100); // Wait for 100 millisecond(s)
}

Add some prints to get your sketch to tell you what it is doing.
Oh, wait.

Please remember to use code tags when posting code.

0.01723 * readUltrasonicDistance(0, 0);

What is this line supposed to do ?

What other prints should I add? I already have prints to say how far the sensor is viewing.

AWOL:
Add some prints to get your sketch to tell you what it is doing.
Oh, wait.

Please remember to use code tags when posting code.

I already have prints to say how far the sensor is viewing.

And what do they tell us?

121in307cm,121in307cm,121in307cm,121in307cm,121in307cm,121in307cm,121in307cm,121in307cm

AWOL:
And what do they tell us?

Do you think it might be helpful to tell us what the program actually does and how that is different from what you want it to do?

Steve

slipstick:
Do you think it might be helpful to tell us what the program actually does and how that is different from what you want it to do?

Steve

Sorry, we would like the servo to move 180 degrees when you are within 50 centimeters but instead, it doesn't move.

And do your Serial.prints ever tell you that you are within 50cm? If not perhaps the problem is with reading the sensor?

BTW the code only tries to move the servo to and from 90 not 180 despite what the idiot comments say.

 for (pos = 90; pos <= 0; pos += 1) { // goes from 0 degrees to 180 degrees

And if pos starts at 90 and you keep adding 1 to it how soon do you think it will be <=0?

Steve

Since all of your comparisons are in cm, why are you bothering to calculate the distance in inches?

 delay(100); // Wait for 100 millisecond(s)

Ditch the "Coding for complete morons" book, and move up to "Coding for Dummies".

Any with more than 2 IQ points can see what that code does. The useless comment adds 0 value.