code for proximity sensor to detect and rotate a servo motor help needed!

i know some code of proximity but i dont know anything about servo code help me insert servo code inside this if value is 1 rotate 90degree right whereas if value is 1 rotate left 90degrees i want something like this pls help

int limitSwitch = 13;
int state = LOW;
void setup() {
  
  Serial.begin(9600);
  pinMode(limitSwitch,INPUT);
  
}

void loop() {
  
    int val = digitalRead(limitSwitch);

    
    }

NINZJAZZ:
if value is 1 rotate 90degree right whereas if value is 1 rotate left 90degrees

So just to check, you want it to go to two different places with the same input?

Or should one of those be "0"?

First, let's discuss your switch. Do you have a pull down resistor on it? (Asking since you have it pinMode()-ed as INPUT, so it would be wired from pin to 5V?) If not, rather change that to INPUT_PULLUP and wire the switch from pin to ground. Then when switch is open,pin is high; when it is closed, pin goes low.

Second, have you digested the servo examples?

By the way, what you are picturing as 90 left and 90 right will be 0 and 180 (or 180 and 0) for the servo library, with 90 in the middle.

Willpatel_Kendmirez:
So just to check, you want it to go to two different places with the same input?

Or should one of those be "0"?

First, let's discuss your switch. Do you have a pull down resistor on it? (Asking since you have it pinMode()-ed as INPUT, so it would be wired from pin to 5V?) If not, rather change that to INPUT_PULLUP and wire the switch from pin to ground. Then when switch is open,pin is high; when it is closed, pin goes low.

Second, have you digested the servo examples?

yeah u are right 1 of them should be 0 that is my typo sorry i have 2 1* k resistor in proximity sensor circuit it is working it would be wired to 5v by breadboard if u can suggest some good ideas to connect explain to me please
and those servo examples i digested only potentiometer one second one got over my head
sorry for my bad english

1 Like

The below code swings the servo side to side when you press or release a button. I have the button from pin 14 to ground, and use input pullup.

// side to side servo with a button

#include <Servo.h>
Servo myServo;

byte servoPin = 10;
byte buttonPin = 14;
byte servoPos;

void setup()
{
  myServo.attach(servoPin);
  pinMode(buttonPin, INPUT_PULLUP);
} //setup

void loop()
{
  if (digitalRead(buttonPin) == HIGH)
  {
    myServo.write(0);
  }
  else
  {
    myServo.write(180);
  }

} //loop

can u tell what extra parts i would need rn i have 1 breadboard mega2560 5 1k ohm resistor please also tell how to connect them

pls tell me additional parts and tell me how to put it together i did the servo and ultrasonic but inductive sensor would take resistor as i heard pls tell how should put them

The term "inductive sensor" is too general for us to be able to help you. Post a data sheet for the sensor and we may be able to help.

groundFungus:
The term "inductive sensor" is too general for us to be able to help you. Post a data sheet for the sensor and we may be able to help.

CHPSS570 Lj12A3-4-Z/Bx Inductive Proximity Sensor Detection Switch Npn Dc6-36V 4mm Normally Open Proximity SwitchVoltage: dc 6-36v, output current: 300ma, 3 wire
Polarity: npn, output status: normally open
Dimension: m12x10x55mm
Detection objects: conductor, detection distance: 4 mm
would this help?

Please don't cross post!
http://forum.arduino.cc/index.php?topic=590778

Duplicate posts waste the time of the people helping you. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread. This behavior is not very respectful to the people you're asking for assistance. Just because they give their time freely doesn't mean it has no value.

In the future, take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Chuir mi na snàithleanan còmhla. Dà uair.

@NINZJAZZ, stop cross-posting. Last warning.

#include <Servo.h>  //add '<' and '>' before and after servo.h

float metalDetected;
int proximitySensorValue;
int proximitySensorPin = A1;

int servoPin = 4;
int servoAngle = 0;   // servo position in degrees
Servo servo;  

const int trigPin = 2;
const int echoPin = 3;
long duration;
int distance;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  servo.attach(servoPin);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

}

void loop() {
  // put your main code here, to run repeatedly:
  proximitySensorValue = analogRead(proximitySensorPin);
  metalDetected = (float) proximitySensorValue*100/1024.0;
  Serial.print("Object detected: ");
  Serial.print(metalDetected);
  Serial.println("%");
}

it should detect metal with the inductive sensor however this is the out put in the serial monitor

Object detected: 99.90%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 61.82%
Object detected: 0.00%
Object detected: 61.04%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 7.71%
Object detected: 0.00%
Object detected: 30.76%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 80.76%
Object detected: 0.00%
Object detected: 57.91%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 35.84%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%
Object detected: 99.90%
Object detected: 0.00%
Object detected: 0.00%

in a constant loop even when the sensor is not close to a metal i want it to just tell me that metal is detected or not if metal is detected show 1 if not send 0 simple as that i have atached the pin to analog and connected the inductive sensor with a separate power supply 9v for whereas arduino is supplied with the usb and motor is by 5v
please help

I think that we need a data sheet for the sensor and a schematic showing how it is wired to the Arduino. I suspect a floating input.

I agree. Most induction sensors I know are open collector and not analog.

groundFungus:
I think that we need a data sheet for the sensor and a schematic showing how it is wired to the Arduino. I suspect a floating input.

groundFungus:
I think that we need a data sheet for the sensor and a schematic showing how it is wired to the Arduino. I suspect a floating input.

CHPSS570 Lj12A3-4-Z/Bx Inductive Proximity Sensor Detection Switch Npn Dc6-36V 4mm Normally Open Proximity Switch
Part number: lj12a3-4-z/bx
Voltage: dc 6-36v, output current: 300ma, 3 wire
Polarity: npn, output status: normally open
Dimension: m12x10x55mm
Detection objects: conductor, detection distance: 4 mm

Yep, NPN output. Use a pull up and read digital

#include <Servo.h>  //add '<' and '>' before and after servo.h

const byte ProximitySensorPin = A1;
const byte TrigPin = 2;
const byte EchoPin = 3;

const byte ServoPin = 4;
byte servoAngle = 0;   // servo position in degrees
Servo servo; 


long duration;
int distance;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  servo.attach(servoPin);
  pinMode(TrigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(EchoPin, INPUT); // Sets the echoPin as an Input
  pinMode(ProximitySensorPin, INPUT_PULLUP);

}

void loop() {
  // put your main code here, to run repeatedly:
  if(!digitalRead(ProximitySensorPin)){
    Serial.print("Object detected!");
  }
}

Oh @NINZJAZZ, you have been warned repeatably to stop cross-posting. What to do with you? What to do?

Threads merged.