Robot moving in a queue fo social distancing and mask detection this code is for the straight line movement please what is wrong in the code

#include<NewPing.h>

int ENA = 8; //ENA connected to digital pin 3
int ENB = 9; //ENB connected to digital pin 9
int MOTOR_A1 = 4; // MOTOR_A1 connected to digital pin 4
int MOTOR_A2 = 5; // MOTOR_A2 connected to digital pin 5
int MOTOR_B1 = 6; // MOTOR_B1 connected to digital pin 6
int MOTOR_B2 = 7; // MOTOR_B2 connected to digital pin 7

int RIGHT = 2; // RIGHT sensor connected to analog pin A0
int LEFT = 3; // LEFT sensor connected to analog pin A0

const int trigPin = 11; // TRIG PIN connected to digital pin 11
const int echoPin = 12; // ECHO PIN connected to digital pin 12
#define MAX_DISTANCE 100 // Define Maximum Distance

NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
volatile byte leftstate=0 ;
volatile byte rightstate=0 ;
long duration;
int distance;

void setup() {
// put your setup code here, to run once:

pinMode(ENA, OUTPUT); // initialize ENA pin as an output
pinMode(ENB, OUTPUT); // initialize ENB pin as an output
pinMode(MOTOR_A1, OUTPUT); // initialize MOTOR_A1 pin as an output
pinMode(MOTOR_A2, OUTPUT); // initialize MOTOR_A2 pin as an output
pinMode(MOTOR_B1, OUTPUT); // initialize MOTOR_B1 pin as an output
pinMode(MOTOR_B2, OUTPUT); // initialize MOTOR_B2 pin as an output
pinMode(RIGHT, INPUT); // initialize RIGHT pin as an input
pinMode(LEFT, INPUT); // initialize LEFT pin as an input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
long duration;
int distance;
pinMode(2,INPUT);
pinMode(3,OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), left_ind, FALLING);
attachInterrupt(digitalPinToInterrupt(3), right_ind, FALLING);
}

void loop() {
analogWrite(ENA, 45);
analogWrite(ENB, 45);
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, HIGH);
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, LOW);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;

if(leftstate&&rightstate){
moveBackward();
delay(1000);
leftstate=0;
rightstate=0;
}
else if(!leftstate&&!rightstate){
moveForward();
delay(30);
leftstate=0;
rightstate=0;}
else if(!rightstate && leftstate){
turnRight();
delay(30);
leftstate=0;
rightstate=0;}
else if(rightstate && !leftstate){
turnLeft();
delay(30);
leftstate=0;
rightstate=0;}

// !rightstate && leftstate
//if (RIGHT==0 && LEFT==0) {
// analogWrite(ENA, 45);
// analogWrite(ENB, 45);
// digitalWrite(MOTOR_A1, LOW);
// digitalWrite(MOTOR_A2, HIGH);
// digitalWrite(MOTOR_B1, HIGH);
// digitalWrite(MOTOR_B2, LOW);
//
//}else if (RIGHT==1 && LEFT==0) {
// analogWrite(ENA, 45);
// analogWrite(ENB, 45);
// digitalWrite(MOTOR_A1, LOW);
// digitalWrite(MOTOR_A2, HIGH);
// digitalWrite(MOTOR_B1, LOW);
// digitalWrite(MOTOR_B2, HIGH);
//
//}else if (RIGHT==0 && LEFT==1) {
// analogWrite(ENA, 45);
// analogWrite(ENB, 45);
// digitalWrite(MOTOR_A1, HIGH);
// digitalWrite(MOTOR_A2, LOW);
// digitalWrite(MOTOR_B1, HIGH);
// digitalWrite(MOTOR_B2, LOW);
//}else if (RIGHT==1 && LEFT==1) {
// analogWrite(ENA, 45);
// analogWrite(ENB, 45);
// digitalWrite(MOTOR_A1, HIGH);
// digitalWrite(MOTOR_A2, LOW);
// digitalWrite(MOTOR_B1, LOW);
// digitalWrite(MOTOR_B2, HIGH);
//
//}else if (!digitalRead(distance)<=35) {
//
// Stop();
// delay(1000);
//}
//if(leftstate==1){turnLeft();
//delay(10);
//leftstate=0;}
//if(rightstate==1){turnLeft();delay(10);leftstate=0;}
//}
//
}
void Stop() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, LOW);
digitalWrite(MOTOR_B1, LOW);
digitalWrite(MOTOR_B2, LOW);
}

void turnRight() {
analogWrite(ENA, 45);
analogWrite(ENB, 45);
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, HIGH);
digitalWrite(MOTOR_B1, LOW);
digitalWrite(MOTOR_B2, HIGH);

}

void turnLeft() {
analogWrite(ENA,45);
analogWrite(ENB, 45);
digitalWrite(MOTOR_A1, HIGH);
digitalWrite(MOTOR_A2, LOW);
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, LOW);
}

void moveForward() {
analogWrite(ENA, 45);
analogWrite(ENB, 45);
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, HIGH);
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, LOW);
}
void moveBackward() {
analogWrite(ENA, 45);
analogWrite(ENB, 45);
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, LOW);
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, HIGH);
}

void left_ind() {
leftstate=1;
}
void right_ind() {
rightstate=1;
}

What is wrong ?
Firstly it is not posted here in code tags as described in How to get the best out of this forum

Secondly, you do not describe what it should do or what it actually does

That's not great

Hi, @timothyadekola001
Welcome to the forum.

What is your code doing?
Have you written your code in stages?
Have you got code that JUST runs the motors, to prove that they work properly?
Have you got code the JUST reads the Ultra Sonic and proves that your have good consistent readings?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

The code is meant to move the robot in a straight line it uses Ir sensors detect the black line

and . . .?

The code is meant to move the robot in a straight line it uses Ir sensors to detect the black line if there is an obstacle the robot should stop for 10sec then continue on the line but the robot should not leave the line

And...?

Hi,

What does it do?

Please answer post #4.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

The code never call the Stop() function

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.