object detection sensor (ir) control Servo

I need that if object detection sensor (ir) is detected the servo should rotate 9 degree rotation.the problem is that i have a program shown below, it rotates 9 degree and it comes back to 0 degree I don’t need to come to 0 degree ,it should continuously rotate . Please support me.

#include <Servo.h>

Servo tap_servo;

int sensor_pin = 7;
int tap_servo_pin =6;
int val;

void setup(){
pinMode(sensor_pin,INPUT);
tap_servo.attach(tap_servo_pin);

}

void loop(){
val = digitalRead(sensor_pin);

}
if (val==0)
{tap_servo.write(9);
}
}

You are not getting a compile time error?

void loop(){
  val = digitalRead(sensor_pin);

  } /// the code past this point is NOT in loop()
  if (val==0)
  {tap_servo.write(9);
    }
}
}

Try using the auto format feature of the Arduino IDE and, when posting code, use code tags.

Hi,
Sorry, Code having error.

#include <Servo.h>

Servo tap_servo;

int sensor_pin = 7;
int tap_servo_pin =6;
int val;

void setup(){
pinMode(sensor_pin,INPUT);
tap_servo.attach(tap_servo_pin);

}

void loop(){
val = digitalRead(sensor_pin);
if (val==0)
{tap_servo.write(0);
}
if (val==1)
{tap_servo.write(9);
}
}

if object detection sensor (ir) is detected the servo should rotate 9 degree rotation

do you mean "while" the sensor is detected?

the problem is that i have a program shown below, it rotates 9 degree and it comes back to 0 degree I don't need to come to 0 degree ,it should continuously rotate

instead of "continuously rotate", do you mean stay at 9 deg?

if you're using a switch as an sensor input, pressing the switch causes the servo to swing to 9 deg and releasing the switch causes the servo to swing back to 0 deg.

do you need two sensors?

Remember that loop() repeats very very quickly. So every time round it checks the sensor and writes either 0 or 9 to the servo. In your code, as soon as the sensor goes low the servo returns to 0. What EXACTLY did you want it to do?

Also you need to be aware that writing(9) to a servo is asking it to move to POSITION 9. If you then write(9) to it again it will not rotate another 9 degrees, it will not move at all because it is already at position 9.

Steve

I don’t want to again come to 0 degree it should rotate 9 degrees continuously.

slipstick: Remember that loop() repeats very very quickly. So every time round it checks the sensor and writes either 0 or 9 to the servo. In your code, as soon as the sensor goes low the servo returns to 0. What EXACTLY did you want it to do?

Also you need to be aware that writing(9) to a servo is asking it to move to POSITION 9. If you then write(9) to it again it will not rotate another 9 degrees, it will not move at all because it is already at position 9.

Steve

Thanks a lot your reply.

I need to rotate the servo by 9 degree if there is no object and delay by 30 sec and then need to check again , still there is no object it should rotate further 9 degree.

It should not come 0 Degree

Then how to write? Please suggest.

"I don't want to again come to 0 degree it should rotate 9 degrees continuously."

That is not very clear as to what you want to happen. Do you want the servo to keep moving back and forth looking for something to detect? A more detailed description is probably needed.

It sounds like you're saying you want to ADD 9 to the amount written to the servo each time there's no object. That's easy enough but I'm not sure that's really what you want. Try it and see, but bear in mind that 180 is the maximum you can write to a servo. After that you must stop adding 9 or go back to 0.

Steve

slipstick: It sounds like you're saying you want to ADD 9 to the amount written to the servo each time there's no object. That's easy enough but I'm not sure that's really what you want. Try it and see

Steve

Yes, that is what I need but I should rotate up to 360 degrees.

is there any solution to that or we can use any other motor instead of servo

Rithik email: rithikthegr8@gmail.com

Hi,

I am working on a robot. I want to rotate the plate by 9 degrees and increase each time by 9 degrees(9,18,27…) and rotate continuously

9 degree should rotate whenever object avoidance IR sensor detecting the object.

I have tried with servo, but I had the following problem

  1. Every time it back to 0 positions which I don’t want, I want only increment and rotate continuously
  2. Servo rotating up to 180-degree to 270 degrees.
    a) Continuous rotation servo - I could not get it right now
    b) Modify a standard servo - Did it .but still not working

Question:

  1. Please support to get to solve the above problem
  2. Is there any other motor available to solve this problem.

A stepper motor, or a brushed DC motor with encoder will work. Pan drives are available at servocity.com

It's fairly easy to get 9, 18, 27, 36 degrees etc but if you really need to go all the way to 360 degrees then you'll need something other than a standard servo.

A stepper motor could do what I think you want. So could a sail winch servo which has a maximum travel of around 3.5 turns. You could probably even use a geared DC motor and encoder. Lots of options.

Steve

jremington: A stepper motor, or a brushed DC motor with encoder will work. Pan drives are available at servocity.com

Thanks for your fast reply.

Could you please explain the details?

Can you specify the motor name which I will buy and try

Thanks a lot.

Could you please suggest some DC motor and encoder which I will buy via amazon

You need to provide the details of your project. "the plate" is not adequate.

Please find the attached image. I want to rotate this plate by 9 degrees each time up to 360 degrees with high precision.

Image from Reply #4 so we don't have to download it. See this Simple Image Posting Guide

|472x500

...R

I have googled and the stepper motor seems to ok. I want more accuracy. which one is best steeper or DC motor?

Stepper motor Cycle looks 1.8 degrees. I want 9 degrees. So 5 cycle is ok

It's a nice picture but it does not tell us much - not even its size or weight.

For a wheel to rotate it must be attached to something that can support it. What will that be? It would be a big help if you can post a diagram or sketch of the machine you hope to make.

I find it hard to make sense of this

9 degree should rotate whenever object avoidance IR sensor detecting the object.

because there is no indication of how the wheel could get in the way of, or move out of the way of an object.

Before choosing any motor you need to figure out the required torque and speed. There is a simple method for measuring torque in Stepper Motor Basics

...R