/*detection of ultrasonic distance*/ int watch(){ long howfar; digitalWrite(Trig_PIN,LOW); delayMicroseconds(5); digitalWrite(Trig_PIN,HIGH); delayMicroseconds(15); digitalWrite(Trig_PIN,LOW); howfar=pulseIn(Echo_PIN,HIGH); howfar=howfar*0.01657; //how far away is the object in cm //Serial.print((int)howfar); //Serial.println(" cm"); return round(howfar); } //Meassures distances to the right, left, front, left diagonal, right diagonal and asign them in cm to the variables rightscanval, //leftscanval, centerscanval, ldiagonalscanval and rdiagonalscanval (there are 5 points for distance testing) void watchsurrounding(){ centerscanval = watch(); if(centerscanval100){ //Watch if something is around every 100 loops while moving forward watchsurrounding(); if(leftscanvaldistancelimit){ thereis=0;} //Count is restarted if (thereis > 25){ stop_Stop(); // Since something is ahead, stop moving. Serial.println("stop"); thereis=0; } }