Arduino Robot Joystick Dengan Tombol Analog
Gambar 1 Aturan besar Koordinat tombol joystick analog.
Gambar diatas merupakan cara kerja dari analog stick yang terdiri dari 2 sumbu X dan Y pada sumbu Y terdapat nilai minimum 0, 128 sebagai nilai tengah dan 255 sebagai nilai maksimal sehingga apabila dalam posisi netral maka stick analog bernilai 128, 128 yang mana penerapannya pada motor robot yang akan kita buat pada posisi ini robot akan diam (tidak bergerak) sedangkan untuk membuat motor maju maka koordinat X < dari 128 dan untuk bergerak mundur nilai koordinat X > dari 128 sedang untuk bergerak ke kiri maka nilai koordinat Y < 128 dan untuk berbelok kekanan nilai koordinat Y > dari 128 untuk lebih jelas perhatikan pada gambar yang diarsir dengan warna.
Berdasarkan gambar diatas dapat diartikan seperti berikut :
BACA JUGA Versi lengkap TUTORIAL ROBOT JOYSTICK ARDUINO
1. Motor bergerak maju apabila stick berada pada koordinat berwarna Merah
2. Motor bergerak mundur apabila stick berada pada koordinat berwarna Kuning
3. Motor berbelok ke kanan apabila stick berada pada koordinat berwarna Biru
4. Motor berbelok ke kiri apabila stick berada pada koordinat berwarna Hijau
5. Motor Berhenti apabila stick berada pada koordinat berwarna Putih
Pergerakan stick dapat diterjemahkan kedalam koordinat dan angka yang kurang lebihnya seperti ini :
1. Untuk maju maka : Y < 64 dan X di antara 64 dan 192
2. Untuk mundur maka: Y > 192 dan X di antara 64 dan 192
3. Untuk kanan maka : X > 192 dan Y di antara 64 dan 192
4. Untuk kiri maka : X < 64 dan Y di antara 64 dan 192
Gambar 2 Pengambilan data tombol analog joystick
|
Program Robot Joystick dengan Tombol Analog
#include <PS2X_lib.h> //for v1.6
#define PS2_DAT 13 //14
#define PS2_CMD 12 //15
#define PS2_SEL 11 //16
#define PS2_CLK 10 //17
#define pressures false
#define rumble false
PS2X ps2x;
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup(){
Serial.begin(57600);
delay(300);
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if(error == 0){
Serial.print("Found Controller, configured successful ");
Serial.print("pressures = ");
if (pressures)
Serial.println("true ");
else
Serial.println("false");
Serial.print("rumble = ");
if (rumble)
Serial.println("true)");
else
Serial.println("false");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
}
else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
else if(error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType();
switch(type) {
case 0:
Serial.print("Unknown Controller type found ");
break;
case 1:
Serial.print("DualShock Controller found ");
break;
case 2:
Serial.print("GuitarHero Controller found ");
break;
case 3:
Serial.print("Wireless Sony DualShock Controller found ");
break;
}
}
void loop() {
if(error == 1)
return;
if(type == 2){
ps2x.read_gamepad();
if(ps2x.ButtonPressed(GREEN_FRET))
Serial.println("Green Fret Pressed");
if(ps2x.ButtonPressed(RED_FRET))
Serial.println("Red Fret Pressed");
if(ps2x.ButtonPressed(YELLOW_FRET))
Serial.println("Yellow Fret Pressed");
if(ps2x.ButtonPressed(BLUE_FRET))
Serial.println("Blue Fret Pressed");
if(ps2x.ButtonPressed(ORANGE_FRET))
Serial.println("Orange Fret Pressed");
if(ps2x.ButtonPressed(STAR_POWER))
Serial.println("Star Power Command");
if(ps2x.Button(UP_STRUM))
Serial.println("Up Strum");
if(ps2x.Button(DOWN_STRUM))
Serial.println("DOWN Strum");
if(ps2x.Button(PSB_START))
Serial.println("Start is being held");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if(ps2x.Button(ORANGE_FRET)) {
Serial.print("Wammy Bar Position:");
Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
}
}
else { //DualShock Controller
ps2x.read_gamepad(false, vibrate);
if(ps2x.Button(PSB_START))
Serial.println("Start is being held");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if(ps2x.Button(PSB_PAD_UP)) { // ROBOT MAJU
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
digitalWrite(7, LOW); //Sesuaikan dengan Pin Motor Driver yang ke Arduino Uno
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if(ps2x.Button(PSB_PAD_RIGHT)){
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else if(ps2x.Button(PSB_PAD_LEFT)){
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if(ps2x.Button(PSB_PAD_DOWN)){
Serial.print("DOWN held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else {
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
if(ps2x.Button(PSB_L2)){ //ROBOT CELEBRATION MAJU MUNDUR
Serial.println("L2 pressed");
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
delay(500);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else if(ps2x.Button(PSB_R2)){ //ROBOT CELEBRATION KANAN KIRI
Serial.println("R2 pressed");
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if(ps2x.Button(PSB_TRIANGLE)){ //TOMBOL SEGITIGA
Serial.println("Triangle pressed");
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if(ps2x.Button(PSB_CIRCLE)) { //TOMBOL BULAT
Serial.println("Circle just pressed");
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else if(ps2x.Button(PSB_SQUARE)){ //TOMBOL KOTAK
Serial.println("Square just released");
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if(ps2x.Button(PSB_CROSS)) { //TOMBOL X
Serial.println("X just changed");
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
////TOMBOL ANALOG PS2 KANAN
if((ps2x.Analog(PSS_LY) == 127) && (ps2x.Analog(PSS_LX) == 128) && (ps2x.Analog(PSS_RY) == 0) && (ps2x.Analog(PSS_RX) == 128))
{
digitalWrite(7, LOW); //MAJU
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if((ps2x.Analog(PSS_LY) == 127) && (ps2x.Analog(PSS_LX) == 128) && (ps2x.Analog(PSS_RY) == 127) && (ps2x.Analog(PSS_RX) == 255))
{
digitalWrite(7, LOW); //KANAN
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else if((ps2x.Analog(PSS_LY) == 127) && (ps2x.Analog(PSS_LX) == 128) && (ps2x.Analog(PSS_RY) == 127) && (ps2x.Analog(PSS_RX) == 0))
{
digitalWrite(7, HIGH); //KIRI
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if((ps2x.Analog(PSS_LY) == 127) && (ps2x.Analog(PSS_LX) == 128) && (ps2x.Analog(PSS_RY) == 255) && (ps2x.Analog(PSS_RX) == 128))
{
digitalWrite(7, HIGH); //MUNDUR
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
vibrate = ps2x.Analog(PSAB_CROSS);
if (ps2x.NewButtonState()) {
if(ps2x.Button(PSB_L3))
Serial.println("L3 pressed");
if(ps2x.Button(PSB_R3))
Serial.println("R3 pressed");
if(ps2x.Button(PSB_L2))
Serial.println("L2 pressed");
if(ps2x.Button(PSB_R2))
Serial.println("R2 pressed");
if(ps2x.Button(PSB_TRIANGLE))
Serial.println("Triangle pressed");
}
if(ps2x.ButtonPressed(PSB_CIRCLE))
Serial.println("Circle just pressed");
if(ps2x.NewButtonState(PSB_CROSS))
Serial.println("X just changed");
if(ps2x.ButtonReleased(PSB_SQUARE))
Serial.println("Square just released");
if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) {
Serial.print("Stick Values:");
Serial.print(ps2x.Analog(PSS_LY), DEC);
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LX), DEC);
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC);
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC);
}
////TOMBOL ANALOG PS2 KIRI
if((ps2x.Analog(PSS_LY) == 0) && (ps2x.Analog(PSS_LX) == 128) && (ps2x.Analog(PSS_RY) == 127) && (ps2x.Analog(PSS_RX) == 128))
{
digitalWrite(7, LOW); //MAJU
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if((ps2x.Analog(PSS_LY) == 127) && (ps2x.Analog(PSS_LX) == 255) && (ps2x.Analog(PSS_RY) == 127) && (ps2x.Analog(PSS_RX) == 128))
{
digitalWrite(7, LOW); //KANAN
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
else if((ps2x.Analog(PSS_LY) == 127) && (ps2x.Analog(PSS_LX) == 0) && (ps2x.Analog(PSS_RY) == 127) && (ps2x.Analog(PSS_RX) == 128))
{
digitalWrite(7, HIGH); //KIRI
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
}
else if((ps2x.Analog(PSS_LY) == 255) && (ps2x.Analog(PSS_LX) == 128) && (ps2x.Analog(PSS_RY) == 127) && (ps2x.Analog(PSS_RX) == 128))
{
digitalWrite(7, HIGH); //MUNDUR
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
}
}
delay(50);
}
No comments:
Post a Comment