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;
}
}
}
}
}