Scanning servo

I’ve got an IR scanning servo for an Arduino robot. It scans from 35 to 165 degs and back again. Right now all I have is it taking a leftside reading @ 35 and sending it to a subroutine to turn right if the reading is too close. The problem is it’s taking a reading @ 165 on the return scan also. Any ideas?

Thanks,

Dave

void loop()
{

{
for (pos = 35; pos <= 165; pos ++) //scan IR sensor from 35 to 165 degs from left to right
{
scanServo.write (pos); //writes the position to scanning servo
delay(5); //give it time to get there
}
if (pos==35) analogRead (sensorPin); //take an IR reading at 35 - left side
readDistance3 = analogRead(sensorPin);
if (readDistance3 > minDistanceLimit) goRight(); // if obstacle distance is closer than threshold og in opposite direction
goForward(); //continue going forward
}

{
for (pos = 165; pos >=35; pos --) //scans right to left

{
scanServo.write (pos);
delay (5);
}
if (pos==35) analogRead (sensorPin);
readDistance3 = analogRead(sensorPin);
if (readDistance3 > minDistanceLimit) goRight();
goForward();
}
}

for (pos = 35; pos <= 165; pos ++)      //scan IR sensor from 35 to 165 degs from left to right
   { 
     scanServo.write (pos);               //writes the position to scanning servo
     delay(5);                            //give it time to get there
   }

Why not just :
scanServo.write (165): ?

if (pos==35) analogRead (sensorPin);   //take an IR reading at 35 - left side

You have two calls like this, where you take a reading, but don't store it.

These are followed by:

readDistance3 = analogRead(sensorPin);

In these, you are taking a reading regardless of where the sensor is pointing.

The problem is it's taking a reading @ 165 on the return scan also. Any ideas?

I think so.

Paul,

In the setup I've set 3 variables... readDistance 1,2 and 3. The if statement looks to see if it's at 35 and then assigns the IR value to readDistance3 (that's the plan anyway). It works @ 35 fine but also calls the subroutine @ 165 also. Nowhere in the code do I tell it to branch off @ 165. I'm confused (last thing I coded was BASIC 35 years ago).

Groove,

I didn't want to wait for the servo to get to 165 before the drive wheels started. This way I can have the servo move a click, move the wheels a click, move the servo a click, etc.

The if statement looks to see if it's at 35 and then assigns the IR value to readDistance3

No, it doesn't.

   if (pos==35) analogRead (sensorPin);   //take an IR reading at 35 - left side
   readDistance3 = analogRead(sensorPin);

The if statement is evaluated. If it evaluates to true, analogRead is called, and the return value discarded.

Then, the sensor is read, and the value stored in readDistance3. This statement is executed EVERY time, regardless of the value of pos.

The same code is repeated in the second block.

how do I fix it?

if (pos==35) //take an IR reading at 35 - left side
   readDistance3 = analogRead(sensorPin);

Thanks, I'll try it out when I get home.

I finally got it to work the way I wanted. I gave it separate variables to write to as it exits each directional for loop. That did the trick. Thanks for the suggestions. http://www.youtube.com/watch?v=0kx28MCuL2E

Dave