I'm working on a simple application using JavaFX with controller class and FXML and Jssc to control arduino. The arduino is connected to a sg90 servo motor and a LED.
I'm having problem with slider to control the servo motor. i want to use the slider to control the angle of the servo motor and whenever i drag the slider thumb the servo will constantly update its angle.
here is the controller class the code works but the problem is i don't have any idea how to implement the controls for the slider and servo.
public class ServoCtrl implements Initializable {
@FXML
private Button IncrBtn = new Button();
@FXML
private Button DecrBtn = new Button();
@FXML
private ToggleButton toggleConnectSerial = new ToggleButton();
@FXML
private ToggleButton lightSwitcH = new ToggleButton();
@FXML
private Slider AngleSlider = new Slider(0, 180, 90);
@FXML
private TextField tfAngle = new TextField();
@FXML
private TextField tfSteps = new TextField();
SerialPort ServoSerialPort;
private int tempstr = 0;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
tfAngle.textProperty().bindBidirectional(AngleSlider.valueProperty(), NumberFormat.getIntegerInstance());
//disable all port when starting the application
AngleSlider.setDisable(true);
lightSwitcH.setDisable(true);
IncrBtn.setDisable(true);
DecrBtn.setDisable(true);
tfAngle.setDisable(true);
tfSteps.setDisable(true);
}
/*--------- increase or decrease slider value using buttons---------------------------------*/
public void IncrBtnPress(ActionEvent btnIncr) {
tempstr = Integer.parseInt(tfSteps.getText());
AngleSlider.setValue(Integer.parseInt(tfAngle.getText()) + tempstr);
tfAngle.setText(Integer.toString((int) AngleSlider.getValue()));
}
public void DecrBtnPress(ActionEvent btnDecr) {
tempstr = Integer.parseInt(tfSteps.getText());
AngleSlider.setValue(Integer.parseInt(tfAngle.getText()) - tempstr);
tfAngle.setText(Integer.toString((int) AngleSlider.getValue()));
}
/*-----------toggle Switch on light-----------------------------------*/
public void lightSwitch(ActionEvent eve) {
try {
if (lightSwitcH.isSelected() == true) {
lightSwitcH.setText("OFF");
/*
* send the following string commands 3 = led number 1 = On, 0 =
* Off / = seperator - = wait for next command
*
*/
// turn on led
ServoSerialPort.writeString("3/1-");
} else {
// turn off led
lightSwitcH.setText("ON");
ServoSerialPort.writeString("3/0-");
}
}
catch (SerialPortException e) {
e.printStackTrace();
}
}
/*-------------------- connect or disconnect port-------------------------*/
public void toggleConnect(ActionEvent tgle) {
if (toggleConnectSerial.isSelected() == true) {
// enable control when serial port is connected
AngleSlider.setDisable(false);
lightSwitcH.setDisable(false);
IncrBtn.setDisable(false);
DecrBtn.setDisable(false);
tfAngle.setDisable(false);
tfSteps.setDisable(false);
// set the port name
ServoSerialPort = new SerialPort("COM8");
try {
// set the toggle button text to disconnect
toggleConnectSerial.setText("Disconnect");
// open the serial port
ServoSerialPort.openPort();
// set the port parameters
ServoSerialPort.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (SerialPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// disable all control when port is disconnected
AngleSlider.setDisable(true);
lightSwitcH.setDisable(true);
IncrBtn.setDisable(true);
DecrBtn.setDisable(true);
tfAngle.setDisable(true);
tfSteps.setDisable(true);
toggleConnectSerial.setText("Connect");
try {
ServoSerialPort.closePort();
} catch (SerialPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
You are binding the value bidirectional:
tfAngle.textProperty().bindBidirectional(AngleSlider.valueProperty(), NumberFormat.getIntegerInstance());
which means the property gets already updated if the textfield
or the slider
changes - the other component gets updated automatically. therefore your methods like IncrBtnPress
should not update both values, just one: either the slider
OR the textField
.
if you want to update the new ankle to your arduino: pick a property (e.g. the textProperty()
of your textfield) and add a ChangeListener
where you can implement your new ankle-Setter to your arduino.