-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduinocode.ino
85 lines (72 loc) · 2.07 KB
/
arduinocode.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
// Pin definitions for motors
const int rightFwd = 9;
const int rightRev = 10;
const int leftFwd = 5;
const int leftRev = 6;
// Pin definitions for ultrasonic sensor
const int trigPin = 3;
const int echoPin = 4;
// Motor control variables
int rightPwm = 0;
int leftPwm = 0;
// Function to update PWM values
void updatePwm(int rightPwm, int leftPwm) {
analogWrite(rightFwd, rightPwm);
analogWrite(leftFwd, leftPwm);
analogWrite(rightRev, 0);
analogWrite(leftRev, 0);
}
// Function to stop motors
void pwmStop() {
analogWrite(rightFwd, 0);
analogWrite(rightRev, 0);
analogWrite(leftFwd, 0);
analogWrite(leftRev, 0);
}
// Function to get distance from ultrasonic sensor
long getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = (duration / 2) / 29.1; // Convert to cm
return distance;
}
void setup() {
// Initialize motor pins
pinMode(rightFwd, OUTPUT);
pinMode(rightRev, OUTPUT);
pinMode(leftFwd, OUTPUT);
pinMode(leftRev, OUTPUT);
// Initialize pins for ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize serial communication
Serial.begin(9600);
// Stop motors at startup
pwmStop();
}
void loop() {
// Read distance from ultrasonic sensor
long distance = getDistance();
// If distance is less than 10 cm, stop the motors
if (distance < 10) {
pwmStop();
} else {
// Check for incoming serial data
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
// Process motor command
if (command.startsWith("MOTOR")) {
int separatorIndex = command.indexOf(' ', 6);
rightPwm = command.substring(6, separatorIndex).toInt();
leftPwm = command.substring(separatorIndex + 1).toInt();
updatePwm(rightPwm, leftPwm);
} else if (command.startsWith("STOP")) {
pwmStop();
}
}
}
}