-
Notifications
You must be signed in to change notification settings - Fork 0
/
braco_robotico_varSpeedServo_lib.ino
117 lines (102 loc) · 2.65 KB
/
braco_robotico_varSpeedServo_lib.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
#include <VarSpeedServo.h>
#define ANGULO_INICIAL_MOTOR 90
#define VELOCIDADE_MOTOR 50
int auxBase = ANGULO_INICIAL_MOTOR;
int auxBraco1 = ANGULO_INICIAL_MOTOR;
int auxBraco2 = ANGULO_INICIAL_MOTOR;
int auxGarra = ANGULO_INICIAL_MOTOR;
// --- Mapeamento dos Joysticks ---
#define joystick1X A4
#define joystick1Y A5
#define joystick2X A2
#define joystick2Y A3
// --- Mapeamento dos Servos ---
VarSpeedServo motorBase;
VarSpeedServo motorBraco1;
VarSpeedServo motorBraco2;
VarSpeedServo motorGarra;
// --- Inicialização ---
void setup() {
Serial.begin(9600);
mapearPinosDosMotores();
inicializarAngulosDosMotores();
mapearJoystickComoInput();
}
// --- Loop Infinito ---
void loop() {
moverBase();
moverBraco1();
moverBraco2();
moverGarra();
delay(100);
}
// --- FUNÇÕES AUXILIARES ---
// --- Mapear Pinos dos Servo Motores ---
void mapearPinosDosMotores() {
motorBase.attach(5);
motorBraco1.attach(6);
motorBraco2.attach(9);
motorGarra.attach(10); // entradas com PWM
}
// --- Inicializar Ângulos Iniciais dos Motores ---
void inicializarAngulosDosMotores() {
motorBase.write(ANGULO_INICIAL_MOTOR);
motorBraco1.write(ANGULO_INICIAL_MOTOR);
motorBraco2.write(60);
motorGarra.write(60);
}
// --- Mapear Pinos do Joystick como Input ---
void mapearJoystickComoInput() {
pinMode(joystick1X, INPUT);
pinMode(joystick1Y, INPUT);
pinMode(joystick2X, INPUT);
pinMode(joystick2Y, INPUT);
}
// --- Mover Base ---
void moverBase() {
int pos1X = analogRead(joystick1X);
pos1X = map(pos1X, 0, 1023, 0, 180);
if (pos1X > 100) {
auxBase += 10;
motorBase.write(auxBase, VELOCIDADE_MOTOR);
} else if (pos1X < 80) {
auxBase -= 10;
motorBase.write(auxBase, VELOCIDADE_MOTOR);
}
}
// --- Mover Braco 1 ---
void moverBraco1() {
int pos1Y = analogRead(joystick1Y);
pos1Y = map(pos1Y, 0, 1023, 45, 135);
if (pos1Y > 100) {
auxBraco1 += 10;
motorBraco1.write(auxBraco1, VELOCIDADE_MOTOR);
} else if (pos1Y < 80) {
auxBraco1 -= 10;
motorBraco1.write(auxBraco1, VELOCIDADE_MOTOR);
}
}
// --- Mover Braco 2 ---
void moverBraco2() {
int pos2X = analogRead(joystick2X);
pos2X = map(pos2X, 0, 1023, 0, 180);
if (pos2X > 100) {
auxBraco2 += 10;
motorBraco2.write(auxBraco2, VELOCIDADE_MOTOR);
} else if (pos2X < 80) {
auxBraco2 -= 10;
motorBraco2.write(auxBraco2, VELOCIDADE_MOTOR);
}
}
// --- Mover Garra ---
void moverGarra() {
int pos2Y = analogRead(joystick2Y);
pos2Y = map(pos2Y, 0, 1023, 0, 180);
if (pos2Y > 100) {
auxGarra += 10;
motorGarra.write(auxGarra, VELOCIDADE_MOTOR);
} else if (pos2Y < 80) {
auxGarra -= 10;
motorGarra.write(auxGarra, VELOCIDADE_MOTOR);
}
}