0% found this document useful (0 votes)
15 views5 pages

FinalProject (CODE)

The document contains an Arduino program that controls traffic lights using both automatic and manual modes. It utilizes ultrasonic sensors to detect distances and adjusts the traffic light states accordingly. The program includes functions for setup, control logic, and distance measurement, and it communicates with the user via serial input.

Uploaded by

lnhoanganh3504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

FinalProject (CODE)

The document contains an Arduino program that controls traffic lights using both automatic and manual modes. It utilizes ultrasonic sensors to detect distances and adjusts the traffic light states accordingly. The program includes functions for setup, control logic, and distance measurement, and it communicates with the user via serial input.

Uploaded by

lnhoanganh3504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

const int G1 = 8;

const int Y1 = 9;
const int R1 = 10;

const int G2 = 2;
const int Y2 = 3;
const int R2 = 4;

const int trigPin = 6;


const int echoPin = 7;

const int trigPin2 = 11;


const int echoPin2 = 12;

char mode = 'a'; char manualCommand = '0';


bool manualActive = false;

int autoState = 0;
unsigned long previousMillis = 0;
bool started = false;

unsigned long previousMillisManual = 0;


int manualState = 0;

void setup() {
[Link](9600);
[Link]("Khoi dong chuong trinh...");

pinMode(G1, OUTPUT);
pinMode(Y1, OUTPUT);
pinMode(R1, OUTPUT);
pinMode(G2, OUTPUT);
pinMode(Y2, OUTPUT);
pinMode(R2, OUTPUT);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);

turnOffAll();
[Link]("Mac dinh che do: Tu dong (a)");
}

void loop() {
checkSerialInput();

if (mode == 'a') {
controlAuto();
} else if (mode == 'b') {
controlManual();
}
}
void checkSerialInput() {
if ([Link]()) {
char command = [Link]();
[Link]("Nhan lenh: ");
[Link](command);

if (command == 'a') {
mode = 'a';
autoState = 0;
started = false;
previousMillis = millis();
turnOffAll();
[Link]("Chuyen sang che do: Tu dong (a)");

} else if (command == 'b') {


mode = 'b';
manualState = 0;
manualCommand = '0';
manualActive = false;
previousMillisManual = millis();
turnOffAll();
[Link]("Chuyen sang che do: Thu cong (b)");
[Link]("Nhap lenh: '1' (G1 xanh), '2' (G2 xanh), '0' (tat den)");

} else if (mode == 'b') {


manualCommand = command;
manualActive = true;
previousMillisManual = millis();
[Link]("Lenh thu cong: ");
[Link](manualCommand);
}
}
}

// Điều khiển tự động


void controlAuto() {
unsigned long currentMillis = millis();
long distance1 = readDistanceCM(trigPin, echoPin);
long distance2 = readDistanceCM(trigPin2, echoPin2);

[Link]("Sensor1: ");
[Link](distance1);
[Link](" cm, Sensor2: ");
[Link](distance2);
[Link](" cm");

switch (autoState) {
case 0:
if (!started) {
turnOffAll();
digitalWrite(G1, HIGH);
digitalWrite(R2, HIGH);
previousMillis = currentMillis;
started = true;
[Link]("Auto 0: G1 ON, R2 ON");
}

if (currentMillis - previousMillis >= ((distance1 < 20 && distance1 != -1) ?


10000 : 5000)) {
autoState = 1;
started = false;
digitalWrite(G1, LOW);
digitalWrite(Y1, HIGH);
previousMillis = currentMillis;
[Link]("Auto 0 -> 1: Y1 ON");
}
break;

case 1:
if (!started) {
previousMillis = currentMillis;
started = true;
}

if (currentMillis - previousMillis >= 5000) {


autoState = 2;
started = false;
digitalWrite(Y1, LOW);
digitalWrite(R1, HIGH);
digitalWrite(R2, LOW);
digitalWrite(G2, HIGH);
previousMillis = currentMillis;
[Link]("Auto 1 -> 2: G2 ON, R1 ON");
}
break;

case 2:
if (!started) {
previousMillis = currentMillis;
started = true;
[Link]("Auto 2: G2 ON, R1 ON");
}

if (currentMillis - previousMillis >= ((distance2 < 20 && distance2 != -1) ?


10000 : 5000)) {
autoState = 3;
started = false;
digitalWrite(G2, LOW);
digitalWrite(Y2, HIGH);
previousMillis = currentMillis;
[Link]("Auto 2 -> 3: Y2 ON");
}
break;

case 3:
if (!started) {
previousMillis = currentMillis;
started = true;
}

if (currentMillis - previousMillis >= 5000) {


autoState = 0;
started = false;
digitalWrite(Y2, LOW);
digitalWrite(R1, LOW);
digitalWrite(R2, HIGH);
digitalWrite(G1, HIGH);
previousMillis = currentMillis;
[Link]("Auto 3 -> 0: G1 ON, R2 ON");
}
break;
}

delay(100);
}

// Điều khiển thủ công


void controlManual() {
unsigned long currentMillis = millis();

switch (manualCommand) {
case '1':
turnOffAll();
switch (manualState) {
case 0:
digitalWrite(Y2, HIGH);
digitalWrite(R1, HIGH);
if (currentMillis - previousMillisManual >= 5000) {
manualState = 1;
previousMillisManual = currentMillis;
digitalWrite(Y2, LOW);
digitalWrite(R1, LOW);
digitalWrite(G1, HIGH);
digitalWrite(R2, HIGH);
}
break;
case 1:
break;
}
break;

case '2':
turnOffAll();
switch (manualState) {
case 0:
digitalWrite(Y1, HIGH);
digitalWrite(R2, HIGH);
if (currentMillis - previousMillisManual >= 5000) {
manualState = 1;
previousMillisManual = currentMillis;
digitalWrite(Y1, LOW);
digitalWrite(R2, LOW);
digitalWrite(G2, HIGH);
digitalWrite(R1, HIGH);
}
break;
case 1:
break;
}
break;

case '0':
turnOffAll();
manualActive = false;
manualCommand = '0';
manualState = 0;
break;

default:
if (manualActive) {
[Link]("Lenh khong hop le!");
}
break;
}

if (manualActive && (currentMillis - previousMillisManual > 10000)) {


turnOffAll();
manualActive = false;
manualCommand = '0';
[Link]("Het thoi gian, tat den thu cong.");
}

delay(100);
}

// Tắt tất cả đèn


void turnOffAll() {
digitalWrite(G1, LOW);
digitalWrite(Y1, LOW);
digitalWrite(R1, LOW);
digitalWrite(G2, LOW);
digitalWrite(Y2, LOW);
digitalWrite(R2, LOW);
[Link]("Tat tat ca den");
}

// Hàm đo khoảng cách


long readDistanceCM(int trig, int echo) {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

long duration = pulseIn(echo, HIGH, 30000); // timeout 30ms


if (duration == 0) {
[Link]("Khong nhan xung tu cam bien!");
return -1;
}

long distance = duration / 2 / 29.154;


return distance;
}

You might also like