-
Notifications
You must be signed in to change notification settings - Fork 0
/
WMS.ino
106 lines (90 loc) · 2.47 KB
/
WMS.ino
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
#include <L298N.h>
// Define L298N instances for each motor
L298N motor1(9, 8, 10); // Adjust pin numbers as per your wiring
L298N motor2(11, 12, 13); // Adjust pin numbers as per your wiring
int error = 0;
int lerror = 0;
int speed1, speed2, out;
const int kp = 15;
const int kd = 7;
int trigPin1 = 6;
int echoPin1 = A1;
int trigPin2 = 5;
int echoPin2 = A0;
long duration1, cm1;
long duration2, cm2;
void setup() {
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
// Adjust motor direction if necessary
motor1.setSpeed(255); // Adjust speed as needed
motor2.setSpeed(255); // Adjust speed as needed
delay(1000);
}
void loop() {
// Readings for sensor 1
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);
cm1 = (duration1 / 2) / 29.1;
// Readings for sensor 2
digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);
cm2 = (duration2 / 2) / 29.1;
// Debugging: print sensor readings
Serial.print("Sensor 1: ");
Serial.print(cm1);
Serial.print(" cm, Sensor 2: ");
Serial.print(cm2);
Serial.println(" cm");
// Adjusting motor speed based on error
if (cm2 < 10 && cm2 > 6) {
error = 0;
}
if (cm2 > 10 && cm2 < 20) {
error = 1;
}
if (cm2 < 6) {
error = -1;
}
out = kp * error + kd * (lerror - error);
Serial.print("Output: ");
Serial.println(out);
speed1 = 255 + out; // Adjust speed base value as needed
speed2 = 255 - out; // Adjust speed base value as needed
motor1.setSpeed(speed1);
motor2.setSpeed(speed2);
// Debugging: print motor speeds
Serial.print("Motor Speeds: ");
Serial.print(speed1);
Serial.print(", ");
Serial.println(speed2);
// Adjust motor directions based on conditions
if (cm1 < 5 && cm2 < 10) {
motor1.run(L298N::BACKWARD); // Adjust direction as needed
motor2.run(L298N::BACKWARD); // Adjust direction as needed
delay(500);
motor1.stop();
motor2.stop();
}
if (cm2 > 17) {
delay(1100);
motor1.run(L298N::FORWARD); // Adjust direction as needed
motor2.run(L298N::FORWARD); // Adjust direction as needed
delay(500);
motor1.stop();
motor2.stop();
}
}