-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArm Robot Remote.ino
125 lines (113 loc) · 3.07 KB
/
Arm Robot Remote.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
#define stepPin1 2
#define stepPin2 4
#define dirPin1 5
#define dirPin2 7
#define EN 8
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial blue(A1, A2);
Servo ser_yaw, ser_roll;
String cmd;
int kecc = 250;
char data;
int yaw_ = 90, roll_ = 90;
float yaw = 90, roll = 90;
void move_servo() {
if (data == 'F') stepping(stepPin1, 0, kecc);
else if (data == 'G') do_yaw(0);
else if (data == 'I') do_roll(0);
else if (data == 'H') do_roll(1);
else if (data == 'J') do_yaw(1);
else if (data == 'B') stepping(stepPin1, 1, kecc);
else if (data == 'L') stepping(stepPin2, 0, kecc);
else if (data == 'R') stepping(stepPin2, 1, kecc);
else if (data == 'C') digitalWrite(EN, LOW); //W
else if (data == 'M') digitalWrite(EN, HIGH); //w
// else if (data == 'G') stepping2(1,1,0,1,kecc);
// else if (data == 'I') stepping2(1,1,0,0,kecc);
// else if (data == 'H') stepping2(1,1,1,1,kecc);
// else if (data == 'J') stepping2(1,1,1,1,kecc);
}
void setup() {
// put your setup code here, to run once:
pinMode(stepPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(EN, OUTPUT);
digitalWrite(EN, HIGH);
ser_roll.attach(9);
ser_roll.write(yaw);
ser_yaw.attach(10);
ser_yaw.write(roll);
Serial.begin(9600);
blue.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (blue.available() > 0) {
data = blue.read();
Serial.println(data);
}
move_servo();
// if (cmd == "1") {
// Serial.println("Stepper1 Kanan");
// stepping(stepPin1, 0, kecc);
// }
// else if (cmd == "2") {
// Serial.println("Stepper1 Kiri");
// stepping(stepPin1, 1, kecc);
// }
// else if (cmd == "3") {
// Serial.println("Stepper2 Kanan");
// stepping(stepPin2, 0, kecc);
// }
// else if (cmd == "4") {
// Serial.println("Stepper2 Kiri");
// stepping(stepPin2, 1, kecc);
// }
}
void stepping(int stepPin, bool dir, int kec) {
for (int i = 0; i < 50; i++) {
digitalWrite(stepPin + 3, dir);
digitalWrite(stepPin, HIGH);
delayMicroseconds(kec);
digitalWrite(stepPin, LOW);
delayMicroseconds(kec);
}
// Serial.print(stepPin);
// Serial.print(dir);
// Serial.println(kec);
}
void stepping2(bool step1, bool step2, bool dir1, bool dir2, int kec) {
digitalWrite(dirPin1, dir1);
digitalWrite(dirPin2, dir2);
for (int i = 0; i < 50; i++) {
digitalWrite(stepPin1, step1);
digitalWrite(stepPin2, step2);
delayMicroseconds(kec);
digitalWrite(stepPin1, 0);
digitalWrite(stepPin2, 0);
delayMicroseconds(kec);
}
}
void do_yaw(bool dir_yaw) {
float dyaw = 0.4;
if (dir_yaw == 0)yaw = yaw + dyaw;
else yaw = yaw - dyaw;
if (yaw > 180)yaw = 180;
if (yaw < 0)yaw = 0;
yaw_ = yaw;
ser_yaw.write(yaw_);
Serial.println(yaw_);
}
void do_roll(bool dir_roll) {
float droll = 0.4;
if (dir_roll == 0)roll = roll + droll;
else roll = roll - droll;
if (roll > 180)roll = 180;
if (roll < 0)roll = 0;
roll_ = roll;
ser_roll.write(roll_);
Serial.println(roll_);
}