I want to control a relay with a button press from a joystick using Game Control Plus and Arduino

I am using the Game Control Plus library in processing. I have setup a configuration for the joystick and mapped one button to “TRIM”. Processing is reading button presses correctly. The code below runs and the Arduino will trigger the relay with my If/Else command if I switch it back and forth. I cannot get the input from the joystick to trigger the relay. I have watched the Game Control Plus videos and looked through the Examples and haven’t been able to figure this out. Running the Arduino Out Exapmle and using the mouse to trigger the relays works. Any help would be appreciated.

<
import processing.serial.*;

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

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

ControlDevice vkb;
ControlIO control;

Arduino arduino;

boolean trim;

void setup() {
size(200,200);

control = ControlIO.getInstance(this);
vkb = control.getMatchedDevice(“trim2”);

if (vkb == null) {
println(“No Device”);
System.exit (-1);
}

// println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(10, Arduino.OUTPUT);

}

public void getUserInput() {
trim = vkb.getButton(“TRIM”).pressed();
// printLn(TRIM);

}

public void draw() {
getUserInput();
background(250,70,22);
if (trim)
arduino.digitalWrite(10, Arduino.LOW);
else
arduino.digitalWrite(10, Arduino.HIGH);
//printLn (trim);

}

/>

Hi @01G8R, You are welcome here and I’m sorry no-one has replied. I’ve never used the Game Control library. “Joystick…relay” - Can you think of that as two separate steps: Joystick to Processing and Processing to Arduino? Are you able to get Processing to print the state of the Joystick?

Just noticed this discussion so sorry in not replying earlier.
The first thing to do is check whether Processing is detecting the joystick-button press. Modify the user input function to

public void getUserInput() {
  trim = vkb.getButton(“TRIM”).pressed();
  println("TRIM pressed " + trim);
}

When you run the sketch does it flip between true and false when you press and release the button?

If it does then the problem is in how you process the input.

I noticed in the draw() method the following statement

if (trim)
  arduino.digitalWrite(10, Arduino.LOW);
else
  arduino.digitalWrite(10, Arduino.HIGH);

So trim is true when the button is pressed and false otherwise. Now draw() is executed approximately 60 times a second so when you press and release the button trim is only true for a fraction of a second so the statement arduino.digitalWrite(10, Arduino.HIGH); is executed almost all the time so it will appear that the button has not been pressed.

Thank you for responding. I was able to get this working the day after I posted the question. I’m sorry. I should have updated the thread.

1 Like

Sorry for the late response here, Im running into a similar issue with my current setup and I cant get my LED to turn on with my PS4 controller. Im wondering what you did to fix your code? Thanks!