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

**URL:** https://discourse.processing.org/t/i-want-to-control-a-relay-with-a-button-press-from-a-joystick-using-game-control-plus-and-arduino/36153
**Category:** Electronics (Arduino, etc.)
**Created:** [April 6, 2022, 7:38am UTC](https://discourse.processing.org/t/i-want-to-control-a-relay-with-a-button-press-from-a-joystick-using-game-control-plus-and-arduino/36153 "2022-04-06T07:38:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![01G8R](https://avatars.discourse-cdn.com/v4/letter/0/eb8c5e/32.png) [@01G8R](https://discourse.processing.org/u/01G8R)
#### Post date: [April 6, 2022, 7:38am UTC](https://discourse.processing.org/t/i-want-to-control-a-relay-with-a-button-press-from-a-joystick-using-game-control-plus-and-arduino/36153/1 "2022-04-06T07:38:54Z")

</div>

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

}

/\>

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [April 15, 2022, 7:41am UTC](https://discourse.processing.org/t/i-want-to-control-a-relay-with-a-button-press-from-a-joystick-using-game-control-plus-and-arduino/36153/2 "2022-04-15T07:41:54Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [April 15, 2022, 1:04pm UTC](https://discourse.processing.org/t/i-want-to-control-a-relay-with-a-button-press-from-a-joystick-using-game-control-plus-and-arduino/36153/3 "2022-04-15T13:04:52Z")

</div>

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

```auto
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

```auto
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.

---

<div class="post-metadata">

### Author: ![01G8R](https://avatars.discourse-cdn.com/v4/letter/0/eb8c5e/32.png) [@01G8R](https://discourse.processing.org/u/01G8R)
#### Post date: [April 15, 2022, 4:03pm UTC](https://discourse.processing.org/t/i-want-to-control-a-relay-with-a-button-press-from-a-joystick-using-game-control-plus-and-arduino/36153/4 "2022-04-15T16:03:18Z")

</div>

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.

---

<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: [September 8, 2023, 2:50pm UTC](https://discourse.processing.org/t/i-want-to-control-a-relay-with-a-button-press-from-a-joystick-using-game-control-plus-and-arduino/36153/5 "2023-09-08T14:50:27Z")

</div>

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!
