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

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:

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

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,

arduino.analogwrite(3, (int)thumbY)

to

arduino.servoWrite(3, (int)thumbY)

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

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:

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);
}

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.

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

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

you might also be able to use

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

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

Hello @bkeith ,

This came up in a search for error:

Give it a try and report back to us.

:)

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.

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!

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.

Hi

Example to use the controller