So I am making a bot that has a line sensor and an ultrasonic sensor.
#include <NewPing.h>
#include "CytronMotorDriver.h"
#define LINE_L1 8
#define LINE_L2 7
#define LINE_L3 6
#define LINE_L4 5
#define LINE_L5 4
#define TRIGGER_PIN 2
#define ECHO_PIN 3
CytronMD motorL(PWM_PWM, 11, 12); //full RPM is 193
CytronMD motorR(PWM_PWM, 9, 10); //full RPM is 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN);
void setup()
{
Serial.begin(9600);
pinMode(LINE_L1, INPUT);
pinMode(LINE_L2, INPUT);
pinMode(LINE_L3, INPUT);
pinMode(LINE_L4, INPUT);
pinMode(LINE_L5, INPUT);
pinMode(2,INPUT_PULLUP);
while(digitalRead(2)==HIGH);
while(digitalRead(2)==LOW);
delay (500);
}
void loop()
{
int L1 = digitalRead(LINE_L1);
int L2 = digitalRead(LINE_L2);
int L3 = digitalRead(LINE_L3);
int L4 = digitalRead(LINE_L4);
int L5 = digitalRead(LINE_L5);
delay(50);
unsigned int distance = sonar.ping_cm();
Serial.print(distance);
Serial.println("cm");
if (L1==1 || L2==1 || L3==1 || L4==1 || L5==1)
{
if (L1==0 && L2==0 && L3==1 && L4==0 && L5==0) { //line sensor part
motorL.setSpeed(193);
motorR.setSpeed(200); //robot move forward
}
else if (L1==0 && L2==1 && L3==1 && L4==0 && L5==0) {
motorL.setSpeed(160);
motorR.setSpeed(200); //robot move slightly left
}
else if (L1==0 && L2==1 && L3==0 && L4==0 && L5==0) {
motorL.setSpeed(160);
motorR.setSpeed(200); //robot move slightly left
}
else if (L1==1 && L2==1 && L3==0 && L4==0 && L5==0) {
motorL.setSpeed(60);
motorR.setSpeed(200); //robot slowing down, move more to left
}
else if (L1==1 && L2==0 && L3==0 && L4==0 && L5==0) {
motorL.setSpeed(0);
motorR.setSpeed(200); //robot slowing down, move more to left
}
else if (L1==0 && L2==0 && L3==1 && L4==1 && L5==0) {
motorL.setSpeed(193);
motorR.setSpeed(160); //robot move slightly right
}
else if (L1==0 && L2==0 && L3==0 && L4==1 && L5==0) {
motorL.setSpeed(193);
motorR.setSpeed(160); //robot move slightly right
}
else if (L1==0 && L2==0 && L3==0 && L4==1 && L5==1) {
motorL.setSpeed(193);
motorR.setSpeed(90); //robot slowing down, move more to right
}
else if (L1==0 && L2==0 && L3==0 && L4==0 && L5==1) {
motorL.setSpeed(100);
motorR.setSpeed(0); //robot slowing down, move more to right
}
else {
}
}
else
{
if (distance > 20) //ultrasonic sensor part
{
motorL.setSpeed(193);
motorR.setSpeed(200);
}
else
{
motorL.setSpeed(-193);
motorR.setSpeed(200); //turn left
delay(300);
motorL.setSpeed(0);
motorR.setSpeed(0);
delay(1000);
motorL.setSpeed(193);
motorR.setSpeed(200); //go straight
delay(1000);
motorL.setSpeed(0);
motorR.setSpeed(0);
delay(1000);
motorL.setSpeed(193);
motorR.setSpeed(-200); //turn right
delay(340);
motorL.setSpeed(0);
motorR.setSpeed(0);
delay(1000);
}
}
}
This is the code that is not working as I intended, specifically the line sensor part.^^^^
#include "CytronMotorDriver.h"
#define LINE_D1 8
#define LINE_D2 7
#define LINE_D3 6
#define LINE_D4 5
#define LINE_D5 4
// Configure the motor driver.
CytronMD motorL(PWM_PWM, 11, 12); // motor 1 = Left motor, M1A = Pin 3, M1B = Pin 9
CytronMD motorR(PWM_PWM, 9, 10); // motor 2= right motor, M2A = Pin 10, M2B = Pin 11
void setup() {
pinMode(LINE_D1, INPUT);
pinMode(LINE_D2, INPUT);
pinMode(LINE_D3, INPUT);
pinMode(LINE_D4, INPUT);
pinMode(LINE_D5, INPUT);
pinMode(2,INPUT_PULLUP);
while(digitalRead(2)==HIGH);
while(digitalRead(2)==LOW);
}
void loop() {
// Perform line following
int D1 = digitalRead(LINE_D1);
int D2 = digitalRead(LINE_D2);
int D3 = digitalRead(LINE_D3);
int D4 = digitalRead(LINE_D4);
int D5 = digitalRead(LINE_D5);
if (D1==0 && D2==0 && D3==1 && D4==0 && D5==0) {
motorL.setSpeed(193);
motorR.setSpeed(200); //robot move forward
}
else if (D1==0 && D2==1 && D3==1 && D4==0 && D5==0) {
motorL.setSpeed(160);
motorR.setSpeed(200); //robot move slightly left
}
else if (D1==0 && D2==1 && D3==0 && D4==0 && D5==0) {
motorL.setSpeed(160);
motorR.setSpeed(200); //robot move slightly left
}
else if (D1==1 && D2==1 && D3==0 && D4==0 && D5==0) {
motorL.setSpeed(60);
motorR.setSpeed(200); //robot slowing down, move more to left
}
else if (D1==1 && D2==0 && D3==0 && D4==0 && D5==0) {
motorL.setSpeed(0);
motorR.setSpeed(200); //robot slowing down, move more to left
}
else if (D1==0 && D2==0 && D3==1 && D4==1 && D5==0) {
motorL.setSpeed(193);
motorR.setSpeed(160); //robot move slightly right
}
else if (D1==0 && D2==0 && D3==0 && D4==1 && D5==0) {
motorL.setSpeed(193);
motorR.setSpeed(160); //robot move slightly right
}
else if (D1==0 && D2==0 && D3==0 && D4==1 && D5==1) {
motorL.setSpeed(193);
motorR.setSpeed(90); //robot slowing down, move more to right
}
else if (D1==0 && D2==0 && D3==0 && D4==0 && D5==1) {
motorL.setSpeed(100);
motorR.setSpeed(0); //robot slowing down, move more to right
}
else {
}
}
This is the code I wrote prior adding it to the first code, this code works when i upload it onto the board. But when I integrate it into the first code as the line sensor code, it just partially works. I mean by it can detect the line alright and follow it, but when the line turns like, to the right. It just stopped working, like it stop in its tracks. What is the problem? Thanks in advance.
I am using an Arduino Uno, a maker line sensor from seeed, a HC-SR04 ultrasonic sensor, and a seeed maker motor driver.