2 IR sensors for one servo

Hi....

I am really struggling to get two IR sensors to control one servo. The first sensor needs to move the servo from 0-90 degrees whilst the second sensor moves it back from 90-0 degrees.

Firstly is this possible? secondly can anyone help?

Topic moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Guess so.

Maybe somebody can advise if you show what you have tried; don't forget to use code tags as described in https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems.

Like this:

if (sensor1) servo.moveTo(90);
else if (sensor2) servo.moveTo(0);

Many thanks.... Dr Diettrich....

I am very new to this and am struggling..

Does this look right?

#include <Servo.h>
Servo Serv;

int pinIR1=3;
int pinIR2=4
int myservo=5;
int val;

void setup(){
pinMode(pinIR1,INPUT);
pinMode(pinIR2,INPUT);
Servo.attach(myservo);
}

void loop(){

if (pinIR1) servo.moveTo(90);
else if (pinIR2) servo.moveTo(0);

}

First you need to post your code correctly so that it is readable.
In the IDE, click on Edit, then Copy for Forum, That will copy your code for use in this Forum.
Then come back here and just do a simple paste.

Is this the type of IR sensor you have

Yes





  #include <Servo.h>
  Servo Serv;

  int pinIR1=3;
  int pinIR2=4
  int myservo=5;
  int val;

  void setup(){
	pinMode(pinIR1,INPUT);
  pinMode(pinIR2,INPUT);
  Servo.attach(myservo);
  }

  void loop(){

if (pinIR1) servo.moveTo(90);
else if (pinIR2) servo.moveTo(0);

  }

Much better
Do you have a solderless breadboard to make all the connections?

yes....

I have a starter kit with many parts! but I am just struggling with the coding!

This is the information on the Arduino servo library.
https://docs.arduino.cc/libraries/servo/
DO NOT connect a servo to the 5V pin on the Arduino as the documentation states, even if it is just one servo.
Try the examples in the IDE and see if you can make the sweep example work

Click on File, then select Examples and scroll down to find Servo. There you will see the Sweep example.

thanks!

If you need more help, just ask.

This

  if (pinIR1) servo.moveTo(90);

won't do what you think. pinIR1 is always going to be true, the servo will always be told to move.

You need to read the pin to find the sensor value.

  bool sensorBeSensing = digitalRead(pinIR1) == LOW;   
  if (sensorBeSensing) servo.moveTo(90);

Your sensor may read HIGH when it is seeing motion.

I am unfamiliar with moveTo(), I assume you aren't making that part up.

The servo will move, but it takes time. If the other sensor be sensing, the servo will make crazy backing and forthing noises and motion.

a7

You are welcome.
Have a nice day!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.