EDIT: Got it sorted, thanks everyone.
My arduino program and device appears to be working correctly, sending the appropriate info out to serial. From reading through the console prints in processing, it is reading the inputs into strings correctly. I’m just hitting a wall in terms of figuring out how to do different things with the sequential inputs.
The arduino device uses a 4x4 keypad to perform various unit conversions, triggered by the A/B/C/D keys. It sends over the letter of the key pressed, followed by the numbers entered into, and returned by the function. The D key toggles between Imperial to Metric, and Metric to Imperial, and outputs a boolean state status.
All the variables are Strings, since I’m using the info from serial for text displays and they don’t have to be used for any calculations within processing.
Copied below is a section of the serial.read code. Inputs from arduino are in the format of:
- D 1 or D 0 ( letter D followed by the boolean state of conversion direction)
- A # # (the letter A, B, or C followed by 2 separate numbers, the entered value to convert, and the resulting converted value)
How should I go about changing things to be able to accept those 2 or 3 inputs into separate, useable items, and then acting off of them?
while (myPort.available() > 0)
{
// set myPort.read() to a readable variable
// using read string until to collect the entirety of the arduino input.
inPort = myPort.readStringUntil(10);
// what do i do to get it to operate on following inputs?
// a trigger when it hits the newline command?
// printing for troubleshooting
println(inPort);
// trigger actions based on input from arduino
if(inPort == "D")
{
String dPort = inPort;
// set variable to display selected conversion mode
if(dPort == "1")
{
conDis = "I->M";
}
else if(dPort == "0")
{
conDis = "M->I";
}
}
// differences in the A/B/C inputs is really only the trigger of the units
else if(inPort == "A")
{
// really need to figure out how to get these to read properly
inSum = myPort.readStringUntil(10);
inVal = myPort.readStringUntil(10);
// printing to console for debugging
println("inSum");
println(inSum);
println("inVal");
println(inVal);
// set unitString to A units, based on current conversion path
if(conDis == "I->M")
{
unitString = "Meters";
}
else if(conDis =="M->I")
{
unitString = "Feet";
}
}
Was suggested to look into Serial Event as a possibility, but I am not understanding the formatting and syntax for how that works.
https://processing.org/reference/libraries/serial/serialEvent_.html
void serialEvent(Serial whichPort) {
statements
}
that is the given general format, but the example code looks like
void serialEvent(Serial p) {
inString = p.readString();
}
is the choice of “whichPort” just whatever you arbitrarily want it to be, or do you have to use something like your actual myPort variable declaration?
Ok, so I played around with it, and that does seem to be whatever. Arduino site talks about using it for when you have multiple serial inputs, so I’m assuming it’s similar here? Since I only have one serial input, it’s an arbitrary choice.
Still leaves me with figuring out how to sort the inputs, which is essentially square 1.