Help with turning LEDs on and off using Game Control Plus Library and PS4 remote

Hello everyone,

As the title suggests, I’m using the game control plus library to turn on an LED, I’ve read through some other threads with similar issues but haven’t found a solution. I want to press X and have an LED turn on, then press X again and have it turn off, I’m working on just pressing X to have it turn on for now.

First I made sure my controller (PS4 controller) was properly connected to my computer using the G4P configurator. I have other code that works fine with the controller that I know works and I ran an example that uses buttons and that worked as well, so I think the problem lies within my code.

In another thread with a similar problem, @quark mentions the following:

I think this is the solution I need but I’m not sure how to solve it. Thanks for any help!

Here’s my code:

import processing.serial.*;

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

import g4p_controls.*;

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

ControlDevice PS4;
ControlIO control;

Arduino arduino;

boolean X = false;

color off = color(4, 79, 111);
color on = color(84, 145, 158);

int[] value = {3, Arduino.LOW}; //Im not sure if this is needed anymore

public void setup(){
  size(470, 200); //this will be deleted once integrated into other code
  
  control = ControlIO.getInstance(this);
  PS4 = control.getMatchedDevice("LED test"); //refer to my saved file with controls and stuff
  
  if (PS4 == null){
    println("no controller"); 
    System.exit(-1); //quit program if no controller, this doesnt actually work but ill worry about it later
  }
  
  arduino = new Arduino(this, "COM3", 9600); //connect to my arduino
  
  //for (int i = 0; i <= 13; i++)
  arduino.pinMode(3, Arduino.OUTPUT); //pin 3 is my LED
}

public void getUserInput(){
  
 boolean X = PS4.getButton("LedOn").pressed(); //X button on controller should turn LEDs on and off
 
 if (X == true) { //if X is pressed, pin 3 is high
   value[1] = Arduino.HIGH;
 }
}

public void draw(){
  getUserInput();
  
  if (X){ //maybe redundant, if X is pressed, pin 3 is high, otherwise pin 3 is low. 
    arduino.digitalWrite(3, Arduino.HIGH);
    background(200, 0, 0);
  } else { 
    arduino.digitalWrite(3, Arduino.LOW);
    background(155, 100, 155);
  }
}
1 Like

Hi @bkeith ,

Can’t test your code, but the issue is, that the global X is always false, as you declare another local X here…

Try again by removing boolean before the X inside the getUserInput function…

Cheers
— mnse

Awesome, thanks!

So I removed the boolean and added a line in the getUserInput section, I added:

println("LedOn Pressed " + X); //tells me if signal changes from false to true when pressing X

I also plugged the controller into my computer. When I run this it shows true or false when I’m pushing the button and my box changes color from purple to red. So at least that part of the code works but my LED is still not turning on. I tested the LED with a 9v battery on my breadboard and that turned it on so I know the LED is working. I ran a really simple arduino code that just turns the LED on and off with my current pin setup and that worked, so now I’m really at a loss. I just don’t get why my code isn’t sending information properly to the arduino. Thanks again for your help!

well, well, well. Wouldnt you know it, my baud was too low. I changed baud from 9600 to 57600 and it works! Thanks again for the help!!

Correction: 57600 not 15200

Hi

https://www.reddit.com/r/arduino/comments/169uje0/using_the_game_controller_plus_library_in/