-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmeArm-Control.ino
104 lines (84 loc) · 2.48 KB
/
meArm-Control.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
/*============================================================================
meArm Software by Scott Pierce - sales@buildacnc.com - www.buildacnc.com
The following is heavily based on but a complete rewrite of code found here:
http://www.instructables.com/id/MeArm-software/
Pin 5 = Height Servo
Pin 9 = Gripper Servo
Pin 10 = Extension Servo
Pin 11 = Pivot Servo
==============================================================================*/
#include <Servo.h>
#define NUM_SERVOS 4
#define DELAY_TIME 5
byte servoPins[] = {5, 9, 10, 11};
struct ServoStruct
{
Servo servo;
byte pin;
byte currentPosition;
byte lastPosition;
byte minPosition;
byte maxPosition;
byte centerPosition;
byte homePosition;
boolean reverse;
byte idleCycles;
};//struct
ServoStruct Servos[NUM_SERVOS];
void setup()
{
Serial.begin(57600);
Serial.println("Initializing Servos");
for (int i = 0; i < NUM_SERVOS; i++)
{
Servos[i].pin = servoPins[i];
Servos[i].minPosition = 0;
Servos[i].maxPosition = 179;
Servos[i].centerPosition = ((Servos[i].maxPosition - Servos[i].minPosition)/2)+Servos[i].minPosition;
Servos[i].homePosition = Servos[i].centerPosition;
Servos[i].lastPosition = Servos[i].homePosition;
Servos[i].currentPosition = Servos[i].lastPosition;
Servos[i].reverse = true;
Servos[i].idleCycles = 0;
Servos[i].servo.attach(servoPins[i]);
Servos[i].servo.write(Servos[i].homePosition);
}//for
Servos[3].reverse = false;
}//setup
void loop()
{
for (int i = 0; i < NUM_SERVOS; i++)
{
Servos[i].lastPosition = Servos[i].currentPosition;
int potVal = analogRead(i);
if(potVal > 612)
{
if(Servos[i].reverse)
{
if(Servos[i].currentPosition > Servos[i].minPosition)
Servos[i].currentPosition--;
}//if
else
{
if(Servos[i].currentPosition < Servos[i].maxPosition)
Servos[i].currentPosition++;
}//else
}//if
else if (potVal < 412)
{
if(Servos[i].reverse)
{
if(Servos[i].currentPosition < Servos[i].maxPosition)
Servos[i].currentPosition++;
}//if
else
{
if(Servos[i].currentPosition > Servos[i].minPosition)
Servos[i].currentPosition--;
}//else
}//else if
if(Servos[i].currentPosition != Servos[i].lastPosition)
Servos[i].servo.write(Servos[i].currentPosition);
delay(DELAY_TIME);
} //for
}//loop