
Arduino Robot Avoid Obstacles
thingiverse
Materials - nr. 2 micro servi - rotation 360° - nr. 1 sensor HC-SR04 - nr. 1 Arduino Uno - nr. 1 Battery 9 volt Arduino Code: "#include<Servo.h>" const int triggerPort = 11; const int echoPort = 12; Servo servoLeft; Servo servoRight; void setup(){ pinMode(triggerPort, OUTPUT); pinMode(echoPort, INPUT); servoLeft.attach(9); servoLeft.write(90); servoRight.attach(10); servoRight.write(90); Serial.begin(9600); } void loop(){ long distance = CalculateDistance(); if (distance < 15) { Stop(); ChooseDirection(); Stop(); } else { Go(); } //delay(1000); } long CalculateDistance(){ Serial.println("CalculateDistance"); digitalWrite( triggerPort, LOW ); digitalWrite( triggerPort, HIGH ); delayMicroseconds(10); digitalWrite( triggerPort, LOW ); long time = pulseIn( echoPort, HIGH ); long distance = 0.034 * time / 2; return distance; } void Go(){ Serial.println("Go"); servoLeft.write(120); servoRight.write(60); } void Stop(){ Serial.println("Stop"); servoLeft.write(90); servoRight.write(90); delay(2000); } void ChooseDirection(){ Serial.println("ChooseDirection"); int randomChoose = random(1, 4); switch (randomChoose) { case 1: Serial.println("Right 2sec"); servoLeft.write(90); servoRight.write(120); delay(2000); break; case 2: Serial.println("Left 2sec"); servoLeft.write(60); servoRight.write(90); delay(2000); break; case 3: Serial.println("Right 4sec"); servoLeft.write(90); servoRight.write(120); delay(4000); case 4: Serial.println("Left 4sec"); servoLeft.write(60); servoRight.write(90); delay(4000); break; } }
With this file you will be able to print Arduino Robot Avoid Obstacles with your 3D printer. Click on the button and save the file on your computer to work, edit or customize your design. You can also find more 3D designs for printers on Arduino Robot Avoid Obstacles.