Getting two values from the serial monitor

Hello, beginner here so sorry for my lack of knowledge/understanding. In a project I am working on, I want to be able to enter two different values into the serial monitor, to represent “weight” and “BPM”.

I am aware that the code I have written fails to do this correctly, but I’m struggling to find ways of writing the code any other way. With the code I have right now, “weight” can be inputted fine, and then when I input “BPM” it prints the value of it correctly, but then pauses and prints the value of BPM as “0” each time.

Would anyone be willing to offer any guidance as to how I could achieve my goal? Thank you.

#include "ArduinoMotorShieldR3.h"
ArduinoMotorShieldR3 md;

#include <avr/pgmspace.h>
#include <Wire.h>
#include <sfm3000wedo.h>

int weight = 0;

int BPM = 0;

int state = -1;
unsigned long startTime;
unsigned long duration;

SFM3000wedo measflow(64);

int offset = 32768;
int scale = 120;


void setup() {
  Wire.begin();
  delay(500);
  Serial.begin(9600);

  measflow.init();
  Serial.println("Sensor initialized!");
  md.init();

  Serial.print("Enter weight: ");
}


void loop() {

    if (Serial.available() > 0) {
      int weight = Serial.parseInt();
      Serial.print("Weight:");
      Serial.println(weight);
      
    


  while (weight > 0) {

  if (Serial.available() > 0) {
      int BPM = Serial.parseInt();
        Serial.print("BPM:");
        Serial.println(BPM);
      }


  
      
  float pressdur = 7.7117 * weight;
  pressdur = pressdur + 49.022;

  float updur = pressdur / 2;

  
  // for reading SFM3300
  float flowSFM = measflow.getvalue();
  //if (flowSFM > 0) flowSFM = flowSFM + offset;
  if (flowSFM > 0) flowSFM = 0;
  else if (flowSFM < 0) flowSFM = flowSFM - offset;
  flowSFM = flowSFM / scale;
  flowSFM = flowSFM * 16.6666;

  if (flowSFM > 1.00) {
    Serial.println(flowSFM);
    Serial.print(millis());
    Serial.print(",  ");
    delay(100);
  }
  if (flowSFM < 1.00) {
    Serial.println(0.00);
    Serial.print(millis());
    Serial.print(",  ");
    delay(100);
  }

  if ( millis() - startTime >= duration ) {
    // advance to next state
    state++;
    if ( state > 3 ) state = 0;
    startTime = millis();
    switch (state) {
      case 0:
        md.setM1Speed(350);
        duration = pressdur;
        break;
      case 1:
        md.setM1Speed(0);
        duration = 500;
        break;
      case 2:
        md.setM1Speed(-350);
        duration = updur;
        break;
      case 3:
        md.setM1Speed(0);
        duration = 2000;
        break;
    }
    }
  }
  }
}

The serial input basics tutorial shows how to read serial input.

Here is the serial input basics example #5 modified to return your weight and BPM from the serial input data. Note the use of start and end markers to add robustness to the code.

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 12;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
int weight = 0;
int BPM = 0;

boolean newData = false;

//============

void setup() {
    Serial.begin(9600);
    Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
    Serial.println("Enter data in this style <15,61> (<weight,BPM>)");
    Serial.println();
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    weight = atoi(strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    BPM = atoi(strtokIndx);     // convert this part to an integer   

}

//============

void showParsedData() {
    Serial.print("weight ");
    Serial.println(weight);
    Serial.print("BPM ");
    Serial.println(BPM);    
}

Thank you, that helps a lot. Now when incorporating this into my code stated above. Where would I put my while statement and everything in it: "while (weight > 0) {" since "void loop()" is now used by this code. Sorry if this is a dumb question.

The only things in the loop() function that work on serial input is the call to recvWithStartEndMarkers(), a test if there is new data available and the parsing and display of the new data (if new data is available) . Put the code that deals with the new data in the if structure after the parsing of the data. Display is optional.