We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have some code here that asks for variables one at a time and then prints them at the end. Very basic UI stuff. What I need to do is find a way to turn each string (or character as they are typed) and turn them into a long, float or variable. End goal is to send to an arduino but that comes later. Also, I know this is programmed very inefficiently. It'd be great if I could slim it down. First thing's first though. Make those strings to ints. Thanks in advance! Already tried parseInt, may not be invoking it correctly...
import processing.serial.*;
Serial myPort;
int letter;
int ypos = 25;
int Step = 0;
String input = " ";
String currentString = " ";
String distance;
boolean distanceRec = false;
String timer1;
boolean timer1Rec = false;
String relayTimer;
boolean relayTimerRec = false;
String timer2;
boolean timer2Rec = false;
String speed;
boolean speedRec = false;
String confirm;
boolean confirmVal = false;
void setup() {
size(640, 900);
// Create the font
textFont(createFont("SourceCodePro-Regular.ttf", 36));
//printArray(Serial.list());
//myPort = new Serial(this, Serial.list()[1], 9600);
}
void draw() {
background(0);
textSize(14);
text("Click on the program to begin, enter distance", 25, 25);
text(input, 25, ypos, 540, 800);
if(distanceRec == true) {
text("Distance= ", 25, 400);
text(distance, 100 ,400);
text("Enter Timer 1" ,25, 50);
}
if(timer1Rec == true) {
text("Timer 1= ", 25, 425);
text(timer1, 100 ,425);
text("Enter Relay Time", 25, 75);
}
if(relayTimerRec == true) {
text("Relay Timer= ", 25, 450);
text(relayTimer, 125 ,450);
text("Enter Timer 2", 25, 100);
}
if(timer2Rec == true) {
text("Timer 2= ", 25, 475);
text(timer2, 100 ,475);
text("Enter Speed", 25, 125);
}
if(speedRec == true) {
text("Speed= ", 25, 500);
text(speed, 100 ,500);
text("CONFIRM THESE VALUES? Y/N", 25, 150);
}
if(confirmVal == true) {
text("Executing...", 25, 525);
text(distance, 0, 550);
text(timer1, 100, 550);
text(relayTimer, 200, 550);
text(timer2, 300, 550);
text(speed, 400, 550);
// myPort.write(distance);
// println(distance);
noLoop();
}
}
void keyTyped() {
letter = key;
input = input + key;
if(letter == ENTER && Step == 0) {
distance = input;
distanceRec = true;
Step++;
letter = 0;
input = " ";
}
if(letter == ENTER && Step == 1) {
timer1 = input;
timer1Rec = true;
Step++;
letter = 0;
input = " ";
}
if(letter == ENTER && Step == 2) {
relayTimer = input;
relayTimerRec = true;
Step++;
letter = 0;
input = " ";
}
if(letter == ENTER && Step == 3) {
timer2 = input;
timer2Rec = true;
Step++;
letter = 0;
input = " ";
}
if(letter == ENTER && Step == 4) {
speed = input;
speedRec = true;
Step++;
letter = 0;
input = " ";
}
if(letter == 121 || letter == 89 && Step == 5) {
confirm = input;
confirmVal = true;
Step++;
letter = 0;
input = " ";
}
if(letter == 78 || letter == 110 && Step == 5) {
exit();
// redraw();
}
}
Answers
For user input, you better look up for JOptionPane:
http://docs.Oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html
I've made a class called WindowJS which implements JOptionPane:
https://forum.Processing.org/two/discussion/12532/windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java
Another class for it:
https://forum.Processing.org/two/discussion/13175/do-whille-is-not-working-how-it-suppose-to#Item_9
So there's no way to do this within processing? I'm confused this is all a little bit beyond me currently.
Those links are different approaches to get an input from the user.
Last 1 is even better for your case, since it can request the exact datatype you demand.
I don't know the first thing about java classes and invoking them within processing. Would you be so kind as to give me one example within the program I wrote? I'm drowning here...
I wonder whether you have even tried the examples on those links?
For example, you can copy and paste the example here and run it:
https://forum.Processing.org/two/discussion/13175/do-whille-is-not-working-how-it-suppose-to#Item_9
I tried all three. They work great for someone that knows java. I need multiple inputs and I can't even get another prompt/textbox to open. Why is this so complicated? Is processing not able to do something so basic like process user input? If it can do any ONE THING it should be that.
For the time being I'm looking around the controlp5 library. That looks familiar and I can work with stuff like that.
Solved with parseFloat()
float f = Float.parseFloat(input); println(f);
Processing is focused on artist graphics authorship.
Unfortunately there's no internal API to deal w/ GUI and even basic inputs.
Contributed libraries such as ControlP5, GP4, etc. fill that void.
While the links I've presented to you above offer very basic input approaches.
I understand, but that code looks so unfamiliar to me. If I had 2 days I'd learn all about it, but right now I needed a controller program for a single axis CNC machine. It's done now. Now that I'm not under any time constraints, I'll familiarize myself with your approach. I'm sure it's great, I just didn't have the time.
Thanks for your patience.