My Java project is a headlamp control with Arduino. So far so good. However I did a function in which the leds gets automatic in a loop after a RadionButton
is selected. My problem is that when I select RadionButton
my JFrame
freezes and will not let me deselect RadionButton
.
In other words, selecting the button causes an active loop. The idea would be to uncheck the button, make the loop stop.
Follow my code.
Here is where the button will be selected and will send the command to the function:
if ( teste.isSelected() ) {
try {
utiArduino.enviarDados( "automatico" );
} catch (InterruptedException | IOException ex) {
Logger.getLogger(Jframe_farol.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
try {
utiArduino.enviarDados( "DesligaAuto" );
} catch (InterruptedException | IOException ex) {
Logger.getLogger(Jframe_farol.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here is the function of the loop:
public void enviarDados(String status) throws InterruptedException, IOException{
int i = 8;
if ( "automatico".equals(status) ) { // caso do automatico
while ( true ) {
output.write(i);
Thread.sleep(1000);
i++;
if ( i > 10 ){
i = 8;
}
}
}
}