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