-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_code.ino
More file actions
156 lines (137 loc) · 3.51 KB
/
Copy pathmain_code.ino
File metadata and controls
156 lines (137 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <Motor_Shield.h>
//this code does not do turning properly
// Pin definitions
#define trigPin A5
#define echoPin A3
#define irLeft 2
#define irRight 5
// Configuration variables (easily modifiable)
const int FORWARD_SPEED = 180; // Default forward speed (can be changed)
const int OBSTACLE_THRESHOLD = 3; // Obstacle distance in cm
const int TURN_DELAY = 10; // Delay for turns (ms)
// Timing variables
unsigned long lastSpeedPrintTime = 0;
const unsigned long SPEED_PRINT_INTERVAL = 3000; // 3 seconds
unsigned long lastDistancePrintTime = 0;
const unsigned long DISTANCE_PRINT_INTERVAL = 1500; // 1.5 seconds
// Motor instances
DCMotor Amotor(1);
DCMotor Bmotor(2);
// Function declarations
long getDistance();
void Forward(unsigned char SpeedLeft, unsigned char SpeedRight);
void Backward(unsigned char Speed);
void Left();
void Right();
void Stop();
void take_diversion();
void setup() {
Serial.begin(9600);
pinMode(irLeft, INPUT);
pinMode(irRight, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Direct port manipulation for faster IR sensor reading
bool irLeftStatus = PIND & (1 << irLeft);
bool irRightStatus = PIND & (1 << irRight);
// Get distance from ultrasonic sensor
long distance = getDistance();
// Print distance every 1.5 seconds
unsigned long currentMillis = millis();
if (currentMillis - lastDistancePrintTime >= DISTANCE_PRINT_INTERVAL) {
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
lastDistancePrintTime = currentMillis;
}
// Control logic based on sensor readings
if (!irLeftStatus && irRightStatus) {
Stop();
Left();
}
else if (!irRightStatus && irLeftStatus) {
Stop();
Right();
}
else if (irLeftStatus && irRightStatus) {
if (distance < OBSTACLE_THRESHOLD) {
Serial.println("Obstacle Detected! Taking Diversion...");
Stop();
delay(200);
take_diversion();
}
else {
Forward(FORWARD_SPEED, FORWARD_SPEED);
}
}
else {
Stop();
}
// Small delay for stability
//delay(10);
}
// Motor control functions:
void Forward(unsigned char SpeedLeft, unsigned char SpeedRight) {
Amotor.run(FORWARD);
Bmotor.run(FORWARD);
Amotor.setSpeed(SpeedLeft);
Bmotor.setSpeed(SpeedRight);
// Print speed status every 3 seconds
unsigned long currentMillis = millis();
if (currentMillis - lastSpeedPrintTime >= SPEED_PRINT_INTERVAL) {
Serial.print("Motor Speeds -> Left: ");
Serial.print(SpeedLeft);
Serial.print(" | Right: ");
Serial.println(SpeedRight);
lastSpeedPrintTime = currentMillis;
}
}
void Backward(unsigned char Speed) {
Amotor.run(BACKWARD);
Bmotor.run(BACKWARD);
Amotor.setSpeed(Speed);
Bmotor.setSpeed(Speed);
}
void Left() {
Amotor.run(FORWARD);
Bmotor.run(BACKWARD);
Amotor.setSpeed(150);
Bmotor.setSpeed(200);
delay(TURN_DELAY);
}
void Right() {
Amotor.run(BACKWARD);
Bmotor.run(FORWARD);
Amotor.setSpeed(200);
Bmotor.setSpeed(150);
delay(TURN_DELAY);
}
void Stop() {
Amotor.setSpeed(0);
Bmotor.setSpeed(0);
delay(50);
}
void take_diversion() {
Right();
delay(250);
Forward(180, 180);
delay(800);
Left();
delay(450);
Forward(180, 180);
delay(1120);
Left();
delay(400);
}
long getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000); // 30ms timeout
long distance = (duration * 0.034) / 2;
return distance;
}