-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCmoplete_Motor_BT_control
More file actions
143 lines (108 loc) · 4.23 KB
/
Cmoplete_Motor_BT_control
File metadata and controls
143 lines (108 loc) · 4.23 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
/****************************************************************************
* Copyright(C) : National Central University ROD Lab Jen Hao Chen (Howard)
* Create Date : 2014/07/28 by Howard Chen
* Modified Date: 2014/09/25 by Howard Chen
* Abstract : This program will let you test the connection of phone and the buletooth module on robot.
You could simply use the botton on the phone to send commend and make the robot
to take action, such as forward, backward. turn right and turn left.
* Reference : None
/*****************************************************************************/
const int motorIn1 = 4;
const int motorIn2 = 5;
const int motorIn3 = 6;
const int motorIn4 = 7;
void setup()
{
Serial.begin(9600); // Make sure the baud rate is 9600
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
}
void loop()
{
if(Serial.available()) // If there are any data receieved
{
char cmd = Serial.read();
switch (cmd)
{
case 'w': // 接收到'f',前進。
forward();
break;
case 'x': // 接收到'b',後退。
backward();
break;
case 'a': // 接收到'l',左轉。
left();
break;
case 'd': // 接收到'r',右轉。
right();
break;
case 's':
motorstop(); // 停止馬達
break;
}
Serial.flush();
}
delay(10);
}
/*********************************************************************************
* Function Name: motorstop
* Discription: This function will send the commend to the motor driver and stop.
* Communication protocol: digital write
*********************************************************************************/
void motorstop()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
}
/*********************************************************************************
* Function Name: motorstop
* Discription: This function will send the commend to the motor driver and forward.
* Communication protocol: digital write
*********************************************************************************/
void forward()
{
digitalWrite(motorIn1, HIGH );
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
}
/*********************************************************************************
* Function Name: backward
* Discription: This function will send the commend to the motor driver and backward
* Communication protocol: digital write
*********************************************************************************/
void backward()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, HIGH);
}
/*********************************************************************************
* Function Name: right
* Discription: This function will send the commend to the motor driver and turn right.
* Communication protocol: digital write
*********************************************************************************/
void right()
{
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
}
/*********************************************************************************
* Function Name: left
* Discription: This function will send the commend to the motor driver and turn left.
* Communication protocol: digital write
*********************************************************************************/
void left()
{
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, HIGH);
}