-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_code.ino
182 lines (118 loc) · 3.41 KB
/
arduino_code.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
/*
Enmre SURK
input -> 120,50,50
servolar bu açıya gelir.
*/
#include <Servo.h>
Servo servo1; //pin:8 0 - 140
Servo servo2; //pin:9 50 - 90
Servo servo3; //pin:10 40 - 110
int middle1 = 120;
int middle2 = 50;
int middle3 = 60;
int potan1Pin = 15;
int potan2Pin = 14;
int potan3Pin = 13;
int potans1Value = 0;
int potans2Value = 0;
int potans3Value = 0;
int positions[7][3] = {
{ middle1 , middle2 , middle3 },
{ middle1 + 40 , middle2 , middle3 + 20 },
{ middle1 + 40 , middle2 + 20 , middle3 - 20 },
{ middle1 + 40 , middle2 + 20 , middle3 + 20 },
{ middle1 - 40 , middle2 + 20 , middle3 - 20 },
{ middle1 - 40 , middle2 - 20 , middle3 + 20 },
{ middle1 - 40 , middle2 - 20 , middle3 - 20 }
};
void setup() {
servo1.attach( 8 );
servo2.attach( 9 );
servo3.attach( 10 );
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
axisMove( 0 );
}
void loop() {
//readPotans();
/*
int i = 40;
while(i < 140 ) {
i++;
axisMove( 3 , i );
Serial.println( i );
delay(100);
}*/
while (Serial.available() > 0) {
String cmd = Serial.readString();
char fchar = cmd.charAt(1);
String number = "no";
if ( fchar == ' ' ) {
number = "yes";
//Serial.println( "number sended." );
int servoNumber = getValue( cmd , ' ' , 0).toInt();
int degree = getValue( cmd , ' ' , 1).toInt();
axisMove( servoNumber, degree );
} else {
String pos1str = getValue( cmd , ',' , 0);
String pos2str = getValue( cmd , ',' , 1);
String pos3str = getValue( cmd , ',' , 2);
int pos1 = pos1str.toInt();
int pos2 = pos2str.toInt();
int pos3 = pos3str.toInt();
axisMove( pos1 , pos2 , pos3 );
//Serial.println( " komut : " + pos1str + " ," + pos2str + " ," + pos3str ); // 139,120,55
}
}
}
int readPotans(){
delay(10);
potans1Value = analogRead(potan1Pin);
potans1Value = map( potans1Value , 0 , 1000 , 0 , 180 );
axisMove( 1 , potans1Value );
potans2Value = analogRead(potan2Pin);
potans2Value = map( potans2Value , 0 , 1000 , 50 , 90 );
axisMove( 2 , potans2Value );
potans3Value = analogRead(potan3Pin);
potans3Value = map( potans3Value , 0 , 1000 , 40 , 110 );
axisMove( 3 , potans3Value );
Serial.println( potans3Value );
}
int axisMove( int positionIndex ) {
servo1.write( positions[positionIndex][0] );
servo2.write( positions[positionIndex][1] );
servo3.write( positions[positionIndex][2] );
return 0;
}
int axisMove( int s1 , int s2 , int s3 ) {
servo1.write( s1 );
servo2.write( s2 );
servo3.write( s3 );
return 0;
}
int axisMove( int servoNumber , int degree ) {
if ( servoNumber == 1 ) {
servo1.write(degree);
}
if ( servoNumber == 2 ) {
servo2.write(degree);
}
if ( servoNumber == 3 ) {
servo3.write(degree);
}
}
String getValue(String data, char separator, int index){
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}