Hello everyone,
I am making a little program with processing and arduino. What I want to do is to click on a button in the processing program and then send a value to the arduino code, when it gets the value, I want it to move a servomotor forward and backward until I click on another button in the processing code.
However, I am just sending the value just once to the arduino, hence, it only moves the servo forward and I need to click on the button again to go back to the initial position.
I have tried to do while loop to keep sending the same value if I click the button but then I can't get out of the loop.
This is my relevant arduino code (I've got more buttons to make the servo move with a potentiometer and a FSR but if I'm capable to make it work with one, I'll mmake it work with the others) :
void loop ()
{
if(Serial.available()){
val= Serial.read();
switch(val){
//-------------------------- Close Mode----------------------//
case 0:
if(val == 0){
digitalWrite(ledPot, LOW);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR, LOW);
}
break;
//--------------------------CPM Mode----------------------//
case 1:
if(val == 1){
cpmMovement();
digitalWrite(ledCPM, HIGH);
digitalWrite(ledPot, LOW);
digitalWrite(ledFSR,LOW);
}
break;
//--------------------------POT Mode----------------------//
case 2:
if(val == 2){
digitalWrite(ledPot, HIGH);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR,LOW);
potMovement();
}
break;
//--------------------------FSR Mode----------------------//
case 3:
if(val == 3){
digitalWrite(ledPot, LOW);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR,HIGH);
fsrMovement();
}
break;
//---------------------Calibration Mode-----------------------//
case 4:
if(val == 4){
digitalWrite(ledPot,HIGH);
digitalWrite(ledCPM,HIGH);
digitalWrite(ledFSR,HIGH);
}
break;
}
And here's my relevant processing code:
void update(int x, int y) { //Update postion of mouse
if ( pressedButtonCPM(cpmButtonX, cpmButtonY, butWidth, butHeight ) ) {
pressedCPMButton = true;
pressedPOTButton = false;
pressedFSRButton = false;
pressedCALButton = false;
pressedBACKButton = false;
pressedSTARTCALButton = false;
//myPort.write(1);
//println(1);
}
}
void mousePressed() {
println("Coordinates: " + mouseX + "," + mouseY);
if (pressedCPMButton && currentColorCPM==butColor) { //Changing color CPM to pressed button color
currentColorCPM = butpreColor;
currentColorPOT = butColor;
currentColorFSR = butColor;
currentColorCAL = butColor;
myPort.write(1); //Send 1 to port
println(1);
} else if (pressedCPMButton && currentColorCPM==butpreColor) {
currentColorCPM = butColor;
currentColorPOT = butColor;
currentColorFSR = butColor;
currentColorCAL = butColor;
myPort.write(0); //Send 1 to port
println(0);
}
}