MR Thei
MR Thei
h>
#include <LiquidCrystal_I2C.h>
// Khai báo LCD với địa chỉ I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Motor Pins
const int motorA1 = 6;
const int motorA2 = 5;
const int motorAspeed = 4;
const int motorB1 = 8;
const int motorB2 = 9;
const int motorBspeed = 7;
// Line Sensor Pins
const int L_L_S = 40;
const int L_S = 42;
const int S_S = 44;
const int R_S = 46;
const int R_R_S = 48;
// Additional Sensor Pins
const int sensorPin1 = 49;
const int sensorPin2 = 47;
const int sensorPin3 = 45;
const int sensorPin4 = 43;
const int sensorPin5 = 51;
// Ultrasonic Sensor Pins
const int TRIG_PIN = 3;
const int ECHO_PIN = 2;
// Khai báo các chân
const int IN1 = 31;
const int IN2 = 33;
const int LIMIT_SWITCH1 = 35;
const int IN3 = 30;
const int IN4 = 32;
const int LIMIT_SWITCH2 = 34;
const int INFRARED_SENSOR = 37;
long duration;
int distance;
void setup() {
pinMode(L_L_S, INPUT);
pinMode(L_S, INPUT);
pinMode(S_S, INPUT);
pinMode(R_S, INPUT);
pinMode(R_R_S, INPUT);
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
pinMode(motorAspeed, OUTPUT);
pinMode(motorBspeed, OUTPUT);
pinMode(sensorPin5, INPUT_PULLUP);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(sensorPin3, INPUT);
pinMode(sensorPin4, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(LIMIT_SWITCH1, INPUT_PULLUP);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(LIMIT_SWITCH2, INPUT_PULLUP);
pinMode(INFRARED_SENSOR, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_TOP, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Serial1.begin(115200);
lcd.begin();
lcd.backlight();
lcd.setCursor(2, 0);
lcd.print("He thong OK!");
delay(1000);
calibrateSensors();
moveMotorsHome();
//reverseMotorsAfterHome();
}
///////////////////////////////////////////////////////////////////////////////////
///
void loop() {
lcd.setCursor(0, 0);
lcd.print("Dang cho lenh...");
blinkLED();
if (digitalRead(LIMIT_SWITCH_TOP) == LOW) {
stopMotors();
//Serial.println("Động cơ dừng do công tắc hành trình giới hạn trên.");
}
if (Serial1.available()) {
String command = Serial1.readString();
command.trim();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Lenh: " + command);
if (command == "11") {
motor1Running = true;
motor2Running = true;
findStartPosition();
moveToPosition1();
reverseMotorsAfterHome();
findStartPosition();
} else if (command == "22") {
motor1Running = true;
motor2Running = true;
findStartPosition();
moveToPosition2();
reverseMotorsAfterHome();
findStartPosition();
} else if (command == "33") {
motor1Running = true;
motor2Running = true;
findStartPosition();
moveToPosition3();
reverseMotorsAfterHome();
findStartPosition();
} else if (command == "44") {
motor1Running = true;
motor2Running = true;
findStartPosition();
moveToPosition4();
reverseMotorsAfterHome();
findStartPosition();
} else if (command == "55") {
motor1Running = true;
motor2Running = true;
findStartPosition();
reverseMotorsAfterHome();
moveToPosition1();
moveMotorsHome();
findStartPosition();
} else if (command == "66") {
motor1Running = true;
motor2Running = true;
findStartPosition();
reverseMotorsAfterHome();
moveToPosition2();
moveMotorsHome();
findStartPosition();
} else if (command == "77") {
motor1Running = true;
motor2Running = true;
findStartPosition();
reverseMotorsAfterHome();
moveToPosition3();
moveMotorsHome();
findStartPosition();
} else if (command == "88") {
motor1Running = true;
motor2Running = true;
findStartPosition();
reverseMotorsAfterHome();
moveToPosition4();
moveMotorsHome();
findStartPosition();
}
}
}
///////////////////////////////////////////////////////////////////////////////////
///
void calibrateSensors() {
Serial.println("Calibrating sensors...");
delay(1000);
Serial.println("Calibration complete.");
}
///////////////////////////////////////////////////////////////////////////////////
///
void Line_follower() {
distance = getDistance();
if (distance < stop_distance) {
Stop();
return;
}
if (digitalRead(sensorPin5) == LOW) {
forword();
delay(1000);
Vuong_goc();
delay(200);
unsigned long startTime = millis();
while (true) {
int sensorStates = (digitalRead(L_L_S) << 4) | (digitalRead(L_S) << 3) |
(digitalRead(S_S) << 2) | (digitalRead(R_S) << 1) | digitalRead(R_R_S);
if (sensorStates == 0b11110 || sensorStates == 0b11101 || sensorStates ==
0b11100) {
break;
}
if (millis() - startTime > 5000) {
break;
}
Vuong_goc();
delay(100);
}
Stop();
}
handleLineFollowing();
}
///////////////////////////////////////////////////////////////////////////////////
///
void handleLineFollowing() {
int sensorStates = (digitalRead(L_L_S) << 4) | (digitalRead(L_S) << 3) |
(digitalRead(S_S) << 2) | (digitalRead(R_S) << 1) | digitalRead(R_R_S);
if (sensorStates != 0b00000) {
lastValidSensorTime = millis();
}
if (millis() - lastValidSensorTime > 2000) {
Serial.println("No valid signal for 2 seconds, stopping.");
forword();
delay(200);
Stop();
lastValidSensorTime = millis();
}
int L_L_S_val = !digitalRead(L_L_S);
int L_S_val = !digitalRead(L_S);
int S_S_val = !digitalRead(S_S);
int R_S_val = !digitalRead(R_S);
int R_R_S_val = !digitalRead(R_R_S);
///////////////////////////////////////////////////////////////////////////////////
///
/*void displayLineSensorStatus() {
Serial.print("L_L_S: ");
Serial.print(digitalRead(L_L_S));
Serial.print(" | L_S: ");
Serial.print(digitalRead(L_S));
Serial.print(" | S_S: ");
Serial.print(digitalRead(S_S));
Serial.print(" | R_S: ");
Serial.print(digitalRead(R_S));
Serial.print(" | R_R_S: ");
Serial.print(digitalRead(R_R_S));
Serial.print(" | Sensor 1: ");
Serial.print(digitalRead(sensorPin1));
Serial.print(" | Sensor 2: ");
Serial.print(digitalRead(sensorPin2));
Serial.print(" | Sensor 3: ");
Serial.print(digitalRead(sensorPin3));
Serial.print(" | Sensor 4: ");
Serial.println(digitalRead(sensorPin4));
}*/
///////////////////////////////////////////////////////////////////////////////////
///
void rotateMotorsForward() {
if (digitalRead(LIMIT_SWITCH_TOP) == LOW) {
stopMotors();
return;
}
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
///////////////////////////////////////////////////////////////////////////////////
///
void rotateMotorsBackward() {
if (digitalRead(LIMIT_SWITCH1) == LOW) {
stopMotors();
delay(1000);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(6000);
stopMotors();
} else {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
}
///////////////////////////////////////////////////////////////////////////////////
///
void stopMotors() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
///////////////////////////////////////////////////////////////////////////////////
///
void checkLimitSwitch(int limitSwitchPin, int motorPin1, int motorPin2, String
motorName) {
if (digitalRead(limitSwitchPin) == LOW) {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
}
///////////////////////////////////////////////////////////////////////////////////
///
void moveMotorsHome() {
unsigned long startMillis = millis();
bool motor1Home = false;
bool motor2Home = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dang ha hang");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dang nang hang");