Seems like I repaired the code for exploration. Now it works everytime i plug it into usb but not on external baterry!
/*
# This code has been inspired by an article from Dominique Meurisse : http://arduino103.blogspot.fr/2011/06/detecteur-de-proximite-infrarouge-sharp.html
#
# this script is distributed under Creative Commons Attribution-ShareAlike 3.0 License
*/
void Explore(){
#define trigPin 13
#define echoPin 12
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
if(distance>15){
obstacle = false;
advance (200,200);
}
else {
if(distance<10 && distance>0){
back_off (200,200);
}
else{
if(distance>0){
obstacle = true;
if(GetPosition() > GetMiddlePosition()){
turn_R (200,200);
SetServoDirection(0);
}
else{
turn_L (200,200);
SetServoDirection(1);
}
}
}
}
}
I add my previous project to find out if sensor works. It measures the distance in cm!