We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Basically what I want to do is to send values of a potentiometer to the processing and show them in the console after I press a button in the processing program. However, I'm not getting what I want.
My program does a few things that already worked. It has three different modes to move a servo and those are in the main screen of my program. And I've got another screen when I press the button and some of the values of the potentiometer are shown. However, those values don't keep showing unless I press the button again.
This is the relevant arduino code:
void loop ()
{
if (Serial.available()) {
val = Serial.read();
}
//val is send from processing to the arduino to change modes
switch (val) {
//-------------------------- Close Modes----------------------//
case 0:
digitalWrite(ledPot, LOW);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR, LOW);
break;
//--------------------------CPM Mode----------------------//
case 1:
cpmMovement();
digitalWrite(ledCPM, HIGH);
digitalWrite(ledPot, LOW);
digitalWrite(ledFSR, LOW);
break;
//--------------------------POT Mode----------------------//
case 2:
digitalWrite(ledPot, HIGH);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR, LOW);
potMovement();
break;
//--------------------------FSR Mode----------------------//
case 3:
digitalWrite(ledPot, LOW);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR, HIGH);
fsrMovement();
break;
//---------------------Calibration Mode-----------------------//
case 4:
digitalWrite(ledPot, HIGH);
digitalWrite(ledCPM, HIGH);
digitalWrite(ledFSR, HIGH);
break;
//----------------Sending Values--------------------// This is the relevant block
case 5:
digitalWrite(ledPot, LOW);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR, HIGH);
stateCAL == true;
while(stateCAL == true){
sendMaxValue();
}
break;
}
}
void sendMaxValue(){ //Function which sends the values to the console
unsigned long startTime = millis();
long calibrationTime = 3000;
while(millis() - startTime <= calibrationTime) {
maxforce = analogRead(Pot)/4;
if (maxforce > maxReading) maxReading = maxforce;
Serial.print(maxReading);
Serial.print('\t');
Serial.print(maxforce);
Serial.print('\t');
Serial.print(startTime);
Serial.print('\t');
Serial.println(millis() - startTime);
}
if (millis() - startTime > calibrationTime){
stateCAL = false;
Serial.println("Falso");
Serial.print("After 5 seconds, the maximum reading was: ");
Serial.println(maxReading);
maxReading = 0;
}
}
And this is the relevant processing code:
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);
}
if (pressedPOTButton && currentColorPOT==butColor) {
currentColorCPM = butColor;
currentColorPOT = butpreColor;
currentColorFSR = butColor;
currentColorCAL = butColor;
myPort.write(2); //Send 1 to port
println(2); //Send 2 to port
} else if (pressedPOTButton && currentColorPOT==butpreColor) {
currentColorCPM = butColor;
currentColorPOT = butColor;
currentColorFSR = butColor;
currentColorCAL = butColor;
myPort.write(0); //Send 1 to port
println(0);
}
if (pressedFSRButton && currentColorFSR==butColor) {
currentColorCPM = butColor;
currentColorPOT = butColor;
currentColorFSR = butpreColor;
currentColorCAL = butColor;
myPort.write(3); //Send 1 to port
println(3); //Send 3 to port
} else if (pressedFSRButton && currentColorFSR==butpreColor) {
currentColorCPM = butColor;
currentColorPOT = butColor;
currentColorFSR = butColor;
currentColorCAL = butColor;
myPort.write(0); //Send 1 to port
println(0);
}
if (pressedCALButton && currentColorCAL==butColor) {
currentScreen = 1;
currentColorCPM = butColor;
currentColorPOT = butColor;
currentColorFSR = butColor;
currentColorCAL = butpreColor;
myPort.write(4); //Send 1 to port
println(4); //Send 4 to port
} else if (pressedBACKButton) {
currentScreen = 0;
currentColorCPM = butColor;
currentColorPOT = butColor;
currentColorFSR = butColor;
currentColorCAL = butColor;
myPort.write(0); //Send 1 to port
println(0);
}
if (pressedSTARTCALButton) { //This is the bit that isn't working well.
if(myPort.available() > 0){
}
myPort.write(5); //Send 5 to port
println(5);
valforce = myPort.readString();
println(valforce);
}
}
Answers
What does the code for
(pressedSTARTCALButton)
look like - is this a boolean that stays true? You can trial it by changing this temporarily toif (true) {
which shows you where the error is - I wonder if it's around the bit wherepressedSTARTCALButton
is set instead of this code?Also, try this code instead ?
while (myPort.available() > 0) { String valforce= myPort.readString(); if (valforce != null) { println(valforce); } }
Thank you for your answer!