Multifunction robot not working

Anyone please help me, I'm doing a project on a multifunction robot, but it's not working. I connected these components with the connections I mentioned and also provided the code. Does it have any mistakes in the code or connections?

Components :
L298N Dual H Bridge DC/Stepper Motor Driver Controller Module
Arduino UNO
Arduino sensor shield v5
Servo motor
Ultrasonic module
Three channels tracking module
Infrared receive sensor

Connections done:
L298N Motor Driver Module:
MotorRight1 (IN1): Pin 5
MotorRight2 (IN2): Pin 6
MotorLeft1 (IN3): Pin 10
MotorLeft2 (IN4): Pin 11

Three channels tracking module:
Left sensor input: Pin 7 (SensorLeft)
Middle sensor input: Pin 4 (SensorMiddle)
Right sensor input: Pin 3 (SensorRight)

Infrared receive sensor:
Connected to Pin 2 (irReceiverPin)

Servo motor:
Control signal connected to Pin 9

Ultrasonic module
ultrasonic receive pin: 13
ultrasonic echo pin: 12
Code:

#include <IRremote.h>
#include <Servo.h>
//***********************define motor pin*************************
int MotorRight1=5;
int MotorRight2=6;
int MotorLeft1=10;
int MotorLeft2=11;
int counter=0;
const int irReceiverPin = 2; //Infrared receive sensor
char val;
//***********************set detection IRcode*************************
long IRfront= 0x00FFA25D; //go straight
long IRback=0x00FF629D; //go back
long IRturnright=0x00FFC23D; //turn right
long IRturnleft= 0x00FF02FD; //turn left
long IRstop=0x00FFE21D; //stop
long IRcny70=0x00FFA857; //CNY70 automatic mode
long IRAutorun=0x00FF906F; //Ultrasonic automatic mode
long IRturnsmallleft= 0x00FF22DD;
//*************************define CNY70 pin***************************
const int SensorLeft = 7; //left sensor input
const int SensorMiddle= 4 ; //middle sensor input
const int SensorRight = 3; //right sensor input
int SL; //left sensor state
int SM; //middle sensor state
int SR; //right sensor state
IRrecv irrecv(irReceiverPin); // define IRrecv receive signal
decode_results results; // decoderesults
//*************************define ultrasonic pin****************
int inputPin =13 ; // ultrasonic receive pin
int outputPin =12; //ultrasonic echo pin
int Fspeedd = 0; // front distance
int Rspeedd = 0; // right distance
int Lspeedd = 0; // left distance
int directionn = 0; // front=8; back=2; left=4; right=6
Servo myservo; // define myservo
int delay_time = 250; // servo motor go back state time
int Fgo = 8; // go straight
int Rgo = 6; // turn right
int Lgo = 4; // turn left
int Bgo = 2; // go back
//*********************************(SETUP)
void setup()
{
 Serial.begin(9600);
 pinMode(MotorRight1, OUTPUT); // pin 8 (PWM)
 pinMode(MotorRight2, OUTPUT); //pin 9 (PWM)
 pinMode(MotorLeft1, OUTPUT); // pin 10 (PWM)
 pinMode(MotorLeft2, OUTPUT); // pin 11 (PWM)
 irrecv.enableIRIn(); // start infrared decode
 pinMode(SensorLeft, INPUT); //
 pinMode(SensorMiddle, INPUT);//
 pinMode(SensorRight, INPUT); //
 digitalWrite(2,HIGH);
 pinMode(inputPin, INPUT); //
 pinMode(outputPin, OUTPUT); //
 myservo.attach(9); //
 }
//*************************************************(Void)
void advance(int a) // go straight
{
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 delay(a * 100);
}
void right(int b) //turn right(single wheel)
{
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 delay(b * 100);
}
void left(int c) //turn left(single wheel)
{
  digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 delay(c * 100);
}
void turnR(int d) //turn right(two wheels)
{
 digitalWrite(MotorRight1,HIGH);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 delay(d * 100);
}
void turnL(int e) //turn left(two wheels)
{
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,HIGH);
 digitalWrite(MotorLeft2,LOW);
 delay(e * 100);
}
void stopp(int f) //stop
{
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 delay(f * 100);
}
void back(int g) //go back
{
 digitalWrite(MotorRight1,HIGH);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,HIGH);
 digitalWrite(MotorLeft2,LOW);;
 delay(g * 100);
}
void detection() //measurement 3 angle(front ,left,right)
{
 int delay_time = 250; //
 ask_pin_F(); // detection distance in front
 if(Fspeedd < 10) // if distance less than 10mm
 {
 stopp(1); // clear output
 back(2); // go back 0.2s
 }
 if(Fspeedd < 25) // if distance less than 25mm
 {
 stopp(1); // clear output
 ask_pin_L(); // detection distance in left
 delay(delay_time); // Waiting for the servo motor is stable
 ask_pin_R(); // detection distance in right
 delay(delay_time); // waiting for servo motor state
 if(Lspeedd > Rspeedd) //if left distance greater than right
 {
 directionn = Lgo; //go left
 }
 if(Lspeedd <= Rspeedd) //if left distance less than right
 {
 directionn = Rgo; //go right
 }
 if (Lspeedd < 15 && Rspeedd < 15) //if distance less 10mm both right and left
 {
 directionn = Bgo; //go back
 }
 }
 else //if distance greater than 25mm
 {
 directionn = Fgo; //go straight
 }
}
//*********************************************
void ask_pin_F() // detection distance in front
{
myservo.write(90);
digitalWrite(outputPin, LOW); // ultrasonic echo low level in 2us
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic echo high level in 10us, At least 10us
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // ultrasonic echo low leve
float Fdistance = pulseIn(inputPin, HIGH); // read time
Fdistance= Fdistance/5.8/10; // turn time to distance
Serial.print("F distance:"); //output distance (mm)
Serial.println(Fdistance); //display distance
Fspeedd = Fdistance; // write distance to Fspeed
}
//******************************************************
void ask_pin_L() // detection distance in left
{
myservo.write(177);
delay(delay_time);
digitalWrite(outputPin, LOW); // ultrasonic echo low level in 2us
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic echo high level in 10us, At least 10us
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // ultrasonic echo low level
float Ldistance = pulseIn(inputPin, HIGH); // read time
Ldistance= Ldistance/5.8/10; // turn time to distance
Serial.print("L distance:"); //output distance (mm)
Serial.println(Ldistance); //display distance
Lspeedd = Ldistance; // write distance to Lspeed
}
//*****************************************************************************
void ask_pin_R() // detection distance in right
{
myservo.write(5);
delay(delay_time);
digitalWrite(outputPin, LOW); //
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); //
delayMicroseconds(10);
digitalWrite(outputPin, LOW); //
float Rdistance = pulseIn(inputPin, HIGH); //
Rdistance= Rdistance/5.8/10; //
Serial.print("R distance:"); //
Serial.println(Rdistance); //
Rspeedd = Rdistance; //
}
//***********************************(LOOP)
void loop()
{
 SL = digitalRead(SensorLeft);
 SM = digitalRead(SensorMiddle);
 SR = digitalRead(SensorRight);
 performCommand();
//******************************normal remote control mode
if (irrecv.decode(&results))
 { // Decoding success,receive infrared signal
/*********************************************/
 if (results.value == IRfront)//go straight
 {
 advance(10);//go straight
 }
/***************************************/
 if (results.value == IRback)//go back
 {
 back(10);//go back
 }
/***********************************************************************/
 if (results.value == IRturnright)//turn right
 {
 right(6); // turn right
 }
/***********************************************************************/
 if (results.value == IRturnleft)//turn left
 {
 left(6); // turn left;
 }
/***********************************************************************/
 if (results.value == IRstop)//stop
{
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 }
//*************************************cny70 automatic mode
 if (results.value == IRcny70)
 {
 while(IRcny70)
 {
 SL = digitalRead(SensorLeft);
 SM = digitalRead(SensorMiddle);
 SR = digitalRead(SensorRight);
 if (SM == HIGH)//middle sensor in black area
 {
 if (SL == LOW & SR == HIGH) // left sensor in black area,right sensor in whitearea ,so turn left
 {
   digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 analogWrite(MotorLeft1,0);
 analogWrite(MotorLeft2,80);
 }
 else if (SR == LOW & SL == HIGH) //left white,right black ,turn right
 {
 analogWrite(MotorRight1,0);//
 analogWrite(MotorRight2,80);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 }
 else // left and right both in white ,go straight
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 analogWrite(MotorLeft1,200);
 analogWrite(MotorLeft2,200);
 analogWrite(MotorRight1,200);
 analogWrite(MotorRight2,200);
 }
 }
 else // middle sensor in white area
 {
 if (SL == LOW & SR == HIGH)// left black,right white,turn left
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 }
 else if (SR == LOW & SL == HIGH) // left white,right black ,turn right
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 }
 else // left and right both in white ,stop
 {
 digitalWrite(MotorRight1,HIGH);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,HIGH);
 digitalWrite(MotorLeft2,LOW);;
 }
 }
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,HIGH);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,HIGH);
 digitalWrite(MotorLeft2,HIGH);
 break;
 }
 }
 }
 results.value=0;
 }
//********************************ultracsonic automaitc mode
 if (results.value ==IRAutorun )
 {
 while(IRAutorun)
 {
 myservo.write(90); //make the servo motor reset
 detection(); //
 if(directionn == 8) //directionn = 8(go straight)
 {
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 results.value=0;
 advance(1); //
 Serial.print(" Advance "); //
 Serial.print(" ");
 }
 if(directionn == 2) //2(go back)
 {
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 results.value=0;
 back(8); //
 turnL(3); //To prevent the jammed
 Serial.print(" Reverse "); //
 }
 if(directionn == 6) // 6(turn right)
 {
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 results.value=0;
 back(1);
 turnR(6); //
 Serial.print(" Right "); //
 }
 if(directionn == 4) // 4(turn left)
 {
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 results.value=0;
 back(1);
 turnL(6); //
 Serial.print(" Left "); //
 }
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 }
 results.value=0;
 }
/***********************************************************************/
 else
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 }
 irrecv.resume(); //
 }
}
void performCommand() {
 if (Serial.available()) {
 val = Serial.read();
 }
 if (val == 'f') { // Forward
 advance(10);
 } else if (val == 'z') { // Stop Forward
 stopp(10) ;
 } else if (val == 'b') { // Backward
 back(10);
 } else if (val == 'y') { // Stop Backward
 back(10);
 } else if (val == 'l') { // Right
 turnR(10);
 } else if (val == 'r') { // Left
 turnL(10);
 } else if (val == 'v') { // Stop Turn
 stopp(10) ;
 } else if (val == 's') { // Stop
 stopp(10) ;
 }
}
1 Like

Probably the most annoying, least useful thing you could write.

1 Like

Please also include a schematic for clarity, can be pen and paper.

As you assemble your robot, test each device. For example, with just the Uno, run the IDE built-in sketch called blink.ino. Do the same (test with an example sketch) for each part you install. You will either have a successful build, or find where the problem exists. You should post your progress here, so that we can see your progress, or catch where the problem is in its earliest stage. Have fun!

How did you made your test until now?

I tested the motor driver with code, and it worked perfectly, so the motor driver and Uno are working.

Excellent. Keep doing that with your other devices.
[edit] Is this your line tracker?
https://wiki.keyestudio.com/KS0453_Keyestudio_3-channel_Line_Tracking_Module

Hi, @santoshps
Welcome to the forum.
Thanks for using code tags. :+1:

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.
Please no cut and paste images or Fritzy "diagrams"

What is your power supply?

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

No, I am following the instructions in this PDF and doing the last multifunction robot project.https://robu.in/wp-content/uploads/2017/06/31454-Turtle-kIt.pdf

The power supply I'm using is 2 batteries of 3.7V and 2400 mAh. I don't have a circuit diagram, but I am following the instructions in this PDF and doing the last multifunction robot project
.https://robu.in/wp-content/uploads/2017/06/31454-Turtle-kIt.pdf

That link does not supply a schematic, just assembly drawings.

You will need to reverse engineer your project and produce a schematic, specifically showing how you have connected your powersupplies.

Some images of your project will also help.

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

This is the connection

Post a real connection and wiring diagram.

Noone will able to follow up your spaghetti drawings.

Arduino sensor shield is not available in circuit simulators or schematic-making software, which is why I do so.

It is just a connection shield no components, a junction box.

The shield has UNO pin numbers printed on it.

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

Yes right

Sir,this is the connections

I'm doing a project on a remote control multifunction robot, but it's not working after wiring and coding done. I connected these components with the wiring I posted and also provided the code. Please help me fix any mistakes in the code or wiring.

Components :
L298N Dual H Bridge DC Motor Driver Module
Arduino UNO
Arduino sensor shield v5
Servo motor
Ultrasonic module
Three channels tracking module
Infrared receive sensor

Connections done:
L298N Motor Driver Module:
MotorRight1 (IN1): Pin 5
MotorRight2 (IN2): Pin 6
MotorLeft1 (IN3): Pin 10
MotorLeft2 (IN4): Pin 11

Three channels tracking module:
Left sensor input: Pin 7 (SensorLeft)
Middle sensor input: Pin 4 (SensorMiddle)
Right sensor input: Pin 3 (SensorRight)

Infrared receive sensor:
Connected to Pin 2 (irReceiverPin)

Servo motor:
Control signal connected to Pin 9

Ultrasonic module
ultrasonic receive pin: 13
ultrasonic echo pin: 12

Code:

//******************************
#include <IRremote.h>
#include <Servo.h>
//***********************define motor pin*************************
int MotorRight1=5;
int MotorRight2=6;
int MotorLeft1=10;
int MotorLeft2=11;
int counter=0;
const int irReceiverPin = 2; //Infrared receive sensor
char val;
//***********************set detection IRcode*************************
long IRfront= 0x00FFA25D; //go straight
long IRback=0x00FF629D; //go back
long IRturnright=0x00FFC23D; //turn right
long IRturnleft= 0x00FF02FD; //turn left
long IRstop=0x00FFE21D; //stop
long IRcny70=0x00FFA857; //CNY70 automatic mode
long IRAutorun=0x00FF906F; //Ultrasonic automatic mode
long IRturnsmallleft= 0x00FF22DD;
//*************************define CNY70 pin***************************
const int SensorLeft = 7; //left sensor input
const int SensorMiddle= 4 ; //middle sensor input
const int SensorRight = 3; //right sensor input
int SL; //left sensor state
int SM; //middle sensor state
int SR; //right sensor state
IRrecv irrecv(irReceiverPin); // define IRrecv receive signal
decode_results results; // decoderesults
//*************************define ultrasonic pin****************
int inputPin =13 ; // ultrasonic receive pin
int outputPin =12; //ultrasonic echo pin
int Fspeedd = 0; // front distance
int Rspeedd = 0; // right distance
int Lspeedd = 0; // left distance
int directionn = 0; // front=8; back=2; left=4; right=6
Servo myservo; // define myservo
int delay_time = 250; // servo motor go back state time
int Fgo = 8; // go straight
int Rgo = 6; // turn right
int Lgo = 4; // turn left
int Bgo = 2; // go back
//*********************************(SETUP)
void setup()
{
 Serial.begin(9600);
 pinMode(MotorRight1, OUTPUT); // pin 8 (PWM)
 pinMode(MotorRight2, OUTPUT); //pin 9 (PWM)
 pinMode(MotorLeft1, OUTPUT); // pin 10 (PWM)
 pinMode(MotorLeft2, OUTPUT); // pin 11 (PWM)
 irrecv.enableIRIn(); // start infrared decode
 pinMode(SensorLeft, INPUT); //
 pinMode(SensorMiddle, INPUT);//
 pinMode(SensorRight, INPUT); //
 digitalWrite(2,HIGH);
 pinMode(inputPin, INPUT); //
 pinMode(outputPin, OUTPUT); //
 myservo.attach(9); //
 }
//*************************************************(Void)
void advance(int a) // go straight
{
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 delay(a * 100);
}
void right(int b) //turn right(single wheel)
{
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 delay(b * 100);
}
void left(int c) //turn left(single wheel)
{
  digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 delay(c * 100);
}
void turnR(int d) //turn right(two wheels)
{
 digitalWrite(MotorRight1,HIGH);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 delay(d * 100);
}
void turnL(int e) //turn left(two wheels)
{
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,HIGH);
 digitalWrite(MotorLeft2,LOW);
 delay(e * 100);
}
void stopp(int f) //stop
{
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 delay(f * 100);
}
void back(int g) //go back
{
 digitalWrite(MotorRight1,HIGH);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,HIGH);
 digitalWrite(MotorLeft2,LOW);;
 delay(g * 100);
}
void detection() //measurement 3 angle(front ,left,right)
{
 int delay_time = 250; //
 ask_pin_F(); // detection distance in front
 if(Fspeedd < 10) // if distance less than 10mm
 {
 stopp(1); // clear output
 back(2); // go back 0.2s
 }
 if(Fspeedd < 25) // if distance less than 25mm
 {
 stopp(1); // clear output
 ask_pin_L(); // detection distance in left
 delay(delay_time); // Waiting for the servo motor is stable
 ask_pin_R(); // detection distance in right
 delay(delay_time); // waiting for servo motor state
 if(Lspeedd > Rspeedd) //if left distance greater than right
 {
 directionn = Lgo; //go left
 }
 if(Lspeedd <= Rspeedd) //if left distance less than right
 {
 directionn = Rgo; //go right
 }
 if (Lspeedd < 15 && Rspeedd < 15) //if distance less 10mm both right and left
 {
 directionn = Bgo; //go back
 }
 }
 else //if distance greater than 25mm
 {
 directionn = Fgo; //go straight
 }
}
//*********************************************
void ask_pin_F() // detection distance in front
{
myservo.write(90);
digitalWrite(outputPin, LOW); // ultrasonic echo low level in 2us
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic echo high level in 10us, At least 10us
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // ultrasonic echo low leve
float Fdistance = pulseIn(inputPin, HIGH); // read time
Fdistance= Fdistance/5.8/10; // turn time to distance
Serial.print("F distance:"); //output distance (mm)
Serial.println(Fdistance); //display distance
Fspeedd = Fdistance; // write distance to Fspeed
}
//******************************************************
void ask_pin_L() // detection distance in left
{
myservo.write(177);
delay(delay_time);
digitalWrite(outputPin, LOW); // ultrasonic echo low level in 2us
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); // ultrasonic echo high level in 10us, At least 10us
delayMicroseconds(10);
digitalWrite(outputPin, LOW); // ultrasonic echo low level
float Ldistance = pulseIn(inputPin, HIGH); // read time
Ldistance= Ldistance/5.8/10; // turn time to distance
Serial.print("L distance:"); //output distance (mm)
Serial.println(Ldistance); //display distance
Lspeedd = Ldistance; // write distance to Lspeed
}
//*****************************************************************************
void ask_pin_R() // detection distance in right
{
myservo.write(5);
delay(delay_time);
digitalWrite(outputPin, LOW); //
delayMicroseconds(2);
digitalWrite(outputPin, HIGH); //
delayMicroseconds(10);
digitalWrite(outputPin, LOW); //
float Rdistance = pulseIn(inputPin, HIGH); //
Rdistance= Rdistance/5.8/10; //
Serial.print("R distance:"); //
Serial.println(Rdistance); //
Rspeedd = Rdistance; //
}
//***********************************(LOOP)
void loop()
{
 SL = digitalRead(SensorLeft);
 SM = digitalRead(SensorMiddle);
 SR = digitalRead(SensorRight);
 performCommand();
//******************************normal remote control mode
if (irrecv.decode(&results))
 { // Decoding success,receive infrared signal
/*********************************************/
 if (results.value == IRfront)//go straight
 {
 advance(10);//go straight
 }
/***************************************/
 if (results.value == IRback)//go back
 {
 back(10);//go back
 }
/***********************************************************************/
 if (results.value == IRturnright)//turn right
 {
 right(6); // turn right
 }
/***********************************************************************/
 if (results.value == IRturnleft)//turn left
 {
 left(6); // turn left;
 }
/***********************************************************************/
 if (results.value == IRstop)//stop
{
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 }
//*************************************cny70 automatic mode
 if (results.value == IRcny70)
 {
 while(IRcny70)
 {
 SL = digitalRead(SensorLeft);
 SM = digitalRead(SensorMiddle);
 SR = digitalRead(SensorRight);
 if (SM == HIGH)//middle sensor in black area
 {
 if (SL == LOW & SR == HIGH) // left sensor in black area,right sensor in whitearea ,so turn left
 {
   digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 analogWrite(MotorLeft1,0);
 analogWrite(MotorLeft2,80);
 }
 else if (SR == LOW & SL == HIGH) //left white,right black ,turn right
 {
 analogWrite(MotorRight1,0);//
 analogWrite(MotorRight2,80);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 }
 else // left and right both in white ,go straight
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 analogWrite(MotorLeft1,200);
 analogWrite(MotorLeft2,200);
 analogWrite(MotorRight1,200);
 analogWrite(MotorRight2,200);
 }
 }
 else // middle sensor in white area
 {
 if (SL == LOW & SR == HIGH)// left black,right white,turn left
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 }
 else if (SR == LOW & SL == HIGH) // left white,right black ,turn right
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,HIGH);
 }
 else // left and right both in white ,stop
 {
 digitalWrite(MotorRight1,HIGH);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,HIGH);
 digitalWrite(MotorLeft2,LOW);;
 }
 }
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,HIGH);
 digitalWrite(MotorRight2,HIGH);
 digitalWrite(MotorLeft1,HIGH);
 digitalWrite(MotorLeft2,HIGH);
 break;
 }
 }
 }
 results.value=0;
 }
//********************************ultracsonic automaitc mode
 if (results.value ==IRAutorun )
 {
 while(IRAutorun)
 {
 myservo.write(90); //make the servo motor reset
 detection(); //
 if(directionn == 8) //directionn = 8(go straight)
 {
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 results.value=0;
 advance(1); //
 Serial.print(" Advance "); //
 Serial.print(" ");
 }
 if(directionn == 2) //2(go back)
 {
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 results.value=0;
 back(8); //
 turnL(3); //To prevent the jammed
 Serial.print(" Reverse "); //
 }
 if(directionn == 6) // 6(turn right)
 {
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 results.value=0;
 back(1);
 turnR(6); //
 Serial.print(" Right "); //
 }
 if(directionn == 4) // 4(turn left)
 {
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 results.value=0;
 back(1);
 turnL(6); //
 Serial.print(" Left "); //
 }
 if (irrecv.decode(&results))
 {
 irrecv.resume();
 Serial.println(results.value,HEX);
 if(results.value ==IRstop)
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 break;
 }
 }
 }
 results.value=0;
 }
/***********************************************************************/
 else
 {
 digitalWrite(MotorRight1,LOW);
 digitalWrite(MotorRight2,LOW);
 digitalWrite(MotorLeft1,LOW);
 digitalWrite(MotorLeft2,LOW);
 }
 irrecv.resume(); //
 }
}
void performCommand() {
 if (Serial.available()) {
 val = Serial.read();
 }
 if (val == 'f') { // Forward
 advance(10);
 } else if (val == 'z') { // Stop Forward
 stopp(10) ;
 } else if (val == 'b') { // Backward
 back(10);
 } else if (val == 'y') { // Stop Backward
 back(10);
 } else if (val == 'l') { // Right
 turnR(10);
 } else if (val == 'r') { // Left
 turnL(10);
 } else if (val == 'v') { // Stop Turn
 stopp(10) ;
 } else if (val == 's') { // Stop
 stopp(10) ;
 }
}

1 Like