Micro Servos work in response to an IR when detecting something

I need help coding a micro servo to work in response to an IR sensor when the sensor detects something. I have checked multiple websites and I haven’t found anything. The IR sensor I am using is a Model ARD0077. Any suggestions? I have a picture of the sensor. I also only have part of the code but only for it to work not to get micro servos to work in response. Here is the code too:

// Variables for connection from Arduino and H Bridge
int isObstaclePin = 11; // First IR sensor for
int isObstacle = HIGH;

void setup(){
pinMode(isObstaclePin, INPUT); // Sets IR sensor to be input
Serial.begin(9600);
}

void sensor(){
isObstacle = digitalRead(isObstaclePin);
if (isObstacle == LOW)
{
Serial.println(“0”); // On the Serial monitor it will read 0 when something is detected
}
else
{
Serial.println(“1”); // On the Serial monitor it will read 1 when nothing is detected
}
delay(50);
}

void loop() {
sensor(); // Initializes to control IR sensor
}

the-infrared-barrier-modules-arduino-obstacle-avoidance-dolly-obstacle-avoidance-sensor-.jpg

Please edit the post and add code tags (see how to use this forum)

Next, what do you want the servo to do when the IR detects something? Explode? Change color? Go to a certain angle?

Just to clarify: does the code you posted work?- ie are you getting the serial print you expect?

And by the way, please modify your post, select the code, and hit the </> icon top left so that…

.... your code appears in a box like this.

edit… here’s the OP’s pic:

85fac2c8005107cc38484ba5023ad64f1461d6fa.jpg

Try working through some of the Servo class examples provided in the IDE.

Ok, I have multiple people to answer to. I will start with septillion. So, this is the first time I have used the forums so I didn't know how to use it correctly. Also the servo is connected to an arm I am working on in my robotics class. I need the servos to move at a horizontal angle when the IR sensor detects for example a ball. The IR sensor tells the servos a ball is there and the servos manipulate the arm to grab the ball.
Now JimboZA, thanks for telling me the correct way to put in the code. Also, I have already tested the code and it works perfectly right now. I just need to figure out the coding for the servos that I mentioned up above.

HIandKABOOM:
I have already tested the code and it works perfectly right now. I just need to figure out the coding for the servos that I mentioned up above.

Well next is to do what AWOL suggested and see how to get a servo working.

Then, include the servo library and put the Servo myservo and servo.attach() into your existing code. After that, put some servo.write() commands in the part of the existing code where the sensor detected the object. Then it's time to experiment but that will at least get the servo moving in response to the sensor.

HIandKABOOM:
So, this is the first time I have used the forums so I didn't know how to use it correctly.

That's why there is is big fat sticky topic at the top of each section that says in large and friendly letter "How to use this forum - please read." :wink: Mannn, I wish it would work as good as it does in the book....

But to get on topic, you have to figure out what those positions are then. To do so you can for example use the Servo->Knob example in the IDE. After that it's just a matter of embedding the Servo library into this project :slight_smile:

Okay. I just found out from my robotics teacher a bit of what I need to do. I already have the sensor working but for the servos, he said to start off with codes that manipulate the servos in a certain amount of time and then change the time to work with sensor instead. I have a code I worked on with a few partners a couple weeks ago. Do I use some of the code I worked on with my lab partners a couple weeks ago? We used an ultrasonic rangefinder instead of the IR sensor that I am using(we didn’t have an arm on that project since I am working with new partners and a different project). Here is the code I worked on a couple weeks ago:

 int TrigPin = 11; //a pin
 int EchoPin = 12;//a pin
 int enA = 10;//a pin
 int in1 = 9;//a pin
 int in2 = 8;//a pin
 int enB = 5;//a pin
 int in3 = 7;
 int in4 = 6;
 int distance;
 long duration;
 int green = 4;
 int red = 3;
 void setup(){
  pinMode(enA,OUTPUT);//enA is now a pin
  pinMode(enB,OUTPUT);//enB is now a pin
  pinMode(in1,OUTPUT);//in1 is now a pin
  pinMode(in2,OUTPUT);//in2 is now a pin
  pinMode(in3,OUTPUT);//in3 is now a pin
  pinMode(in4,OUTPUT);//in4 is now a pin
  pinMode(TrigPin,OUTPUT);//a pin
  pinMode(EchoPin,INPUT);//a pin
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
  Serial.begin(9600);
 }
 void loop(){
  digitalWrite(TrigPin,LOW);//
  delayMicroseconds(2000);//
  digitalWrite(TrigPin,HIGH);//
  delayMicroseconds(2000);
  digitalWrite(TrigPin,LOW);
  duration = pulseIn(EchoPin,HIGH);//
  distance = (duration/2)/29.1;//
  Serial.println(distance);
  digitalWrite(green,LOW);
  digitalWrite(red,LOW);
  if (distance >= 10 ){
    turnRight();//If distance is greater than 20cm, go right
    digitalWrite(green,HIGH);
  }
  else if (distance < 10){
    turnLeft();//If distance is less than 10cm, go left
    digitalWrite(red,HIGH);
    
  }
  else if ((distance==0)||(distance>100)){
    digitalWrite(in1,LOW);
    digitalWrite(in2,LOW);
    digitalWrite(in3,LOW);
    digitalWrite(in4,LOW);
  }
  }
 void turnRight(){
  digitalWrite(in1,HIGH);//in1 power
  digitalWrite(in2,LOW);// in2 ground
  analogWrite(enA,200);// speed of motor A
  digitalWrite(in3,HIGH);// in3 power
  digitalWrite(in4,LOW);//in4 ground
  analogWrite(enB,150);//speed for motor B
 }
 void turnLeft(){
  digitalWrite(in1,HIGH);//in1 power
  digitalWrite(in2,LOW);// in2 ground
  analogWrite(enA,240);// speed of motor A
  digitalWrite(in3,HIGH);// in3 power
  digitalWrite(in4,LOW);//in4 ground
  analogWrite(enB,255);//speed for motor B
 }

he said to start off with codes that manipulate the servos in a certain amount of time and then change the time to work with sensor instead.

I have no idea what this means.

UKHeliBob:
I have no idea what this means.

Okay, here is what I mean. I was manipulating servos within a certain time frame like 10 seconds. I need to change that to work with the IR sensor instead.

I presume that I am being particularly dense today, but I still don't understand.

UKHeliBob:
I presume that I am being particularly dense today, but I still don't understand.

Well, I am sorry that I can't explain to you in a way that you can understand. That is the best I can explain it.

Please write the sequence of events that you want to happen step by step.

What should happen first and how long for
What happens next and how long for
etc, etc

UKHeliBob:
Please write the sequence of events that you want to happen step by step.

What should happen first and how long for
What happens next and how long for
etc, etc

Ok. I won't be able to right now though since I need to clean up before I go to my next class. I will tell you in a better way so you can understand it around 9:40 AM Pacific Time.