Hello, Arduino community,
I’m new to coding in general so please bear with me.
I am trying to control 2 servos and a bunch of LEDs attached to an Arduino Nano clone using Logitech Attack3 Joystick and Processing with the GameControlPlus library.
I figured out how to control the Servos as shown in the code below, but I have no idea how can I make pressing a button on the joystick control an LED, I need a sample code to do that so I can build on it.
Question 2 :
When using Processing with Arduino, do I create the entire code in Processing? I mean what will be the Arduino and Arduino IDE job afterward other than uploading the Firmatta code to it and attaching the devices to be controlled to it?
I already uploaded the Firmata Sketch to my Arduino Nano,
The Processing 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 joystickX;
float joystickY;
boolean but1;
int servoXPin=3; // Where servoX is connected to arduino
int servoYPin=5; // Where servoY is connected to arduino
void setup() {
size(360, 200);
control = ControlIO.getInstance(this);
cont = control.getMatchedDevice("attack3");
if (cont == null) {
println("Controller not connected! Please check it out.");
System.exit(-1);
} //End of "if" code
println(Arduino.list()); //Reads the connected USB devices
arduino = new Arduino(this, Arduino.list()[2], 57600);
arduino.pinMode(servoXPin, Arduino.SERVO);
arduino.pinMode(servoYPin, Arduino.SERVO);
arduino.pinMode(13, Arduino.OUTPUT);
} //End of void setup loop
public void getUserInput() {
joystickX = map(cont.getSlider("servoX").getValue(), -1, 1, 0, 180);
joystickY = map(cont.getSlider("servoY").getValue(), -1, 1, 0, 180);
but1 = cont.getButton("bn1").pressed(); // NOT SURE IF THIS PART IS CORRECT!!!!
/*joystick = map(cont.getSlider("bn2").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn3").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn4").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn5").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn6").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn7").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn8").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn9").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn9").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn10").getValue(), 0, 1, 0, 1);
joystick = map(cont.getSlider("bn11").getValue(), 0, 1, 0, 1);
*/
print(but1);
print (", ");
print(joystickX);
print (", ");
println (joystickY);
delay (100);
} // End of public void loop
void draw() {
getUserInput();
background(joystickX, 100, 255);
background(joystickY, 50, 180);
arduino.servoWrite(servoXPin, (int)joystickX);
arduino.servoWrite(servoYPin, (int)joystickY);
if (but1 == true) {
arduino.digitalWrite(13, 1); // NOT SURE OF THIS WOULD WORK TO TURN ON ARDUINO LED 13
}
}