# Trying to get a BLDC w/ ESC to work with my PS4 controller

**URL:** <https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [August 17, 2023, 10:03pm UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612 "2023-08-17T22:03:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkeith](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@bkeith](https://discourse.processing.org/u/bkeith)\
**Post date:** [August 17, 2023, 10:03pm UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612/1 "2023-08-17T22:03:04Z")

</div>

This is my first post here, im running into trouble with my code and im not sure where im going wrong. I want to control a bldc motor with my ps4 controller, the dc motor is a Firma Spektrum 6000kv BLDC. When I run an arduino code I can use the dc motor with a potentiometer just fine, and the code below worked with two regular DC motors hooked up through a mosfet. I think its the ESC thats causing me trouble, im just not sure how to code that. Hopefully someone can tell me where i went wrong, Thanks! The error I get when I run this is:

```auto
=======================================================
Failed to initialize device HIDI2C Device because of: java.io.IOException: Failed to acquire device (8007001e)
NullPointerException

```

```auto
import processing.serial.*;

import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;

import cc.arduino.*;
import org.firmata.*;

ControlDevice cont;
ControlIO control;

Arduino arduino;

//int fbThrusterX = 50; // 0 - 255
int fbThrusterY = 0; 
float thumbY;
//float thumbX;

void setup() {
  //color box repersting range of motion of controller
   size(360, 200);
  
   control = ControlIO.getInstance(this);
   cont = control.getMatchedDevice("thrust");
   
   //if controller is not plugged in or connected
   //this error message will pop up and the program will stop 
   if (cont == null) {
     println("nope");
     System.exit(-1);
  }
  
  //println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  
  for(int i=0; i<= 50; i++)
    arduino.pinMode(3, Arduino.SERVO);
   
}

public void getUserInput(){
  //assign our float value
  //to access controller
  thumbY = map(cont.getSlider("fbThrusterY").getValue(), -1, 1, 0, 100);
  //thumbX = map(cont.getSlider("fbThrusterX").getValue(), -1, 1, 0, 100);
  //println(thumb);
   
}

void draw(){
  getUserInput();
  background(thumbY, 100, 255, 0);
  //background(thumbX, 0, 255, 100);
  
  arduino.analogWrite(3, (int)thumbY);
  //arduino.analogWrite(5, (int)thumbX);
}

```

Heres my latest update after changing the last line,

```auto
arduino.analogwrite(3, (int)thumbY)

```

to

```auto
arduino.servoWrite(3, (int)thumbY)

```

this is the error I get now, which makes even less sense to me.

```auto
Failed to initialize device HIDI2C Device because of: java.io.IOException: Failed to acquire device (8007001e)
Failed to poll device: Device is released
Failed to poll device: Device is released
Failed to poll device: Device is released
Failed to poll device: Device is released
Failed to poll device: Device is released
Failed to poll device: Device is released
Failed to poll device: Device is released
Could not run the sketch (Target VM failed to initialize).
For more information, read Help ? Troubleshooting.

```

Im not sure what the Target VM is, I read through the trouble shooting guide and didnt see anything helpful. Thanks again everyone!

Final update incase others in the future have a similar issue. I got it to work, I just started from scratch. I think what happened is when I initially did this with two micro DC motors I needed to add a few other things, but when a ESC is used like a servo, so I deleted some stuff and sure enough it works! Heres my code if any one is curious:

```auto
import processing.serial.*;

import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;

import cc.arduino.*;
import org.firmata.*;

ControlDevice cont;
ControlIO control;

Arduino arduino;
 
float thumbY;

void setup() {
  
   size(360, 200);
  
   control = ControlIO.getInstance(this);
   cont = control.getMatchedDevice("thrust");
   
   if (cont == null) {
     println("nope");
     System.exit(-1);
   }

  arduino = new Arduino(this, Arduino.list()[0], 57600);
 
    arduino.pinMode(10, Arduino.SERVO);  
}
 public void getUserInput(){
   
  thumbY = map(cont.getSlider("thrust").getValue(), -1, 1, 0, 100);
}

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

```

---

<div class="post-metadata">

**Author:** ![HackinHarry](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@HackinHarry](https://discourse.processing.org/u/HackinHarry)\
**Post date:** [August 18, 2023, 2:19am UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612/2 "2023-08-18T02:19:17Z")

</div>

Hey, NullPointer, cant initialize the device because it can’t find it. Have you tried changing Arduino.list()[0] to something else like Arduino.list()[some other integer]? Just a thought, since I’ve felt your pain before.

---

<div class="post-metadata">

**Author:** ![sterretje](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sterretje](https://discourse.processing.org/u/sterretje)\
**Post date:** [August 18, 2023, 3:42am UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612/3 "2023-08-18T03:42:34Z")

</div>

I’m familiar with Arduinos but I’m not familiar with the additional imports that you use in your code.

`printArray(Serial.list())` will print the list of serial ports to the console so you can check if you have the correct port selected. I’m reasonably sure that you can use `printArray(Arduino.list())` as well.

Instead of

```java
arduino = new Arduino(this, Arduino.list()[0], 57600);

```

you might also be able to use

```java
arduino = new Arduino(this, "COMx", 57600);

```

where the ‘x’ in COMx is the actual port number (e.g. 3).

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 18, 2023, 11:30am UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612/4 "2023-08-18T11:30:24Z")

</div>

Hello @bkeith ,

This came up in a search for error:

> <https://stackoverflow.com/questions/68813615/slick2d-loading-net-java-games-input-directandrawinputenvironmentplugin>

Give it a try and report back to us.

`:)`

---

<div class="post-metadata">

**Author:** ![bkeith](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@bkeith](https://discourse.processing.org/u/bkeith)\
**Post date:** [August 18, 2023, 3:34pm UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612/5 "2023-08-18T15:34:27Z")

</div>

Thanks for the info! I tried changing the 0 to something else like a 1 or 2 but that didnt work, it says that it cant find the arduino. I also tried changing that entire line to COM3 with no success. Im still getting the same error.

---

<div class="post-metadata">

**Author:** ![bkeith](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@bkeith](https://discourse.processing.org/u/bkeith)\
**Post date:** [August 18, 2023, 3:39pm UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612/6 "2023-08-18T15:39:51Z")

</div>

Thanks for this, I cahnged the line of code you suggested and got the same error, I can see in arduino IDE that im using COM3 so I changed it to that. It did take longer to load so I got my hopes up but eventually I got the same error. Ill keep trying!

---

<div class="post-metadata">

**Author:** ![bkeith](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@bkeith](https://discourse.processing.org/u/bkeith)\
**Post date:** [August 18, 2023, 4:04pm UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612/7 "2023-08-18T16:04:09Z")

</div>

So I dont think that worked, however something I just did was delete the code relating to the box I wanted to create in the void setup. Now that only thing i get when I run the code is NullPointerException. No other information is given when I run the code.

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [August 18, 2023, 9:46pm UTC](https://discourse.processing.org/t/trying-to-get-a-bldc-w-esc-to-work-with-my-ps4-controller/42612/8 "2023-08-18T21:46:43Z")

</div>

Hi

Example to use the controller

[![](https://img.youtube.com/vi/MUM8_4mWxng/hqdefault.jpg "Using a Game Controller with Arduino and Processing") ](https://www.youtube.com/watch?v=MUM8_4mWxng)

> **[Quarks Place](http://lagers.org.uk/gamecontrol/index.html)**
>
> G4P and Processing

> <https://stackoverflow.com/questions/65514034/interfacing-arduino-with-xbox-360-controller-game-control-plus>
