Arduinos and similar compatible boards are among the most popular devices for DIYers. Whether you’re a beginner just getting started with Arduino or someone who already uses them in their life, they provide a platform for countless amazing projects.

Today we’ll be exploring a creative way to control a servo with processing and an Xbox360 controller. If you’re already well versed in game development, you might be interested in our custom game controller guide that uses Unity.

This tutorial assumes a bit of prior knowledge: if this is your first time with Arduino, you may find our Arduino guide here helpful. Similarly, if this is your first time using Java, it can be a little confusing. While processing uses a simplified version of the platform, these Java concepts and tips can still help.

What you need

control robots with game controller and arduino

  • 1 x Arduino. We use the UN today.
  • 1 x hobby servo. Anything that will work with Arduino pins.
  • 1 x Xbox360 wired controller. Although technically it will work with almost any controller.
  • Several connecting wires.

In addition to these things, you also need to download the Processing and Arduino IDE from their respective websites.

Preparing the Arduino

control robots with game controller and arduino

First we need to attach our servo. The color of the wiring here may vary, but as a rule, Red color connected to the 5 V pin, and brown or black — to the conclusion GND . The string of data that is usually yellow or orange color joins conclusion 10 .

Check wiring and connect Arduino to computer. Open Arduino IDE.

control robots with game controller and arduino

Open the StandardFirmata thumbnail located in File > Examples > Firmata > StandardFirmata . This sketch sets up the board for external serial port control, and it’s the same one we used in our article on controlling an Arduino with Python. control arduino with python. control Arduino with Upload the sketch to the board.

If the download fails, make sure you have selected the correct board and port information in the menu Service .

Our Arduino is ready to go!

Processing setup

Open the process, you will be greeted with a blank thumbnail. Before doing anything here, we need to install a few libraries. Go to menu » Sketch» and select » Import Library» > «Add Library» . This will cause content manager, which will look familiar to all Arduino users.

control robots with game controller and arduino

We need to install three libraries to make this work. First of all, it’s a library Game Control Plus . This is what will allow us to use our game controller with Processing. Use the search box to find it and click «Install» in the bottom right corner. Game Control Plus needs another library for its tweak tool, so let’s go now. Find a library G4P and install it too.

Finally, we need a library Arduino (firmata) . You guessed it, find it and click install. With these settings, we are ready to start testing that everything will work. We’re running Windows 10 today, but processing is available for most platforms, including the Raspberry Pi. Imagine the possibilities!

Arduino Testing

Before we dive into creating our own sketch, let’s check out Arduino and Servo with processing. Open » File»>»Examples» and select » ArduinoServo» in the folder » Contributed Libraries / Arduino (firmata) «. We’ll use this to test our servo, but first we might need to change a couple of things.

Scroll down the thumbnail and find this line:


If it’s commented out, remove the two forward slashes println(Arduino.list()); and save the sketch. Launch it by clicking on the play icon and keep an eye on the console at the bottom. This will list everything connected to your COM- ports.

control robots with game controller and arduino

In my case, my Arduino was on COM 8 which was the third port listed here. This is important because the code on the line below has array whose value determines which COM port to use.

control robots with game controller and arduino

We need to change this to reflect our COM port. For me it was the third position or index #2:

 arduino = new Arduino(this, Arduino.list()[2], 57600); 

We need to make a couple of other small changes to this code in order to test it. Scroll down to where the Arduino pins are and comment out one of the lines here. Replace another with pin 10 .

 //arduino.pinMode(4, Arduino.SERVO); arduino.pinMode(10, Arduino.SERVO); 

We need to do the same in the method Draw() :

 arduino.servoWrite(10, constrain(mouseX / 2, 0, 180)); // arduino.servoWrite(4, constrain(180 - mouseX / 2, 0, 180)); 

Save the sketch and run it. You should be able to move the servo by moving the mouse back and forth on the window that the program generates.

control robots with game controller and arduino

If this doesn’t work for you, check the servo wiring and make sure you have the correct array position for your COM port. Once you know the Arduino is good at talking to Processing, it’s time to move on.

Controller Configuration

The Game Control Plus library we use also has a powerful configuration. Make sure your controller is connected, open the sample project Configurator and run it. You will get this menu:

control robots with game controller and arduino

Click on the name of your controller and a much larger configuration window will appear.

control robots with game controller and arduino

This may look quite intimidating, but it should be as simple as possible. On the left side fill in the first key with the name you want as a variable. This variable will control the position of the servo so I will call it servo .

In the box next to it, you can give a brief description of what it does. Now take your controller and move the stick you want to use with your servo. A little experiment shows that the right thumb corresponds to the X turn block. Drag a line between a variable servoPos and this field.

control robots with game controller and arduino

Now we need to save our configuration as a data file. In the upper right corner of the window, fill in the field » Device Role» and field » File name» .

The filename is important as you will be using it in your code. I keep it simple by calling it xbs . Click Confirm, then Save . This writes a file with instructions for our controller that we can use later.

control robots with game controller and arduino

Preparing a Custom Thumbnail Folder

Let’s set up our working folder. Open an empty processing sketch and save it with any name. This will create a directory for it in the save location.

Now go to Docs / Processing / Libraries / GameControlPlus / examples / Configurator and copy the folder labeled data . This folder contains the configuration file we just created. Change to the directory of the blank thumbnail you just saved and paste the data folder.

control robots with game controller and arduino

Create your own sketch

Now everything is in place and we can start sketching using our two libraries. We’ll go through this step by step, but you can download the full sketch and data folder if you want to get ahead of yourself. Note that you may still need to change the code to reflect your Arduino COM ports.

Start by importing all the libraries we need:

 import processing.serial.*; import*; import org.gamecontrolplus.*; import org.gamecontrolplus.gui.*; import cc.arduino.*; import org.firmata.*; 

We also need to declare our ControlDevice , I/O and Arduino together with floating point to store values ​​from our thumb:

 ControlDevice cont; ControlIO control; Arduino arduino; float thumb; 

Our method setup() creates a small window, an instance of the controller, and maps the device to our configuration file. The important thing here is to get the correct filename of our configuration data file:

 void setup() { size(360, 200); control = ControlIO.getInstance(this); cont = control.getMatchedDevice("xbs"); if (cont == null) { println("not today chump"); // write better exit statements than me System.exit(-1); } // println(Arduino.list()); arduino = new Arduino(this, Arduino.list()[2], 57600); arduino.pinMode(10, Arduino.SERVO); } 

We also check if there is a corresponding controller at this stage and exit the program if necessary. Although the window created with size() not needed, it will give us some feedback on whether we are getting useful values ​​from our controller. We also initialize our Arduino and attach it here, as we did during testing.

Now we’ll create a small method to get an input value from our controller and map it to values ​​that our servo will be able to use:

 public void getUserInput() { thumb = map(cont.getSlider("servoPos").getValue(), -1, 1, 0, 180); } 

This line of code uses our data file to get our named servo element control that is associated with the controller’s right icon and read values ​​from it. It then displays the values ​​and stores the value in our floating point variable.

Right now this code is never called, we will fix it now.

 void draw() { getUserInput(); background(thumb,100,255); arduino.servoWrite(10, (int)thumb); } 

Draw() similar to method loop() in Arduino IDE. Each frame calls a method getUserInput() and update the value thumb . It uses this value to change the red value background() that gives us a visual indicator that the value has changed. It then writes that value to the servo using the function arduino.servoWrite() . Please note that we must bring thumb as an integer value, because the servoWrite function takes two integers (pin number and angle) as arguments.

Check the code for errors, save it and click Run. After a short delay in the initialization of the Arduino, it should look like this:

control robots with game controller and arduino

Control with game controller and Arduino: done!

This project was in many ways quite advanced for an inexperienced programmer. despite the fantastic libraries that can help us. It is a new way to control robots and any other devices you create.

This project would go great with our laser tower tutorial. giving you complete control over it. You can set up a piezo buzzer like in our Simple Arduino Alarm tutorial and use your controller to change the height of the buzzer or the color of the lights.

Or you could, well, build a massive robot and take over the earth. As long as you’ve had the USB cord long enough!

Похожие записи