-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArtificialHand.ino
More file actions
168 lines (154 loc) · 4.73 KB
/
ArtificialHand.ino
File metadata and controls
168 lines (154 loc) · 4.73 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<Servo.h>
Servo thumbMotor, indexMotor, middleMotor, ringMotor, littleMotor;
int thumbPin=10, indexPin=11, middlePin=9, ringPin=8, littlePin=7;
Servo motor_list[5] = {thumbMotor, indexMotor, middleMotor, ringMotor, littleMotor};
byte motor_angle_list[5] = {70, 0, 50, 40, 70};
int motor_pin_list[5]={thumbPin, indexPin, middlePin, ringPin, littlePin};
bool motionFlag = false;
int btnOnePin=2, btnTwoPin=3;
int BTN_PRESSED=0, BTN_UNPRESSED=1;
int pos = 0;
/*
Name: ArtificialHand.ino
Created: 2019-11-19 ~
Author: 10304 김민준
*/
// the setup function runs once when you press reset or power the board
void setup() {
/*
5개 서보 가로 폭 100.7mm
볼트 홈 아래 ~ 서보모터 아래 24mm
*/
for(int i=0;i<5;i++){
motor_list[i].attach(motor_pin_list[i]);
motor_list[i].write(0);
delay(15);
}
/*
택트 스위치는 아두이노에서 digitalRead() 함수로 읽어올 때 오작동이 생기는 것을 방지하기 위해 PULL_UP 저항을 사용하며, pinMode() 함수로 설정해줄 때 INPUT_PULLUP 으로 설정해준다.
택트 스위치가 눌렸을때 digitalRead()값은 0이고, 눌리지 않았을 때의 digitalRead()의
*/
pinMode(btnOnePin, INPUT_PULLUP);
pinMode(btnTwoPin, INPUT_PULLUP);
Serial.begin(9600);
}
// the loop function runs over and over again until power down or reset
void loop() {
delay(1000);
byte btnAvailable[] = {digitalRead(btnOnePin), digitalRead(btnTwoPin)};
if(btnAvailable[0] == BTN_PRESSED && btnAvailable[1] == BTN_UNPRESSED&&!motionFlag){
Serial.println("[Hand Mode] FIST");
motionFlag = true;
moveHand_fist();
}
else if(btnAvailable[0] == BTN_UNPRESSED && btnAvailable[1] == BTN_PRESSED&&!motionFlag){
Serial.println("[Hand Mode] COUNT");
motionFlag = true;
moveHand_count();
}
else if (btnAvailable[0] == BTN_PRESSED && btnAvailable[1] == BTN_PRESSED&&!motionFlag){
Serial.println("[Hand Mode] MODE 3");
motionFlag = true;
}
else if(btnAvailable[0] == BTN_UNPRESSED && btnAvailable[1] == BTN_UNPRESSED){
Serial.println("[Hand Mode] UNPRESSED");
motionFlag = false;
}
Serial.println("Motor Loop Begins");
for(double deg=0;deg<70;deg+=0.2){
Serial.println(deg);
//thumbMotor.write(deg);
indexMotor.write(deg);
middleMotor.write(deg);
ringMotor.write(deg);
littleMotor.write(deg);
}
for(double deg=70;deg>=0;deg-=0.2){
Serial.println(deg);
//thumbMotor.write(deg);
indexMotor.write(deg);
middleMotor.write(deg);
ringMotor.write(deg);
littleMotor.write(deg);
}
Serial.println("Motor Loop Ends");
}
void moveHand_fist(){
Serial.println("Motion : <Fist> has started.");
for(float deg=0;deg<180;deg+=0.2f){
if(motor_angle_list[0]<=deg){
motor_list[0].write(deg);
}
if(motor_angle_list[1]<=deg){
motor_list[1].write(deg);
}
if(motor_angle_list[2]<=deg){
motor_list[2].write(deg);
}
if(motor_angle_list[3]<=deg){
motor_list[3].write(deg);
}
if(motor_angle_list[4]<=deg){
motor_list[4].write(deg);
}
}
for(float deg=0;deg<180;deg+=0.2f){
if(motor_angle_list[0]<=deg){
motor_list[0].write(deg);
}
if(motor_angle_list[1]<=deg){
motor_list[1].write(deg);
}
if(motor_angle_list[2]<=deg){
motor_list[2].write(deg);
}
if(motor_angle_list[3]<=deg){
motor_list[3].write(deg);
}
if(motor_angle_list[4]<=deg){
motor_list[4].write(deg);
}
}
Serial.println("Motion : <Fist> has ended.");
motionFlag==false;
}
void moveHand_count(){
Serial.println("Motion : <Counting Numbers> has started.");
for(int i=0;i<5;i++){
for(float deg=0;deg<motor_angle_list[i];deg+=0.2f){
motor_list[i].write(deg);
}
}
for(int i=0;i<5;i++){
for(float deg=motor_angle_list[4-i];deg>0;deg-=0.2f){
motor_list[i].write(deg);
}
}
Serial.println("Motion : <Counting Numbers> has ended.");
motionFlag==false;
}
void servo_angle_debugger(Servo motor, int num){
for(int deg = 0; deg <180; deg+=num){
Serial.print("Starting Motor Test");
Serial.print(" servo degree : ");
Serial.println(deg);
motor.write(deg);
delay(20);
Serial.println("Do you want to quit debugging? Y/N : ");
delay(1000);
char cmd = Serial.read();
if(cmd == 'y'){
Serial.print("Servor Motor [");
Serial.print("] debugging ended. Final result : ");
Serial.println(deg);
return;
}
else{
continue;
}
delay(1000);
}
Serial.print("Servor Motor [");
Serial.print("] debugging ended. Final result : ");
Serial.println(180);
}