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
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
}
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.
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.