-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduinocar.ino
More file actions
145 lines (124 loc) · 2.96 KB
/
arduinocar.ino
File metadata and controls
145 lines (124 loc) · 2.96 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
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial CAR_BT(11,12); //定義 PIN11 及 PIN12 分別為 RX 及 TX 腳位
/* Input for leftMotor:
IN1 IN2 Action
LOW LOW Stop
HIGH LOW Forward
LOW HIGH Backward
HIGH HIGH Stop
*/
const int leftMotorIn1 = 5;
const int leftMotorIn2 = 6;
/* Input for rightMotor:
IN3 IN4 Action
LOW LOW Stop
HIGH LOW Forward
LOW HIGH Backward
HIGH HIGH Stop
*/
const int rightMotorIn3 = 9;
const int rightMotorIn4 = 10;
const int ENA = 2;
const int ENB = 3;
void setup()
{
pinMode(leftMotorIn1, OUTPUT);
pinMode(leftMotorIn2, OUTPUT);
pinMode(rightMotorIn3, OUTPUT);
pinMode(rightMotorIn4, OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
Serial.begin(9600); //Arduino起始鮑率:9600
CAR_BT.begin(9600); //藍牙鮑率:9600 注意!每個藍牙晶片的鮑率都不太一樣,請務必確認
}
void loop()
{
int inSize;
int input;
if( (inSize = (CAR_BT.available())) > 0) { //讀取藍牙訊息
Serial.print("size = ");
Serial.println(inSize);
Serial.print("Input = ");
Serial.println(input = CAR_BT.read());
if( input == 8 ) {
forward();
Serial.println(input);
}
if( input == 2 ) {
backward();
Serial.println(input);
}
if( input == 7 ) {
Fleft();
Serial.println(input);
}
if( input == 9 ) {
Fright();
Serial.println(input);
}
if( input == 1 ) {
Bleft();
Serial.println(input);
}
if( input == 3 ) {
Bright();
Serial.println(input);
}
if( input == 5 ) {
motorstop();
Serial.println(input);
}
}
}
void motorstop()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
}
void forward()
{
analogWrite(leftMotorIn1, 0);
analogWrite(leftMotorIn2, 255);
analogWrite(rightMotorIn3, 255);
analogWrite(rightMotorIn4, 0);
}
void backward()
{
analogWrite(leftMotorIn1, 255);
analogWrite(leftMotorIn2, 0);
analogWrite(rightMotorIn3, 0);
analogWrite(rightMotorIn4, 255);
}
void Fright()
{
analogWrite(leftMotorIn1, 0);
analogWrite(leftMotorIn2, 255);
analogWrite(rightMotorIn3, 150);
analogWrite(rightMotorIn4, 0);
}
void Fleft()
{
analogWrite(leftMotorIn1, 0);
analogWrite(leftMotorIn2, 150);
analogWrite(rightMotorIn3, 255);
analogWrite(rightMotorIn4, 0);
}
void Bright()
{
analogWrite(leftMotorIn1, 255);
analogWrite(leftMotorIn2, 0);
analogWrite(rightMotorIn3, 0);
analogWrite(rightMotorIn4, 150);
}
void Bleft()
{
analogWrite(leftMotorIn1, 150);
analogWrite(leftMotorIn2, 0);
analogWrite(rightMotorIn3, 0);
analogWrite(rightMotorIn4, 255);
}