Hey guys,
i wanna do Obstacle Robot add photo at attachment. I used L293D Motor Driver Shield,HC-SR04 sensor,2 piece DC motor 9v and servo motor.
I try to just 2 piece dc motor is working and servo motor.
I wrote code but My servo motor is turnnig just. Someone help me ? Chek my connection and my code ??
My code is ;
#include <Servo.h>
#include <NewPing.h>
#include <AFMotor.h>
#define echoPin A0
#define trigPin A1
#define maximum_distance 200
NewPing sonar (trigPin, echoPin, maximum_distance);
AF_DCMotor LeftMotor(3);
AF_DCMotor RightMotor(4);
Servo servo1;
int sure, distance;
void setup() {
pinMode(echoPin,INPUT);
pinMode(trigPin,OUTPUT);
Serial.begin(9600);
LeftMotor.setSpeed(255);
RightMotor.setSpeed(255);
servo1.attach(9);
servo1.write(90);
delay(2000);
}
void loop() {
int sure= analogRead(echoPin);
Serial.println(sure);
delay(300);
digitalWrite(A1, sure);
digitalWrite(trigPin,LOW);
delayMicroseconds(5);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
sure=pulseIn(echoPin,HIGH);
distance=sure / 29.1 / 2;
int distanceRight = 2;
int distanceLeft = 2;
if (distance <= 100){
moveStop();
lookLeft();
lookRight();
}
else if (distance >= distanceRight ) {
turnRight();
}
else if(distance >= distanceRight){
turnLeft();
}
else{
moveForward();
}
}
int lookRight(){
servo1.write(0);
delay(500);
servo1.write(90);
delay(500);
return distance;
}
int lookLeft(){
servo1.write(140);
delay(500);
servo1.write(90);
delay(500);
return distance;
}
void moveStop(){
LeftMotor.run(RELEASE);
RightMotor.run(RELEASE);
}
void moveForward(){ //???
LeftMotor.run(FORWARD);
RightMotor.run(FORWARD);
}
void turnRight(){
LeftMotor.run(RELEASE);
RightMotor.run(FORWARD);
delay(500);
LeftMotor.run(RELEASE);
RightMotor.run(FORWARD);
}
void turnLeft(){
LeftMotor.run(FORWARD);
RightMotor.run(RELEASE);
delay(500);
LeftMotor.run(FORWARD);
RightMotor.run(RELEASE);
}
sketch_dec09b2222.ino (2.01 KB)