Go Down

Topic: how to interface 2 ir sensor and servo motor please help..!! (Read 282 times) previous topic - next topic

Srijan_Bakshi

I have a code for the above, Everything is fine, the connections status but the servo motor is not moving .please tell me if my code is correct. Can we use pinMode command as many no. of times? Or is there some other problem.
Here is my code:-

int button =10;
 int ir_sensor =11 ;
 int ir_sonsor =12;
int pos = 0;    // variable to store the servo position
void setup()
{
  pinMode(ir_sensor, INPUT);
  pinMode(ir_sonsor,INPUT);
    myservo.attach(9);
    digitalWrite(ir_sensor, LOW);
    digitalWrite(ir_sonsor, LOW);
    Serial.begin(9600);
}
void loop()
{
if(digitalRead(ir_sensor || ir_sonsor)==HIGH)
{
if(key_1==1)
{
key_1=0;
Serial.println("DOOR OPEN");
 for(pos = 0; pos < 120; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15); 
  }
  delay(2000);
}
 
while(digitalRead(ir_sensor || ir_sonsor)==HIGH);
}
else
{
if(key_1==0)
{
key_1=1;
Serial.println("DOOR CLOSE");
   for(pos = 90; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);
  }

}
while(digitalRead(ir_sensor || ir_sonsor)==LOW);
}
}

Paul_KD7HB



Paul_KD7HB


Srijan_Bakshi

#include <Servo.h>
int key_1;
 
Servo myservo;


add this in the beginning then it will run

Paul_KD7HB

It may be the "for" loops. The examples do not show "+=" or "-=".

Paul

AWOL

Code: [Select]
digitalRead(ir_sensor || ir_sonsor)Oops

Please remember to use code tags when posting code
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

johnwasser

Code: [Select]
  if (digitalRead(ir_sensor || ir_sonsor) == HIGH)


I think you mean:
Code: [Select]
  if (digitalRead(ir_sensor) == HIGH || digitalRead(ir_sonsor) == HIGH)
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Srijan_Bakshi


Srijan_Bakshi

sir, it is now working but both are working together i want to operate from one sensor at a time..!!

Paul_KD7HB

sir, it is now working but both are working together i want to operate from one sensor at a time..!!
Please post your current code. After you have made the corrections already shown to you.

Paul

johnwasser

sir, it is now working but both are working together i want to operate from one sensor at a time..!!
Of course they are "working together" since they are both being tested at the same time.
It is not clear what you mean by "one sensor at a time". What, EXACTLY, do you want your sketch to do.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Srijan_Bakshi

I am using it in a car parking system and I am using 2 sensors at two sides of the servo motor as the entry and exit sensor in a parking lot .So if the car comes from one side then the respective sensor should sense and rotate the servo.
This is what I want to perform.

johnwasser

I am using it in a car parking system and I am using 2 sensors at two sides of the servo motor as the entry and exit sensor in a parking lot .So if the car comes from one side then the respective sensor should sense and rotate the servo.
Are you saying that you want one sensor to OPEN the gate when a car approaches the gate and the other sensor to CLOSE the gate when the car has passed through the gate?
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up