New arduino person here so bear with me. I have watched a video where the guy controlled a servo with his plugged in PS4 controller. I have copied the code from his site, mapped my two thumb joysticks to the correct servos in the processing configurator, and have successfully been able to control 1 servo. The second servo still won't work at all. If someone could please help me that would be great.
Also it won't let me upload my code to show you, any ideas?
Alright, let's take a look at your code! It's great that you're using Processing and the ControlP5 library to connect your PS4 controller to your Arduino. Here's a breakdown of what's going on and how we can fix that second servo issue:
Understanding Your Code
Libraries: You've correctly included the necessary libraries:
processing.serial.*: For serial communication with the Arduino.
net.java.games.input.*, org.gamecontrolplus.*, org.gamecontrolplus.gui.*: For controller input.
cc.arduino.*, org.firmata.*: For Arduino communication.
Setup:
You're initializing the ControlIO and Arduino objects.
You're correctly setting the pin modes for pins 7 and 4 as servo outputs.
getUserInput():
Here's where the problem lies. You're mapping the values from servoPos to thumb, and then immediately overwriting thumb with the mapped value from servoPosA. This means that both servos are getting the same value, based only on servoPosA.
draw():
You're calling getUserInput() to update thumb.
You're using arduino.servoWrite() to send the same thumb value to both servos.
How to Fix It
To control both servos independently, you need to:
Create Separate Variables: Use two different variables to store the mapped values from the two sliders.
Map Sliders to Separate Variables: Map servoPos to one variable and servoPosA to the other.
Send Separate Values to Servos: Use the two different variables in your arduino.servoWrite() calls.
Here's the corrected code:
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 thumb1; // Variable for servo 1 (pin 7)
float thumb2; // Variable for servo 2 (pin 4)
void setup() {
size(360, 200);
control = ControlIO.getInstance(this);
cont = control.getMatchedDevice("xbs");
if (cont == null) {
println("not today chump");
System.exit(-1);
}
//println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(7, Arduino.SERVO);
arduino.pinMode(4, Arduino.SERVO);
}
void getUserInput() {
// Map servoPos to thumb1
thumb1 = map(cont.getSlider("servoPos").getValue(), -1, 1, 0, 90);
// Map servoPosA to thumb2
thumb2 = map(cont.getSlider("servoPosA").getValue(), -1, 1, 0, 90);
// You can uncomment these lines for debugging:
// println("thumb1: " + thumb1 + ", thumb2: " + thumb2);
}
void draw() {
getUserInput();
background(thumb1, 100, 255); // You can change this if you want
arduino.servoWrite(7, (int)thumb1);
arduino.servoWrite(4, (int)thumb2);
}
Explanation of Changes
thumb1 and thumb2: We've created two separate float variables to hold the mapped values.
Separate Mapping: We're now mapping cont.getSlider("servoPos").getValue() to thumb1 and cont.getSlider("servoPosA").getValue() to thumb2.
Separate servoWrite() Calls: We're sending thumb1 to servo on pin 7 and thumb2 to servo on pin 4.
Testing
Upload this corrected code to Processing.
Make sure your Arduino is connected and the servos are wired correctly.
Move the two joysticks on your PS4 controller and observe the servos. They should now move independently.
Let me know if you have any more questions or if you encounter any issues!