Problem with a project

Hey guys i have a problem with my school project, i use an ultrasonic sensor and a servo, i want that my servo turn until the distance between my sensor and the objects is equal to 30cm.

this is my program.

#include <Wire.h>
#include <Servo.h>
Servo servo;

void setup()
{
  servo.attach(9);
  Wire.begin();               
  Serial.begin(9600);         
}

int reading = 0;

void loop()
{
 Wire.beginTransmission(112); 
 Wire.write(byte(0x00));      
 Wire.write(byte(0x51));      
 Wire.endTransmission();     
 delay(70);                   
 Wire.beginTransmission(112); 
 Wire.write(byte(0x02));      
 Wire.endTransmission();      
 Wire.requestFrom(112, 2);   
 if (2 <= Wire.available())   
 {
  reading = Wire.read();  
  reading = reading << 8;    
  reading |= Wire.read(); 
  Serial.print(reading);   
  Serial.println("cm");
  delay(250);
  while(reading>30)   //here is the beginnig of my problem.
  {
   servo.write(5);  //here i would like to my servo turn 5° each 0.1second
   delay(100);
   if(reading==30)
   {
    delay(2000);
    servo.write(0);
    delay(6000);
   }
  } 
 }
 delay(250);              
}

So i would like to have some tips, advice or help.
Sorry for my bad english i am not english :D.

Please go back and change the posting of the code to code-tags. If you don't know how, see How to use the forum.

Good you post your code and what you want to do. But uhm, what is the problem you have? (DON'T answer that with "it does not work" ;))

It works, but it only turn 5degres and stop, i would like it to turn until that my sensor print 30cm

Now THAT is vital information. Now you only need to edit your post to use code-tags and we can explore the code to find the bug :slight_smile:

  servo.write(5); This sends the servo to 5 degrees, not an extra 5 degrees from the current position. You need to add 5 to the value each time before you write to it.

Here i did the code-tag

UKHeliBob:
  servo.write(5); This sends the servo to 5 degrees, not an extra 5 degrees from the current position. You need to add 5 to the value each time before you write to it.

So you suggest me to copy and past:
{
servo.write(5);
delay(100);
if(reading==30)
{
delay(2000);
servo.write(0);
delay(6000);
}
And just add
servo.write(10); instead of 5 ?

No, not copy pasting. Once you want to copy past chucks of code you need to think, can't I do better? Instead of writing a 5 (or 10 etc), use a variable which you increase in steps of 5 (aka, vary).

So you suggest me to copy and past:

No

Create a variable, let's name it servoPos and set it to zero. Then write servoPos to the servo instead of 5 and in the next line add 5 to servoPos so that next time you write it the servo moves to its new position.

However, there is a problem with your current code

  while(reading>30)   //here is the beginnig of my problem.
  {
   servo.write(5);  //here i would like to my servo turn 5° each 0.1second
   delay(100);
   if(reading==30)
   {
    delay(2000);
    servo.write(0);
    delay(6000);
   }
  } 
 }

What will happen if reading is greater than 30 ? You don't update it in the while loop so the while loop will go on for ever.

UKHeliBob:
No

Create a variable, let's name it servoPos and set it to zero. Then write servoPos to the servo instead of 5 and in the next line add 5 to servoPos so that next time you write it the servo moves to its new position
What will happen if reading is greater than 30 ? You don't update it in the while loop so the while loop will go on for ever.

Oh thanks I just understood what you said,
I actually do a tinny autofocus, when it will come to 30cm, it will permite to my friend camera to take a picture of the object and then my servo will go back to his first position etc

(deleted)