hi all
new to the arduino programming and i am tring to work out how to get 1 servo controlled by 2 IR distance sensors. i have 1 on the right of my robot and 1 on the left. 1 servo to turn the head left or right. the object is to detect something or someone, say on the left, and turn its head to the left. after the person or object is removed it return to forward. i can get it to turn left with 1 sensors attached, works as it should. my problem is when i connect the 2 sensors the servo just goes in to sort of a sweep function. if i cover one of the sensors it does what it is meant to do, well either sensor actually. but when none are covered it moves left and right continuosly.
any help would be greatly appreciated
thanks
Doug
#include <Servo.h>
#define irLedPin 4
#define irSensorPin 5
#define irSensorPin2 6
#define irLedPin2 7
int irRead(int readPin, int triggerPin);
Servo myservo;
void setup()
{
myservo.attach(3);
myservo.write(0);
pinMode(irSensorPin, INPUT);
pinMode(irLedPin, OUTPUT);
pinMode(irSensorPin2, INPUT);
pinMode(irLedPin2, OUTPUT);
Serial.begin(9600);
Serial.print(“Program Starting”);
delay(1000);
}
void loop()
{
// Serial.print(irRead(irSensorPin, irLedPin));
if( irRead(irSensorPin, irLedPin ) )
{
myservo.write(180);
}
else
{
myservo.write(90);
}
delay(1000);
//Serial.print(irRead(irSensorPin2, irLedPin2));
if( irRead(irSensorPin2, irLedPin2 ) = HIGH)
{
myservo.write(0);
}
else
{
myservo.write(90);
}
delay(1000);
}
int irRead(int readPin, int triggerPin)
{
int halfPeriod = 13;
int cycles = 38;
int i;
for (i=0; i <=cycles; i++)
{
digitalWrite(triggerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(triggerPin, LOW);
delayMicroseconds(halfPeriod - 1);
}
return digitalRead(readPin);
}