-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobile_Car_Code.ino
90 lines (81 loc) · 2.06 KB
/
Mobile_Car_Code.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
// Define all Pins
#define speedRight 5
#define in1 6
#define in2 7
#define in3 8
#define in4 9
#define speedLeft 10
char bt;
void setup() {
// Set all defined pins as OUTPUT
for (int pin = 5; pin <= 10; pin++) {
pinMode(pin, OUTPUT);
}
// Initialize serial communication at 9600 baud rate
Serial.begin(9600);
}
void loop() {
// Check if there is data available to read from serial port
if (Serial.available() > 0) {
// Read the incoming byte from serial port
bt = Serial.read();
// Perform action based on the received character
switch (bt) {
case 'F': forward(); break;
case 'B': backward(); break;
case 'R': right(); break;
case 'L': left(); break;
case 'S': stop(); break;
}
}
}
// Function to move the car forward
void forward() {
// Set speed and direction of both motors to move forward
analogWrite(speedRight, 150);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(speedLeft, 150);
}
// Function to move the car backward
void backward() {
// Set speed and direction of both motors to move backward
analogWrite(speedRight, 150);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(speedLeft, 150);
}
// Function to turn the car left
void left() {
// Set speed and direction of both motors to turn left
analogWrite(speedRight, 150);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(speedLeft, 150);
}
// Function to turn the car right
void right() {
// Set speed and direction of both motors to turn right
analogWrite(speedRight, 150);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(speedLeft, 150);
}
// Function to stop the car
void stop() {
// Stop both motors
analogWrite(speedRight, 0);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(speedLeft, 0);
}