Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <NewPing.h>
- #include <Servo.h>
- #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
- #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
- #define MAX_DISTANCE 250 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
- Servo left,right; // 360 servo
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
- int led=13;
- int pos=0;
- int pos1=0;
- void setup() {
- Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
- pinMode(13, OUTPUT);
- left.attach(6);//left side
- right.attach(5); //right side
- }
- void loop() {
- Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
- Serial.println("cm");
- if (sonar.ping_cm() >= 15 && sonar.ping_cm() != 0) //move forward
- {
- digitalWrite(led, LOW);
- for (pos1 = 95; pos1 <= 360; pos1 += 1) {
- right.write(pos1);
- left.write(-pos1);
- }
- }
- else if (sonar.ping_cm() > 0 && sonar.ping_cm() <= 15 ) // move left
- {
- for(pos1=95; pos1<=360; pos1 +=1){
- right.write(pos1);
- left.write(pos1);
- }
- }
- else //move backward
- {
- digitalWrite(led,HIGH);
- for (pos1 = 95; pos1 <= 360; pos1 += 2){
- right.write(-pos1);
- left.write(pos1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement