controlling servo using ir sensor

#include <Servo.h>
Servo ser;
int IRpin = 1; // analog pin for reading the IR sensor
int pos=0;

void setup() {
Serial.begin(9600);
// start the serial port
ser.attach(9);
}

void loop() {
float volts = analogRead(IRpin)0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65
pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
Serial.println(distance); // print the distance
if(distance>1000)
{
for(pos=0;pos<180;pos+=1)
{
ser.write(pos);
delay(15);
}
for(pos=180;pos>=1;pos-=1)
{
ser.attach(pos);
}
}

delay(100); // arbitary wait time.
}
this program works for 3 times…then the output values are random and the motor stops rotating.
please rectify the error asap.

Please read http://arduino.cc/forum/index.php/topic,97455.0.html, particularly point 6, with regard to posting code.

  for(pos=180;pos>=1;pos-=1)
  {
    ser.attach(pos);
  }

How many servos do you have?

i have 1 servo..

i have connected one servo with an arduino and an sharp ir sensor(2Y0A02 ) using different power supplies the problem is the servo works only three times and later it gives some arbitrary non ending loop value 420....with some symbols included.So what is wrong???

So what is wrong???

Well modify that first post like you were asked to and maybe some one will tell you.

trupthi: i have 1 servo..

Why are you trying to attach 180 of them then?

  for(pos=180;pos>=1;pos-=1)
  {
    ser.attach(pos);
  }

please rectify the error asap.

;-)

Duane B

can you please tell me what to change in the code as i am a little unfamiliar with the arduino

can you please tell me what to change in the code as i am a little unfamiliar with the arduino

No problem. All you need to do is tell us what you are trying to do in that loop, and we'll tell you what you need to change.

Although, I'm willing to bet that you can look at the method names in the Servo library (attach and write being the most commonly used) and determine what needs to be changed all by yourself.

Perhaps even study the other for loop for a clue by four.

basically…
we are trying to control the servo motor with the ir sensor.
when the ir sensor reads the distance and outputs it…
we want the motor to rotate when the distance is say less than a certain value(<200)…
and it should stop rotating if it is more than the given value.

we are trying to control the servo motor with the ir sensor.

Control is such a vague term that it is nearly meaningless. You must have some idea what you want the servo to do when the IR sensor returns a given distance. “Control” isn’t it.

we want the motor to rotate when the distance is say less than a certain value(<200)…
and it should stop rotating if it is more than the given value.

Is your shift key broken?

Which of the Servo methods is used to control the position of a servo?

  1. attach()
  2. write()
    You have a 50 % chance of getting the answer right without even studying.

when the ir sensor reads the distance and outputs it

I hate to tell you but an IR sensor will not output a number proportional to distance.