Is there any way to disable arduino pins to prevent executing other components while a condition is met. In my case, when my frontsensor detects something, the robot should move backward even if the line tracker detects the line. I would like to disable the line tracker sensor for a few seconds while the front sensor condition is met. I'm having a hard time explaining my problem but I hope you get what I want to say. Any help from you guys would be much appreciated. Sorry for my grammar. By the way, here's the code:
#include <Wire.h> //
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE); // 0x27 is the I2C bus address for an unmodified backpack
#define IN1 22
#define IN2 24
#define IN3 26
#define IN4 28
#define ENA 2
#define ENB 3
int echo_left = 37;
int trig_left = 39;
int echo_right = 41;
int trig_right = 43;
int echo_front = 45;
int trig_front = 47;
long duration, SensorLeft, SensorRight, SensorFront;
int LineTrackRight;
int LineTrackLeft;
int LineTrackCenter;
Servo myservo1; // 20 degree initial value 180 degrees off value
Servo myservo2;
Servo myservo3;
Servo myservo4;
int pos;
void MotorForward() {
analogWrite (ENA, 70);
analogWrite (ENB, 70);
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
}
void MotorBackward() {
analogWrite (ENA, 60);
analogWrite (ENB, 60);
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
}
void MotorRadialLeftTurn() {
analogWrite (ENA, 70);
analogWrite (ENB, 0);
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, LOW);
}
void MotorAxialRightTurn() {
analogWrite (ENA, 70);
analogWrite (ENB, 70);
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
}
void MotorAxialRightTurnLow() {
analogWrite (ENA, 50);
analogWrite (ENB, 50);
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
}
void MotorAxialLeftTurn() {
analogWrite (ENA, 70);
analogWrite (ENB, 70);
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
}
void MotorAxialLeftTurnLow() {
analogWrite (ENA, 50);
analogWrite (ENB, 50);
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, HIGH);
}
void MotorRadialRightTurn() {
analogWrite (ENA, 0);
analogWrite (ENB, 70);
digitalWrite (IN1, LOW);
digitalWrite (IN2, LOW);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
}
void MotorStop() {
analogWrite (ENA, 0);
analogWrite (ENB, 0);
digitalWrite (IN1, HIGH);
digitalWrite (IN2, HIGH);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, HIGH);
}
void CheckSensorFront() {
digitalWrite(trig_front, LOW);
delayMicroseconds(5);
digitalWrite(trig_front, HIGH);
delayMicroseconds(10);
digitalWrite(trig_front, LOW);
duration = pulseIn(echo_front, HIGH);
SensorFront = (duration/2) / 74;
}
void CheckSensorLeft() {
digitalWrite(trig_left, LOW);
delayMicroseconds(5);
digitalWrite(trig_left, HIGH);
delayMicroseconds(10);
digitalWrite(trig_left, LOW);
duration = pulseIn(echo_left, HIGH);
SensorLeft = (duration/2) / 74;
if (SensorLeft <= 5){
MotorStop();
delay(1000);
MotorBackward();
delay(700);
MotorAxialLeftTurnLow();
delay(700);
}
}
void CheckSensorRight() {
digitalWrite(trig_right, LOW);
delayMicroseconds(5);
digitalWrite(trig_right, HIGH);
delayMicroseconds(10);
digitalWrite(trig_right, LOW);
duration = pulseIn(echo_right, HIGH);
SensorRight = (duration/2) / 74;
if (SensorRight <= 5){
MotorStop();
delay(1000);
MotorBackward();
delay(700);
MotorAxialRightTurnLow();
delay(700);
}
}
void setup(){
Serial.begin(9600);
lcd.begin (20, 4);
lcd.print("Autonomous Garbage Collector Robot");
pinMode (31, INPUT);
pinMode (33, INPUT);
pinMode (35, INPUT);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (IN3, OUTPUT);
pinMode (IN4, OUTPUT);
pinMode (ENA, OUTPUT);
pinMode (ENB, OUTPUT);
pinMode (echo_left, INPUT);
pinMode (trig_left, OUTPUT);
pinMode (echo_right, INPUT);
pinMode (trig_right, OUTPUT);
pinMode (echo_front, INPUT);
pinMode (trig_front, OUTPUT);
}
void loop(){
CheckSensorFront();
LineTrackLeft = digitalRead(31);
LineTrackCenter = digitalRead(33);
LineTrackRight = digitalRead(35);
if (LineTrackLeft == 1 && LineTrackRight == 1 && LineTrackCenter == 1){
MotorForward();
}
else if (LineTrackLeft == 1 && LineTrackRight == 0 && LineTrackCenter == 1){
MotorAxialLeftTurn();
}
else if (LineTrackLeft == 0 && LineTrackRight == 1 && LineTrackCenter == 1){
MotorAxialRightTurn();
}
if (SensorFront >= 0 && SensorFront < 7){
MotorBackward();
}
else if (SensorFront == 7){
MotorStop();
delay(5000);
}
CheckSensorLeft();
CheckSensorRight();
}