-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblind_drive_car.ino
107 lines (100 loc) · 2.14 KB
/
blind_drive_car.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
107
#include <SoftwareSerial.h>
#define rxPin 13
#define txPin 12
char previous_state = 'S';
char current_state = 'S';
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 4;
int in3 = 6;
int in4 = 5;
SoftwareSerial BT_Serial(rxPin, txPin); // RX, TX
void setup() {
BT_Serial.begin(9600);
//For debugging
Serial.begin(9600);
Serial.println(BT_Serial.available());
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(10, OUTPUT);
// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(10, 255);W
}
void loop(){
while(BT_Serial.available()){
current_state = BT_Serial.read();
if (previous_state == current_state) {
continue;
}
previous_state = current_state;
switch (current_state) {
case 'S' :
STOP();
break;
case 'F' :
M1Forward();
break;
case 'B' :
M1Back();
break;
case 'L' :
M2Left();
break;
case 'R':
M2Right();
break;
}
Serial.println(current_state);
}
}
// This function lets you control spinning direction of motors
void M1Forward() {
// Set motors to maximum speed
// For PWM maximum possible values are 0 to 255
analogWrite(enA, 255);
// Turn on motor A & B
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
void M1Back() {
analogWrite(enA, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
void M2Left() {
analogWrite(enA, 255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enB, 255);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void M2Right() {
STOP();
analogWrite(enA, 255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enB, 255);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
void STOP() {
analogWrite(enA, 0);
analogWrite(enB, 0);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}