AWOL:
What does that mean?Please remember to use code tags when posting code.
I want to write a code with Ardiuno Uno.I had changed the code which i wrote before.There is a problem.The problem is that if robot's motor catch a obstacle,motors always will run because sensor is not see the any obstacles which is behind the sensor.But i did not want this.I want to all motors stop and run backward until 1 second.What do i do?
Now,i have tried to work following code:
#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
#define TRIG_PIN A4
#define ECHO_PIN A5
#define MAX_SPEED 180
#define COLL_DIST 40
#define TURN_DIST COLL_DIST +20
NewPing sonar(TRIG_PIN, ECHO_PIN);
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(4, MOTOR12_1KHZ);
Servo servo;
const int led = 15;
int leftDistance, rightDistance;
int curDist;
int speedSet = 0;
void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);
servo.attach(9);
servo.write(90);
delay(500);
}
void loop() {
Serial.println(readPing());
digitalWrite(led,HIGH);
Serial.println("START FORWARD");
accelerateForward();
if (readPing()<COLL_DIST){
Serial.println("Collision Detected");
moveStop();
changePath();
compareDistance();
accelerateForward();
}
}
void changePath() {
servo.write(20);
delay(600);
rightDistance = readPing();
delay(600);
servo.write(90);
delay(600);
servo.write(160);
delay(600);
leftDistance = readPing();
delay(600);
servo.write(90);
delay(600);
}
void compareDistance()
{
if (leftDistance>rightDistance)
{
turnLeft();
}
else if (rightDistance>leftDistance)
{
turnRight();
}
else
{
turnAround();
}
}
int readPing() {
delay(70);
unsigned int uS = sonar.ping();
int cm = uS/US_ROUNDTRIP_CM;
Serial.print(cm);
Serial.println(" cm");
return cm;
}
void moveStop() {
Serial.println("Stopped");
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(800);
}
void accelerateForward() {
Serial.println("Start Forward");
motor1.run(FORWARD);
motor2.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
}
}
void turnRight() {
Serial.println("Turn Right");
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(500);
}
void turnLeft() {
Serial.println("Turn Left");
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(500);
}
void turnAround() {
Serial.println("Turn Around");
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(1000);
}