-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_Joystick_Controller_Code.ino
161 lines (128 loc) · 3.25 KB
/
Final_Joystick_Controller_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
158
159
160
161
#include "U8glib.h"
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <SoftwareSerial.h>
U8GLIB_SH1106_128X64 oled(U8G_I2C_OPT_NONE);
int joystickX1 = A0; // Analog pin for joystick 1 X axis
int joystickY1 = A1; // Analog pin for joystick 1 Y axis
int joystickX2 = A2; // Analog pin for joystick 2 X axis
int joystickY2 = A3; // Analog pin for joystick 2 Y axis
int x1Value, y1Value, x2Value, y2Value;
char state = 0;
int response_time = 2;
MPU6050 mpu6050(Wire);
void setup(void) {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
Wire.begin();
mpu6050.calcGyroOffsets(true);
delay(2000);
}
void loop(void) {
x1Value = analogRead(joystickX1);
y1Value = analogRead(joystickY1);
x2Value = analogRead(joystickX2);
y2Value = analogRead(joystickY2);
mpu6050.update();
// For Debugging
// Serial.print("x1: ");
// Serial.print(x1Value);
// Serial.print("\t");
// Serial.print("y1: ");
// Serial.print(y1Value);
// Serial.print("\t");
// Serial.print("x2: ");
// Serial.print(x2Value);
// Serial.print("\t");
// Serial.print("y2: ");
// Serial.print(y2Value);
// Serial.println();
float xAngle = mpu6050.getAngleX();
float yAngle = mpu6050.getAngleY();
// Serial.print("angleX : ");Serial.print(mpu6050.getAngleX());
// Serial.print(" angleY : ");Serial.print(mpu6050.getAngleY());
// Serial.print(" angleZ : ");Serial.println(mpu6050.getAngleZ());
// Move Left
if (yAngle > 25.0) {
// Serial.print("L");
state = 'L';
delay(response_time);
}
//Move Right
if ( yAngle < -25.0) {
// Serial.print("R");
state = 'R';
delay(response_time);
}
// //Claw Up
if (xAngle > 25.0) {
// Serial.print("G");
state = 'G';
delay(response_time);
}
// //Claw Down
if ( xAngle < -25.0) {
// Serial.print("U");
state = 'U';
delay(response_time);
}
// // //Move right
if (x1Value > 650) {
// Serial.print("C");
state = 'C';
delay(response_time);
}
// //Move left --- Right Hand
if ( x1Value < 350) {
// Serial.print("c");
state = 'c';
delay(response_time);
}
if (y2Value > 650) {
// Serial.print("P");
state = 'P';
delay(response_time);
}
if (y2Value < 350 ) {
// Serial.print("p");
state = 'p';
delay(response_time);
}
// // finger 1 - Claw Bend/Open
if (y1Value < 350) {
// Serial.print("F");
state = 'F'; // open gripper
delay(response_time);
}
if (y1Value > 650) {
// Serial.print("f");
state = 'f'; // close gripper
delay(response_time);
}
// // thumb 1 - thumb (Base Rotation)
if (x2Value < 350 ) {
// Serial.print("T");
state = 'T';// stepper left
delay(response_time);
}
if (x2Value > 650) {
// Serial.print("t");
state = 't'; // stepper right
delay(response_time);
}
Serial.println(state);
// page1();
updateDisplay();
state = 'X';
// Update the OLED display with the state
// delay(50);
}
void updateDisplay() {
oled.firstPage();
do {
oled.setFont(u8g_font_osr29r);
oled.setPrintPos(50, 50);
oled.print(state);
} while (oled.nextPage());
}