Home Code Usb Host shield and XBox joypad example

Usb Host shield and XBox joypad example

by shedboy71

In this example we will connect an XBox 360 (USB) joypad to a USB Host shield and flash some LEDs depending on what button was pressed on the joypad

You will need a USB host shield, connect this to an Arduino UNO and then connect 4 sets of LEDs. You will also need an XBox 360 joypad, these are just standard USB joypads

Schematics

Here are some basic schematics and layout.

USB Host Shield and LEDs_schem

USB Host Shield and LEDs_bb

 

Code

You will need to install the https://github.com/felis/USB_Host_Shield_2.0 library. There are already XBox examples, this is an adaptation of one of these

[codesyntax lang=”cpp”]

#include <XBOXUSB.h>

USB Usb;
XBOXUSB Xbox(&Usb);

void setup() 
{
  //Serial.begin(115200);
  //while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  if (Usb.Init() == -1) 
  {
    //Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  //Serial.print(F("\r\nXBOX USB Library Started"));
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

}

void loop() 
{
  Usb.Task();
  digitalWrite(4, LOW); 
  digitalWrite(5, LOW); 
  digitalWrite(6, LOW); 
  digitalWrite(7, LOW); 
  if (Xbox.Xbox360Connected) 
  {
    
    if (Xbox.getButtonClick(A))
    {
      digitalWrite(4, HIGH);
      delay(250);
    }
    if (Xbox.getButtonClick(B))
    {
      digitalWrite(5, HIGH);
      delay(250);
    }
    if (Xbox.getButtonClick(X))
    {
      digitalWrite(6, HIGH);
      delay(250);
    }
    if (Xbox.getButtonClick(Y))
    {
      digitalWrite(7, HIGH);
      delay(250);
    }
  }
  delay(1);
}

[/codesyntax]

Links
USB Host Shield 2 For Arduino ADK Compatible With Google Android ADK

Funduino UNOR3 development board + USB host shield (red)

Share

You may also like