I am working on a robot car project which will calculate distance and move to the longest path every 1 second and some limit switches were used to prevent robot from getting stuck. The function of limit switch in this project is when the limit switch is triggered it will stop and move in opposite direction. The codes that I written is workable but there is a slight error in it. While moving forward/backward the limit switch were triggered suddenly and the robot was unable to run the function for the limit switch, the motors will continue to rotate until the loop is finished then only it will run the function of the limit switch. Any solution for this? Thanks.
Below shows the written codes:
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 250
#define MAX_SPEED 150 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
Servo myservo;
int speedSet = 0;//
int distance = 100;
int del = 0;
byte switch1 = A4, switch2 = A3;
void setup() {
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);
myservo.attach(10);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceR = 0;
int distanceL = 0;
int distance = readPing();
delay(50);
myservo.write(90);
delay(200);
if (digitalRead(switch1) == LOW) {//Front limit switch
moveStop();
moveBackward();
delay(200);
moveStop();
delay(200);
}
else if (digitalRead(switch2) == LOW) {//Rear limit switch
moveStop();
moveForward();
delay(100);
moveStop();
delay(200);
}
else{
if (distance > 20)//different delay were used to prevent collision
{
if (distance >= 150)
{
del = 900;
}
else if (distance < 150 && distance >= 80)
{
del = 600;
}
else if (distance < 80 && distance >= 40)
{
del = 300;
}
else if (distance < 40)
{
del = 150;
}
moveForward();
delay(del);
moveStop();
delay(200);
distance = lookFront();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if (distance >= distanceR)//Compare for the longest distance
{
if (distance >= distanceL)
{
moveStop();
delay(100);
}
else
{
turnLeft();
moveStop();
delay(100);
}
}
else if (distanceR > distance)
{
if (distanceR >= distanceL)
{
turnRight();
moveStop();
delay(100);
}
else
{
turnLeft();
moveStop();
delay(100);
}
}
}
else {//obstcale in front lesser than 20cm
moveStop();
delay(100);
moveBackward();
delay(200);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if (distanceL <= 20 && distanceR <= 20 )//compare left and right
{
turnAround();//turn 180 degree when left and right were lesser than 20cm
moveStop();
delay(100);
}
else if (distanceL > distanceR)
{
turnLeft();
moveStop();
delay(100);
}
else if (distanceR > distanceL)
{
turnRight();
moveStop();
delay(100);
}
}
}
}
int lookFront()
{
int distance = readPing();
delay(100);
return distance;
delay(100);
}
int lookRight()
{
myservo.write(10);
delay(500);
int distance = readPing();
delay(100);
myservo.write(90);
return distance;
delay(100);
}
int lookLeft()
{
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(90);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0)
{
cm = 250;
}
return cm;
}
void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void moveForward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
void moveBackward() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
void turnRight() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(325);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(100);
}
void turnLeft() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(325);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(100);
}
void turnAround() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(650);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}