-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServo_Control_with_AddBotton
More file actions
122 lines (99 loc) · 3.27 KB
/
Servo_Control_with_AddBotton
File metadata and controls
122 lines (99 loc) · 3.27 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
/****************************************************************************
* 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 allow you to control the servo motor via phone or com port.
At first when you insert "1", the servo motor speed will automatically start
from 0 to maixmun and back to 0. Then you could use commend to control the speed.
* Reference : None
/*****************************************************************************/
#include <Servo.h>
Servo myservo;
int speed = 0;
int Stop = 193;
int UnderStop = 190;
int Maximun_For = 195;
boolean a = 0;
void setup()
{
Serial.begin(9600);
myservo.attach(3);
}
void loop()
{
if (a==0)
{
ServoStartUp();
}
else
{
BluetoothCntrl();
}
}
/*********************************************************************************
* Function Name: ServoStartUp
* Discription: This function will let the servo warm up form stop to speed up.
* Output:
* Communication protocol: Digital signal
* Arduino input port:
* Unit:
*********************************************************************************/
void ServoStartUp()
{
Serial.println("System ready!!! Hit 1");
do
{
if(Serial.read()=='1')
break;
}
while(1);
Serial.println("Throttle up");
for(speed = UnderStop; speed<= Maximun_For; speed+=1)
{
setSpeed(speed);
Serial.println(speed);
delay(100);
}
setSpeed(UnderStop);
a=1;
}
/*********************************************************************************
* Function Name: BluetoothCntrl
* Discription: This function will let you to control the speed by clicking the botton on the phone.
There are three botton on the phone. One is stop, another is speed up and
the other is speed down.
* Communication protocol: Bluetooth UART.
* Arduino input port:
* Unit:
*********************************************************************************/
void BluetoothCntrl()
{
if(Serial.available())
{
int insert = Serial.read();
switch (insert)
{
case 'e' :
speed = Stop;
setSpeed(speed);
Serial.println(speed);
break;
case 'a' :
speed = speed+3;
setSpeed(speed);
Serial.println(speed);
break;
case'd' :
speed = speed-3;
setSpeed(speed);
Serial.println(speed);
break;
}
}
}
}
void setSpeed(int speed)
{
int val = map(speed,0,500,0,180);
myservo.write(val);
}