หน้าเว็บ

วันอาทิตย์ที่ 19 พฤษภาคม พ.ศ. 2562

Code โปรเจค Arduino เปิดปิดไฟ ด้วย Wireless Joystick


PS2 Joystick Playstation Adapter for Arduino อะแดปเตอร์แปลงหัว PS2 เป็นขาต่อแบบ DIP สำหรับ Arduino คอนเนคเตอร์สำหรับแปลงจาก PS2 เป็นขา DIP  สำหรับต่อสายเข้ากับไมโครคอนโทรลเลอร์ สำหรับใช้ในการควบคุมหุ่นยนต์


อุปกรณ์ที่ต้องใช้ก็คือ


1. Arduino UNO R3 - Made in italy

2. Wireless Joystick Playstation PS2 Controlle (จอยไร้สาย)

3. PS2 Joystick Converter Adapter for Arduino (อะแดปเตอร์แปลงหัว)

4. Jumper (F2M) cable wire 40pcs 2.54mm 20cm Female to Male

5. Relay 4 Channel 5V DC Solid State High Level Trigger

6. Jumper (M2M) cable wire 40pcs 2.54mm 20cm Male to Male




1. เชื่อมต่อบอร์ด Arduino กับ PS2 Adapter

  • PS2 Adapter              Arduino Board
  •  
  •          DAT                       D 13
  •  
  •          CMD                      D 11 
  •  
  •          GND                      GND
  •  
  •          +5V                       +5V
  •  
  •          SEL                       D 10
  •  
  •          CLK                       D 12





 สายสีดำ GND <-> GND

สายสีแดง +5V <-> +5V



สายสีเขียว DAT   <->   D 13

สายสีเหลือง CMD <-> D 11

สายสีน้ำเงิน SEL <-> D 10

สายสีม่วง  CLK <-> D 12




Receiver ของ Wireless Joystick Playstation PS2





ประกอบ Receiver เข้า กับ อะแดปเตอร์แปลงหัว





2. ติดตั้ง Arduino (IDE) เวอร์ชั่น 1.6.5



เนื่องจาก ไลบรารี่ PS2X สำหรับ  Wireless Joystick (จอยสติ๊กไร้สาย) ในตัวอย่างบทความ เป็นไลบรารี่ ที่ออกแบบสำหรับ Arduino (IDE) เวอร์ชั่น 1.6

ดังจึงแนะนำให้เลือกใช้ Arduino (IDE) เวอร์ชั่น 1.6.5 ซึ่งเป็นเวอร์ชั่นเก่า


2.1 ติดตั้ง Arduino (IDE) เวอร์ชั่น 1.6.5 ตามตัวอย่างในบทความ ให้เลือก วิธีที่ 2

คือ 2.ลงแบบไฟล์บีบอัด (Unzip)


https://robotsiam.blogspot.com/2016/08/arduino-ide.html



2.2 ทดสอบ โปรแกรมแรก Hello World กับ Arduino UNO R3


https://robotsiam.blogspot.com/2016/08/hello-world-arduino-uno-r3.html



3. ติดตั้ง ไลบรารี่ PS2X 


ไลบรารี่ PS2X เป็น ไลบรารี่ ฟังก์ชัน ที่มีผู้พัฒนาเตรียมพร้อมไว้ให้เราแล้ว

โดยให้ไปดาวน์โหลด ไลบรารี่ PS2X ได้ที่



 Download library 


 หรือ

3.1 Unzip ไฟล์ที่ดาวน์โหลดมา PS2X_lib และติดตั้ง ไลบรารี่  
PS2X ลงในโปรแกรม Arduino (IDE) เวอร์ชั่น 1.6.5

3.2 ตรวจสอบว่า ไลบรารี่  PS2X ติดตั้งแล้วหรือยังโดยไปที่ Sketch -> Include Library ต้องพบ PS2X_lib






4. อัพโหลดโค้ด 


#include <PS2X_lib.h>  //for v1.6


/******************************************************************
   set pins connected to PS2 controller:
     - 1e column: original
     - 2e colmun: Stef?
   replace pin numbers by the ones you use
 ******************************************************************/
#define PS2_DAT        13
#define PS2_CMD        11
#define PS2_SEL        10 
#define PS2_CLK        12 

int CH1 = 9;
int CH2 = 8;
int CH3 = 7;
int CH4 = 6;

/******************************************************************
   select modes of PS2 controller:
     - pressures = analog reading of push-butttons
     - rumble    = motor rumbling
   uncomment 1 of the lines for each mode selection
 ******************************************************************/
//#define pressures   true
#define pressures   false
//#define rumble      true
#define rumble      false

PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you connect the controller,
//or call config_gamepad(pins) again after connecting the controller.

int error = 0;
byte type = 0;
byte vibrate = 0;

void setup() {

  pinMode(CH1, OUTPUT);
  pinMode(CH2, OUTPUT);
  pinMode(CH3, OUTPUT);
  pinMode(CH4, OUTPUT);

  Serial.begin(57600);

  delay(300);  //added delay to give wireless ps2 module some time to startup, before configuring it

  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************

  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  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. ");

  //  Serial.print(ps2x.Analog(1), HEX);

  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() {
  /* You must Read Gamepad to get new values and set vibration values
     ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
     if you don't enable the rumble, use ps2x.read_gamepad(); with no values
     You should call this at least once a second
  */
  if (error == 1) //skip loop if no controller found
    return;

  if (type == 2) { //Guitar Hero Controller
    ps2x.read_gamepad();          //read controller

    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))         //will be TRUE as long as button is pressed
      Serial.println("Up Strum");
    if (ps2x.Button(DOWN_STRUM))
      Serial.println("DOWN Strum");
    if (ps2x.Button(PSB_START))        //will be TRUE as long as button is pressed
      Serial.println("Start is being held");
    if (ps2x.Button(PSB_SELECT))
      Serial.println("Select is being held");

    if (ps2x.Button(ORANGE_FRET)) {    // print stick value IF TRUE
      Serial.print("Wammy Bar Position:");
      Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
    }
  }
  else { //DualShock Controller
    ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed

    if (ps2x.Button(PSB_START)) {        //will be TRUE as long as button is pressed
      Serial.println("Start is being held");
      digitalWrite(CH1, LOW);
      digitalWrite(CH2, LOW);
      digitalWrite(CH3, LOW);
      digitalWrite(CH4, LOW);
    }
    if (ps2x.Button(PSB_SELECT)) {
      Serial.println("Select is being held");
      digitalWrite(CH1, HIGH);
      digitalWrite(CH2, HIGH);
      digitalWrite(CH3, HIGH);
      digitalWrite(CH4, HIGH);
    }
    if (ps2x.Button(PSB_PAD_UP)) {     //will be TRUE as long as button is pressed
      Serial.print("Up held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
      digitalWrite(CH2, HIGH);
    }
    if (ps2x.Button(PSB_PAD_RIGHT)) {
      Serial.print("Right held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
      digitalWrite(CH3, HIGH);
    }
    if (ps2x.Button(PSB_PAD_LEFT)) {
      Serial.print("LEFT held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
      digitalWrite(CH1, HIGH);
    }
    if (ps2x.Button(PSB_PAD_DOWN)) {
      Serial.print("DOWN held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
      digitalWrite(CH4, HIGH);
    }

    vibrate = ps2x.Analog(PSAB_CROSS);  //this will set the large motor vibrate speed based on how hard you press the blue (X) button
    if (ps2x.NewButtonState()) {        //will be TRUE if any button changes state (on to off, or off to on)
      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))              //will be TRUE if button was JUST pressed
      Serial.println("Circle just pressed");
    if (ps2x.NewButtonState(PSB_CROSS))              //will be TRUE if button was JUST pressed OR released
      Serial.println("X just changed");
    if (ps2x.ButtonReleased(PSB_SQUARE))             //will be TRUE if button was JUST released
      Serial.println("Square just released");

    if (ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
      Serial.print("Stick Values:");
      Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
      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);
    }
  }
  delay(50);

}



5. ทดสอบการทำงาน

5.1 หลังจากนั้นสังเกตที่ Receiver   ที่ไฟแสดงผล LED
ไฟสีแดงจะติด  และ ไฟสีเขียวกระพริบ แสดงว่าพร้อมใช้งาน 





5.2 ใส่ถ่าน ขนาด AAA จำนวน 2 ก้อน ที่ Wireless Joystick Playstation PS2



5.3 เปิดสวิตช์ ของ  Wireless Joystick Playstation PS2 ไปที่ ON  สังเกตที่ Receiver ไฟสีเขียวที่กระพริบ จะเปลี่ยนเป็นสีเขียวไม่กระพริบ แสดงว่าพร้อมใช้งาน 



5.4 เปิดหน้าต่าง Serial Monitor มุมขวาล่าง ของ Serial Monitor เลือก 57600 baud  คือ ตั้งค่าความเร็วในการรับ-ส่งข้อมูล  คือ 57600




5.5 กดคีย์สวิตซ์ต่างๆที่ Wireless Joystick จะแสดงค่าคีย์ที่ถูกกด แสดงว่า Wireless Joystick Playstation PS2 ของเราเชื่อมต่อ และ พร้อมใช้งานแล้ว






6. เชื่อมต่อ Arduino กับ Relay 4 Channel





ต่อบอร์ด Arduino กับ Relay ดังนี้

  • Relay  4 Channel          Arduino Board
  •  
  •          DC+                       5V
  •  
  •          DC-                        GND 
  •  
  •          CH1                       D 9
  •  
  •          CH2                       D 8
  •  
  •          CH3                       D 7
  •  
  •          CH4                       D 6

หมายเหตุ : Relay ใช้ไฟ 5V จุดเดียวกับ PS2 Adapter จำเป็น ต้อง บัดกรีหรือเชื่อมต่อทั้ง 2 สาย ร่วมกันก่อน แล้วจึงต่อลงไปที่ 5V ของ Arduino อีกครั้ง




7. ทดสอบการทำงาน




กด LEFT :  CH1 ทำงาน (ไฟ LED สีแดงติด)

กด UP :  CH2 ทำงาน (ไฟ LED สีแดงติด)

กด RIGHT :  CH3 ทำงาน (ไฟ LED สีแดงติด)

กด DOWN :  CH4 ทำงาน (ไฟ LED สีแดงติด)

กด START :  CH1 - 4 ปิดทั้งหมด (ไฟ LED สีแดงดับ)

กด SELECT :  CH1 - 4 เปิดทั้งหมด (ไฟ LED สีแดงติด)


8. เชื่อมต่อ Relay 4 Channel กับ หลอดไฟ






ไม่มีความคิดเห็น:

แสดงความคิดเห็น