how can I control 360 servo using inductive proximity sensor?

Hi Good evening.

I having trouble with my codes because I wanted to control 360 servos using inductive proximity sensor.

when sensing the wall or things, the servo won’t move or stop. when it doesn’t sense anything it would be moving around until when it could sense another wall.

here the code:

#include <Servo.h>

int x = 0;
int y = 0;
int limitIN = 13;

Servo myservo;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
pinMode( limitIN, INPUT );
pinMode( limitOUT , INPUT );
myservo.attach(9);

}

void loop() {

int val = digitalRead(limitIN);
if ( val == 0 )
{
myservo.write (90);
Serial.println( “(0) Target Hit!” );
}
else if (val == 1)
{
myservo.write (85);
Serial.println( “(1) None”);
}
}

when I try this code. when it sensing something it still keeps spinning.

thank you for your help

The title sounds like you have 360 servos you want to control, I will assume it is just one.

A servo that moves 360 degrees generally can not be set to move to a specific angle, they can just be controlled to a specific direction and speed.

There are rare exceptions to this. What model of servo do you have and can you provide a link to where you got it please?

You have two problems, not one, right? Either problem might be about the circuit or your coding.

Can your sensor detect the wall? If it can, you're good. If it can't, fix your first problem. It has nothing to do with the servo.

Can you get your servo move (forget the sensor, just get the servo move)? If you can, you're good. If you can't, fix this problem. It has nothing to do with the sensor.

If you've fixed these two separate problems and still can't get the whole working, there is something wrong in your circuit, like conflicting data pins and wires, or poor powering.

when I try this code. when it sensing something it still keeps spinning.

So when you get the print out "(0) Target Hit!", it still keeps spinning? It means myservo.write (90); doesn't stop the servo? That is the code you have to check separately in another sketch, which ignores the sensor.

  int val = digitalRead(limitIN);
    if ( val == 0 )
    {
      myservo.write (90);
    Serial.println( "(0) Target Hit!" );
    }

If this is meant to stop the servo rotating when limitIN goes HIGH I have a couple of questions

1 - how is the input wired ? Do you have pullup or pulldown resistors in place ?
2 - are you sure that the servo actually stops when you write 90 to it in a simple sketch ?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: