-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboHand.ino
215 lines (195 loc) · 5.87 KB
/
RoboHand.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <Servo.h>
#include <OLED_I2C.h>
//define the servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
OLED myOLED(SDA, SCL);
extern uint8_t SmallFont[];
//define the buttons
const int button1 = 12;
const int button2 = 13;
//define variable for values of the button
int button1Pressed = 0;
boolean button2Pressed = false;
//define potentiometers
const int pot1 = A1;
const int pot2 = A2;
const int pot3 = A3;
const int pot4 = A4;
//define variable for values of the potentiometers
int pot1Val;
int pot2Val;
int pot3Val;
int pot4Val;
//define variable for angles of the potentiometer
int pot1Angle;
int pot2Angle;
int pot3Angle;
int pot4Angle;
//define variable for saved position of the servos
int servo1PosSave[]={1,1,1,1,1,1,1,1};
int servo2PosSave[]={1,1,1,1,1,1,1,1};
int servo3PosSave[]={1,1,1,1,1,1,1,1};
int servo4PosSave[]={1,1,1,1,1,1,1,1};
void setup() {
myOLED.begin();
myOLED.setFont(SmallFont);
Serial.begin(9600);
//define attached pins of the servos
servo1.attach(3);
servo2.attach(4);
servo3.attach(5);
servo4.attach(6);
//define buttons as input units
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("PRESS TO SAVE BUTTON", CENTER, 42);
myOLED.update();
}
void loop() {
//read the potentiometer values and define the servo angle to
//the potentiometer value with the map function
pot1Val = analogRead(pot1);
pot1Angle = map (pot1Val, 0, 1023, 10, 179);
pot2Val = analogRead(pot2);
pot2Angle = map (pot2Val, 0, 1023, 10, 150);
pot3Val = analogRead(pot3);
pot3Angle = map (pot3Val, 0, 1023, 10, 170);
pot4Val = analogRead(pot4);
pot4Angle = map (pot4Val, 0, 1023, 10, 170);
//servos move to mapped angles
servo1.write(pot1Angle);
servo2.write(pot2Angle);
servo3.write(pot3Angle);
servo4.write(pot4Angle);
//if button1 is pressed (HIGH), save the potentiometers position
//as long as button1 is pressed
if(digitalRead(button1) == HIGH){
button1Pressed++;
switch(button1Pressed){
case 1:
servo1PosSave[0] = pot1Angle;
servo2PosSave[0] = pot2Angle;
servo3PosSave[0] = pot3Angle;
servo4PosSave[0] = pot4Angle;
Serial.println("Position #1 Saved");
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("POSITION #1 SAVED", CENTER, 42);
myOLED.update();
delay(1500);
break;
case 2:
servo1PosSave[1] = pot1Angle;
servo2PosSave[1] = pot2Angle;
servo3PosSave[1] = pot3Angle;
servo4PosSave[1] = pot4Angle;
Serial.println("Position #2 Saved");
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("POSITION #2 SAVED", CENTER, 42);
myOLED.update();
delay(1500);
break;
case 3:
servo1PosSave[2] = pot1Angle;
servo2PosSave[2] = pot2Angle;
servo3PosSave[2] = pot3Angle;
servo4PosSave[2] = pot4Angle;
Serial.println("Position #3 Saved");
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("POSITION #3 SAVED", CENTER, 42);
myOLED.update();
delay(1500);
break;
case 4:
servo1PosSave[3] = pot1Angle;
servo2PosSave[3] = pot2Angle;
servo3PosSave[3] = pot3Angle;
servo4PosSave[3] = pot4Angle;
Serial.println("Position #4 Saved");
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("POSITION #4 SAVED", CENTER, 42);
myOLED.update();
delay(1500);
break;
case 5:
servo1PosSave[4] = pot1Angle;
servo2PosSave[4] = pot2Angle;
servo3PosSave[4] = pot3Angle;
servo4PosSave[4] = pot4Angle;
Serial.println("Position #5 Saved");
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("POSITION #5 SAVED", CENTER, 42);
myOLED.update();
delay(1500);
break;
case 6:
servo1PosSave[5] = pot1Angle;
servo2PosSave[5] = pot2Angle;
servo3PosSave[5] = pot3Angle;
servo4PosSave[5] = pot4Angle;
Serial.println("Position #6 Saved");
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("POSITION #6 SAVED", CENTER, 42);
myOLED.update();
delay(1500);
break;
case 7:
servo1PosSave[6] = pot1Angle;
servo2PosSave[6] = pot2Angle;
servo3PosSave[6] = pot3Angle;
servo4PosSave[6] = pot4Angle;
Serial.println("Position #7 Saved");
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("POSITION #7 SAVED", CENTER, 42);
myOLED.update();
delay(1500);
break;
case 8:
servo1PosSave[7] = pot1Angle;
servo2PosSave[7] = pot2Angle;
servo3PosSave[7] = pot3Angle;
servo4PosSave[7] = pot4Angle;
Serial.println("Position #8 Saved");
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("POSITION #8 SAVED", CENTER, 28);
myOLED.print("PRESS TO MOVE BUTTON", CENTER, 42);
myOLED.update();
delay(1500);
break;
}
}
//if button2 pressed (HIGH), the servos move saved position
if(digitalRead(button2) == HIGH){
button2Pressed = true;
}
if(button2Pressed){
for(int i=0; i<8; i++){
servo1.write(servo1PosSave[i]);
servo2.write(servo2PosSave[i]);
servo3.write(servo3PosSave[i]);
servo4.write(servo4PosSave[i]);
myOLED.clrScr();
myOLED.print("Roaring Engineers", CENTER, 8);
myOLED.print("MOVING...", CENTER, 42);
myOLED.update();
delay(2000);
}
}
delay(100);
}