For all those who want to try this as it is with the same components, try this code..... /*Pin usage with the Motorshield --------------------------------------- Analog pins: not used at all... A0 ... A5 are still available... They all can also be used as digital pins. Also, I2C (A4 = SDA and A5 = SCL) can be used. These pins have a breadboard area on the shield. Digital pins: used: 3,4,5,6,7,8,9,10,11,12 Pin 9 and 10 are only used for the servo motors. Already in use: 0 (RX) and 1 (TX). Unused: 2,13 Pin 2 has an soldering hole on the board, easy to connect a wire. Pin 13 is also connected to the system led. I2C is possible, but SPI is not possible since those pins are used. */ #include AF_DCMotor m1(1); AF_DCMotor m2(2); String path; int mode = 0; void setup() { // put your setup code here, to run once: Serial.begin(9600); for (int i = 14; i < 20; i++) { pinMode(i, INPUT); } pinMode(2, INPUT); m1.setSpeed(255); m2.setSpeed(255); } void loop() { // mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 if(digitalRead(2) == 0) { for (int i = 14; i < 20; i++) { Serial.print("pin"); Serial.print("i"); Serial.print(" ** "); Serial.println(digitalRead(i)); } // SENSOR POSITION // 16 //14 15 18 19 // 17 if (!digitalRead(14) && !digitalRead(15) && digitalRead(16) && digitalRead(17) && !digitalRead(18) && !digitalRead(19)) { // Serial.println("move forward "); path += 'S'; front(); } //Reverse condition if (!digitalRead(15) && !digitalRead(17) && !digitalRead(18)) { m1.run(FORWARD); m2.run(FORWARD); delay(1000); if (digitalRead(15) && !digitalRead(17) && digitalRead(18)) { path += 'B'; back(); } else { m1.run(BACKWARD); m2.run(BACKWARD); delay(1000); } } // LEFT/LEFT-T CONDITION if (digitalRead(14) && digitalRead(15) && digitalRead(16) && digitalRead(17)) { if (path[0] == 'R') { right(); } if (path[0] == 'L') { left(); } if (path[0] == 'S') { front(); } Serial.println("Check LEFT I &/ Move LEFT"); Serial.println("Move LEFT"); path += 'L'; left(); } //RIGHT/RIGHT-T if (digitalRead(16) && digitalRead(17) && digitalRead(18) && digitalRead(19)) { Serial.println("Check RIGHT T &/ Move RIGHT"); path += 'R'; right(); } //Cross Condition if (digitalRead(14) && digitalRead(15) && digitalRead(16) && digitalRead(17) && digitalRead(18) && digitalRead(19)) { Serial.println(""); left(); } //Move slight left condition if (digitalRead(14) || digitalRead(15)) { Serial.println("move slight left"); left(); } //Move slight Right Condition if (digitalRead(18) || digitalRead(19)) { Serial.println("move slight right"); right(); } // mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 if(digitalRead(2) == 1) { ShortPath(); go(); } } } String ShortPath() { String path = "LBLLLBSBLLBSLL"; path.replace("LBL", "S"); path.replace("LBS", "R"); path.replace("RBL", "B"); path.replace("SBS", "B"); path.replace("SBL", "R"); path.replace("LBR", "B"); return path; } void go() { unsigned int i=0; // Follow the outpath if (!digitalRead(14) && !digitalRead(15) && digitalRead(16) && digitalRead(17) && !digitalRead(18) && !digitalRead(19)) { if (path.charAt(i)== 'L') { // Update the PATH only if there is a change // take left turn left(); i++; } if (path.charAt(i)== 'R') { // Update the PATH only if there is a change // take right turn right(); i++; } if (path.charAt(i)== 'S') { // Update the PATH only if there is a change // move straight // moveForward(); front(); i++; } } if (!digitalRead(14) && !digitalRead(15) && digitalRead(16) && digitalRead(17) && !digitalRead(18) && !digitalRead(19)) { // Serial.println("move forward "); front(); } if (!digitalRead(14) && !digitalRead(15) && !digitalRead(16) && !digitalRead(17) && !digitalRead(18) && !digitalRead(19)) { // Serial.println("move forward "); halt(); } } void front() { m1.run(FORWARD); m2.run(FORWARD); delay(100); m1.run(RELEASE); m2.run(RELEASE); delay(1); } void back() { m1.run(BACKWARD); m2.run(BACKWARD); delay(100); m1.run(RELEASE); m2.run(RELEASE); delay(1); } void right() { m1.run(FORWARD); m2.run(BACKWARD); delay(100); m1.run(RELEASE); m2.run(RELEASE); delay(1); } void left() { m1.run(BACKWARD); m2.run(FORWARD); delay(100); m1.run(RELEASE); m2.run(RELEASE); delay(1); } void halt() { m1.run(RELEASE); m2.run(RELEASE); delay(10); }
sir can you please provide the final code for shortest path maze solver robot for my college techenical fest as i am not able to code it. very much neened sir.
The program remembers the path, basically it records the path in the first iteration based on the sensor readings. There is a need to tweak sensor placement on the robot for optimum path recording with multiple trial-error experiments to cover all types of moves. The rest of the headache is handled by the code in optimizing the path and reaching the goal in the second iteration.
Yes, why not!..... as long as you follow the basics such as Motor speed, sensor placement, motor driver and proper pin mapping in the code. All the best.....✨
You must use the following official library and refer their documentation www.arduino.cc/reference/en/libraries/adafruit-motor-shield-library/. Once you install you may then use the default examples provided e.g. MotorTest to modify according to your needs. Let me know how you go with this.....
Sir... Can you share me the code or Please provide me the link of same code... I need it for my college project... I'm waiting for your reply... Thank you sir..!
Depends on what your build and application is. If you prefer using the AFmotor library then use their documentation. On the other hand if you prefer using no library then simply modify( which means write your own I/O operations on digital pins to get the job done) as per your requirements. If you prefer using the same build and components then you may utilize the code pinned under the video. Alternatively, you may also write down a pseudocode for your application and later decide which approach fits best for achieving goals.
Simply you need to invert all conditional values under "if" conditions. e.g. if it was if (a==1), make it if (a==). Additionally, this process is applicable when you are switching from Active-High to Active-Low sensors or vice-versa.
For all those who want to try this as it is with the same components, try this code.....
/*Pin usage with the Motorshield
---------------------------------------
Analog pins: not used at all...
A0 ... A5 are still available...
They all can also be used as digital pins.
Also, I2C (A4 = SDA and A5 = SCL) can be used.
These pins have a breadboard area on the shield.
Digital pins: used: 3,4,5,6,7,8,9,10,11,12
Pin 9 and 10 are only used for the servo motors.
Already in use: 0 (RX) and 1 (TX).
Unused: 2,13
Pin 2 has an soldering hole on the board, easy to connect a wire.
Pin 13 is also connected to the system led.
I2C is possible, but SPI is not possible since those pins are used.
*/
#include
AF_DCMotor m1(1);
AF_DCMotor m2(2);
String path;
int mode = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for (int i = 14; i < 20; i++) {
pinMode(i, INPUT);
}
pinMode(2, INPUT);
m1.setSpeed(255);
m2.setSpeed(255);
}
void loop() {
// mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0 //mode-0
if(digitalRead(2) == 0)
{
for (int i = 14; i < 20; i++) {
Serial.print("pin");
Serial.print("i");
Serial.print(" ** ");
Serial.println(digitalRead(i));
}
// SENSOR POSITION
// 16
//14 15 18 19
// 17
if (!digitalRead(14) && !digitalRead(15) && digitalRead(16) && digitalRead(17) && !digitalRead(18) && !digitalRead(19)) {
// Serial.println("move forward ");
path += 'S';
front();
}
//Reverse condition
if (!digitalRead(15) && !digitalRead(17) && !digitalRead(18)) {
m1.run(FORWARD);
m2.run(FORWARD);
delay(1000);
if (digitalRead(15) && !digitalRead(17) && digitalRead(18)) {
path += 'B';
back();
} else {
m1.run(BACKWARD);
m2.run(BACKWARD);
delay(1000);
}
}
// LEFT/LEFT-T CONDITION
if (digitalRead(14) && digitalRead(15) && digitalRead(16) && digitalRead(17)) {
if (path[0] == 'R') { right(); }
if (path[0] == 'L') { left(); }
if (path[0] == 'S') { front(); }
Serial.println("Check LEFT I &/ Move LEFT");
Serial.println("Move LEFT");
path += 'L';
left();
}
//RIGHT/RIGHT-T
if (digitalRead(16) && digitalRead(17) && digitalRead(18) && digitalRead(19)) {
Serial.println("Check RIGHT T &/ Move RIGHT");
path += 'R';
right();
}
//Cross Condition
if (digitalRead(14) && digitalRead(15) && digitalRead(16) && digitalRead(17) && digitalRead(18) && digitalRead(19)) {
Serial.println("");
left();
}
//Move slight left condition
if (digitalRead(14) || digitalRead(15)) {
Serial.println("move slight left");
left();
}
//Move slight Right Condition
if (digitalRead(18) || digitalRead(19)) {
Serial.println("move slight right");
right();
}
// mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1 //mode-1
if(digitalRead(2) == 1)
{
ShortPath();
go();
}
}
}
String ShortPath() {
String path = "LBLLLBSBLLBSLL";
path.replace("LBL", "S");
path.replace("LBS", "R");
path.replace("RBL", "B");
path.replace("SBS", "B");
path.replace("SBL", "R");
path.replace("LBR", "B");
return path;
}
void go()
{
unsigned int i=0;
// Follow the outpath
if (!digitalRead(14) && !digitalRead(15) && digitalRead(16) && digitalRead(17) && !digitalRead(18) && !digitalRead(19)) {
if (path.charAt(i)== 'L') {
// Update the PATH only if there is a change
// take left turn
left();
i++;
}
if (path.charAt(i)== 'R') {
// Update the PATH only if there is a change
// take right turn
right();
i++;
}
if (path.charAt(i)== 'S') {
// Update the PATH only if there is a change
// move straight
// moveForward();
front();
i++;
}
}
if (!digitalRead(14) && !digitalRead(15) && digitalRead(16) && digitalRead(17) && !digitalRead(18) && !digitalRead(19)) {
// Serial.println("move forward ");
front();
}
if (!digitalRead(14) && !digitalRead(15) && !digitalRead(16) && !digitalRead(17) && !digitalRead(18) && !digitalRead(19)) {
// Serial.println("move forward ");
halt();
}
}
void front() {
m1.run(FORWARD);
m2.run(FORWARD);
delay(100);
m1.run(RELEASE);
m2.run(RELEASE);
delay(1);
}
void back() {
m1.run(BACKWARD);
m2.run(BACKWARD);
delay(100);
m1.run(RELEASE);
m2.run(RELEASE);
delay(1);
}
void right() {
m1.run(FORWARD);
m2.run(BACKWARD);
delay(100);
m1.run(RELEASE);
m2.run(RELEASE);
delay(1);
}
void left() {
m1.run(BACKWARD);
m2.run(FORWARD);
delay(100);
m1.run(RELEASE);
m2.run(RELEASE);
delay(1);
}
void halt() {
m1.run(RELEASE);
m2.run(RELEASE);
delay(10);
}
Sir how are we defining for unknown no. Of index..?
sir can you please tell what items like motors have you used in this
It is referred to as DC BO Motor.
sir can you please provide the final code for shortest path maze solver robot for my college techenical fest as i am not able to code it.
very much neened sir.
The final working code is already pinned under comments. Please use it.
sir you have already defined string path but for different maze there will be different string path what should be do at that time ?
The program remembers the path, basically it records the path in the first iteration based on the sensor readings. There is a need to tweak sensor placement on the robot for optimum path recording with multiple trial-error experiments to cover all types of moves. The rest of the headache is handled by the code in optimizing the path and reaching the goal in the second iteration.
i have two dc motor with encoder can i use this code for my robot line follower?
Yes, why not!..... as long as you follow the basics such as Motor speed, sensor placement, motor driver and proper pin mapping in the code.
All the best.....✨
sir I am using motor drive so how can i use adafruit library
You must use the following official library and refer their documentation www.arduino.cc/reference/en/libraries/adafruit-motor-shield-library/.
Once you install you may then use the default examples provided e.g. MotorTest to modify according to your needs.
Let me know how you go with this.....
can we use this bod as line following robot?
Yes ofcourse!
sir can is use this code for any path
Yes, you can!
Sir... Can you share me the code or Please provide me the link of same code... I need it for my college project... I'm waiting for your reply... Thank you sir..!
Please find the code provided above.
how to modify your code for unknown code
Depends on what your build and application is. If you prefer using the AFmotor library then use their documentation. On the other hand if you prefer using no library then simply modify( which means write your own I/O operations on digital pins to get the job done) as per your requirements. If you prefer using the same build and components then you may utilize the code pinned under the video.
Alternatively, you may also write down a pseudocode for your application and later decide which approach fits best for achieving goals.
If I need to follow white line what changes has to make in code
Simply you need to invert all conditional values under "if" conditions. e.g. if it was if (a==1), make it if (a==). Additionally, this process is applicable when you are switching from Active-High to Active-Low sensors or vice-versa.
can you give the sample code for it
@@CrazyRobotics
it is already shared under the comments section. Please try it out and keep exploring 🎉
Sir, This code is not working
Please check, the code is posted and pinned under the comments section.
can i get a code
Shared it under comments. Try and let me know.
can you kindly share the code?
I have shared it.