Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PS2X_lib.h>
- #define PS2_DAT 5 // data
- #define PS2_CMD 2 //command
- #define PS2_SEL 3 // attention
- #define PS2_CLK 4 //clock
- PS2X ps2x; // tạo PS2 điều khiển lớp
- int error = 0;
- byte type = 0;
- byte vibrate = 0;
- //Chẵn là dương thì thuận chiều, lẽ là dương thì ngược chiều
- const byte MotorInput[4] = {3, 9, 10, 11};
- const byte MotorOutput[4] = {5, 8, 12, 13};
- //Pittong - Dùng High,Low
- #define LeftHand 1
- #define RightHand 2
- #define MiddleHand 4
- bool isCloseL = false; //Ktra tay trái đóng hay mở
- bool isCloseR = false;
- bool isCloseM = false;
- bool isCloseBoth = false; //Ktra cả 2 tay đang đóng hay mở
- bool isPress1 = false, isPress2 = false;
- //Ổ bi,dùng digital
- #define obi_1 6
- #define obi_2 7
- //0:tay trai,1:tay phai,2:tay giua,3:dong mo ca 2 tay,4:d/c keo di len, 5:d/c keo di xuong
- boolean buttons[6] = {false};
- //---------------------------------------------------------------------------------------------------------------------------------
- void setup()
- {
- Serial.begin(9600);
- error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);
- if (error == 0) {
- Serial.print("Đã tìm thấy bộ điều khiển ");
- }
- else if (error == 1)
- Serial.println("Không kết nối đc, thử lại...");
- else if (error == 2)
- Serial.println("Bộ điều khiển tìm thấy nhưng không chấp nhận lệnh");
- else if (error == 3)
- Serial.println("Bộ điều khiển từ chối để vào chế độ Pressures ,hoặc ko hỗ trợ");
- type = ps2x.readType();
- switch (type) {
- case 0:
- Serial.println("Tay điều khiển không phù hợp ");
- break;
- case 1:
- Serial.println("Đã tìm thấy DualShock ");
- break;
- }
- a();
- }
- void a()
- {
- error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);
- if (error == 0) {
- Serial.print("Đã tìm thấy bộ điều khiển ");
- }
- else if (error == 1)
- Serial.println("Không kết nối đc, thử lại...");
- else if (error == 2)
- Serial.println("Bộ điều khiển tìm thấy nhưng không chấp nhận lệnh");
- else if (error == 3)
- Serial.println("Bộ điều khiển từ chối để vào chế độ Pressures ,hoặc ko hỗ trợ");
- type = ps2x.readType();
- switch (type) {
- case 0:
- Serial.println("Tay điều khiển không phù hợp ");
- break;
- case 1:
- Serial.println("Đã tìm thấy DualShock ");
- break;
- }
- }
- int Channels[3] = {0};
- long long int sys_start = 0;
- int MAX_SPEED = 255;
- int MIN_SPEED = -MAX_SPEED;
- //=======================================================================================================================================
- void loop() {
- buttonState();
- ProcessMotor2();
- //taygap();
- }
- void buttonState() {
- ps2x.read_gamepad(false, vibrate);
- Channels[0] = map(ps2x.Analog(PSS_LY), 256, 0, -255, 255);
- Channels[1] = map(ps2x.Analog(PSS_LX), 0, 256, -255, 255);
- Channels[2] = map(ps2x.Analog(PSS_RX), 0, 256, -255, 255);
- for (int i = 0; i < 3; i++) {
- if (abs(Channels[i]) <= 100) {
- Channels[i] = 0;
- }
- }
- int sum = 0;
- for (int i = 0; i < 3; i++) {
- sum += abs(Channels[i]);
- }
- sum /= 3;
- if (sum < 20) {
- sys_start = millis();
- }
- Channels[2] /= 3.5;
- if (millis() - sys_start < 400) {
- Channels[2] = (float)(Channels[2] * ((millis() - sys_start + 310)) / 710);
- }
- taygap();
- }
- bool isMove_obi = false;
- void taygap() {
- //ps2x.read_gamepad(false, vibrate);
- /*
- Serial.print(ps2x.Analog(PSS_LY));
- Serial.print(" ");
- Serial.print(ps2x.Analog(PSS_LX));
- Serial.print(" ");
- Serial.print(ps2x.Analog(PSS_RX));
- Serial.println();
- */
- //----------------Ổ bi-------------------------------------------------------------------------
- bool temp = ps2x.Button(PSB_PAD_UP);
- if (temp != buttons[4]) {
- buttons[4] = temp;
- if (temp) {
- digitalWrite(obi_1, HIGH);
- Serial.println("Keo Len");
- } else {
- digitalWrite(obi_1, LOW);
- Serial.println("STOP keo");
- }
- }
- temp = ps2x.Button(PSB_PAD_DOWN);
- if (temp != buttons[5]) {
- buttons[5] = temp;
- if (temp) {
- digitalWrite(obi_2, HIGH);
- Serial.println("Keo Xuong");
- } else {
- digitalWrite(obi_2, LOW);
- Serial.println("Stop keo");
- }
- }
- //------------------Tay Gắp--------------------------------------
- temp = ps2x.Button(PSB_PINK);
- if (temp != buttons[0]) { //Tay Trái
- buttons[0] = temp;
- if (temp) {
- if (!isCloseL) {
- Serial.println("Dong tay trai");
- digitalWrite(13, HIGH);
- digitalWrite(LeftHand, HIGH);
- isCloseL = true;
- } else {
- Serial.print("Mo tay trai");
- digitalWrite(13, LOW);
- digitalWrite(LeftHand, LOW);
- isCloseL = false;
- }
- }
- }
- //-----------------------------------------------
- temp = ps2x.Button(PSB_RED);
- if (temp != buttons[1]) { //Tay Phải
- buttons[1] = temp;
- if (temp) {
- if (!isCloseR) {
- Serial.println("Dong tay phai");
- digitalWrite(RightHand, HIGH);
- isCloseR = true;
- } else {
- Serial.println("Mo tay phai");
- digitalWrite(RightHand, LOW);
- isCloseR = false;
- }
- }
- }
- //-----------------------------------------------
- temp = ps2x.Button(PSB_TRIANGLE);
- if (temp != buttons[2]) { //Tay Giữa
- buttons[2] = temp;
- if (temp) {
- if (!isCloseM) {
- Serial.println("Dong tay giua");
- digitalWrite(MiddleHand, HIGH);
- isCloseM = true;
- } else {
- Serial.println("Mo tay giua");
- digitalWrite(MiddleHand, LOW);
- isCloseM = false;
- }
- }
- }
- //-----------------------------------------------
- temp = ps2x.Button(PSB_BLUE);
- if (temp != buttons[3]) { //Đóng mở 2 tay cùng lúc
- buttons[3] = temp;
- if (temp) {
- if (!isCloseBoth) {
- Serial.println("Dong ca hai tay");
- digitalWrite(LeftHand, HIGH);
- digitalWrite(RightHand, HIGH);
- isCloseL = true;
- isCloseR = true;
- isCloseBoth = true;
- } else {
- Serial.println("Mo ca hai tay");
- digitalWrite(LeftHand, LOW);
- digitalWrite(RightHand, LOW);
- isCloseL = false;
- isCloseR = false;
- isCloseBoth = false;
- }
- }
- }
- }
- void ProcessMotor2() {
- int Motors[4] = {0};
- Motors[0] = Channels[0];
- Motors[1] = Channels[0];
- Motors[2] = Channels[0];
- Motors[3] = Channels[0];
- Motors[0] += Channels[1];
- Motors[1] -= Channels[1];
- Motors[2] -= Channels[1];
- Motors[3] += Channels[1];
- Motors[0] += Channels[2];
- Motors[1] -= Channels[2];
- Motors[2] += Channels[2];
- Motors[3] -= Channels[2];
- //SetSpeed2(Motors);
- }
- int SetSpeed2(int* motors) {
- Constrains(motors);
- //E xoa vong for thi moi chay dc nhu clip, con neu co thi k chay dc
- /*
- for (int i = 0; i < 4; i++) {
- if (motors[i] >= 0) {
- analogWrite(MotorInput[i], motors[i]);
- analogWrite(MotorOutput[i], 0);
- //Serial.print("Thuan");
- //Serial.print(" ");
- } else {
- analogWrite(MotorInput[i], 255 + motors[i]);
- analogWrite(MotorOutput[i], 255);
- //Serial.print("Nghich");
- //Serial.print(" ");
- }
- }
- */
- }
- void Constrains2(int *motors) {
- int TEMP_MAX_SPEED = 0;
- if (millis() - sys_start < 400) {
- TEMP_MAX_SPEED = (float)(MAX_SPEED * ((millis() - sys_start + 800)) / 1200);
- } else {
- TEMP_MAX_SPEED = MAX_SPEED;
- }
- int TEMP_MIN_SPEED = -TEMP_MAX_SPEED;
- /*Gotta make sure every value is vaild*/
- int temp = TEMP_MIN_SPEED;
- for (int i = 0; i < 4; i++) {
- if (motors[i] > temp) {
- temp = motors[i];
- }
- }
- if (temp > TEMP_MAX_SPEED) {
- float ratio = (float)TEMP_MAX_SPEED / temp;
- for (int i = 0; i < 4; i++) {
- motors[i] *= ratio;
- }
- }
- temp = TEMP_MAX_SPEED;
- for (int i = 0; i < 4; i++) {
- if (motors[i] < temp) {
- temp = motors[i];
- }
- }
- if (temp < TEMP_MIN_SPEED) {
- float ratio = (float)TEMP_MIN_SPEED / temp;
- for (int i = 0; i < 4; i++) {
- motors[i] *= ratio;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment