Different inputs for different variables into the Serial Monitor

Hello dear community,

I would like to insert different values for each different variable(pulsewidth, pulsetime, pulsefrq) into the Serial Monitor. I have programed a code which is working only for one variable (pulsewidth). As you can see I declared the other two variables(pulsetime, pulsefrq) as integers but I do not know how to proceed with them in the void loop().

Here is my code:

int pulsewidth;
int pulsetime;
int pulsefrq;

void setup () {

  Serial.begin(9600);

}

void loop() {

  while (Serial.available() == 0)  {

    pulsewidth = Serial.parseInt();

    if (Serial.read() == '\n') { 

     Serial.print(pulsewidth);
     Serial.println(pulsetime);
     Serial.print(pulsefrq);
     Serial.print("\n");

    }
  }
}

Thank you very much for your help and time in advance

Best regards

Kosta

consider.

Hello Idahowalker,

thank you for your quick response and your help. I tried to implement your code-suggestion but when I activate the Serial Monitor and insert the values, they are displayed in different order.

Here is my code:


int pulsewidth;
int pulsetime;
int pulsefrq ;

void setup () {
  
  Serial.begin(9600);
  
}

void loop() {
  
  if (Serial.available() > 0) {
    
    pulsewidth = Serial.parseInt();
    pulsetime = Serial.parseInt();
    pulsefrq = Serial.parseInt();

    if (Serial.read() == '\n') {

      Serial.print(pulsewidth);
      Serial.print( ", " );
      Serial.print(pulsetime);
      Serial.print( ", " );
      Serial.print(pulsefrq);
      Serial.print( ", " );
      Serial.println("\n");

    }
  }
}

Thank you very much for your help

Best regards

Kosta

Is that an issue? Can you rearrange the code to get an order you desire?

Hello Idahowalker,

the order would be a problem, because I want to insert values for each variable and have them displayed in the serial monitor in the correct order as you can see in the code above (see Serial.print order). To be more precise: When I insert a value for pulse width, it should be displayed in the Serial.print(pulsewidth) command first. The next insertion should then display the pulsetime and the third insertion should display the pulsefrq.

Do you have any suggestion how I can solve this issue?

Thank you very much for your time

Best Regards

Kosta

So change the order in which they are read and/or change the order in which they are displayed but as far as I can see they are displayed in the order that you say that you want

Where are you stuck ?

The order from the code is the order you describe in post#5. I do not see an issue with the order.

Hello dear UKHeliBob and Idahowalker,

thank you very much for your help in this topic. In the beginning, for one unexplainable reason the order was a little bit messy. After new upload of the code, the order of the inserted values were put correctly. I will try to implement this code-functionality in a larger project.

Again, thank you very much for your time and help

Best regards

Kosta

Hello again dear community,

unfortunately for another project, I have an issue for saving the inserted values for the variables "BPM1" and "pulsbreite" for the whole void loop()-time. I type in the various values but they are displayed only for one loop. In the next loop you will notice that the Serial.print of the two aforementioned variables shows "0 ms" and "0 Hz".
My question would be, how can I keep a typed-in value constant for every loop (type the values only one time during Serial Monitor and keep them saved).

Here is my code:


#include <Wire.h>
#include <math.h>
#include "PWM.h"


int IBI = 600;
int Pulse = false;

int Signal;
float Rwert;
int AnalogPin = 0;

volatile int rate[10];                    // used to hold last ten IBI values
volatile unsigned long sampleCounter = 0;          // used to determine pulse timing
volatile unsigned long lastBeatTime = 0;           // used to find the inter beat interval
volatile int P = 512;                     // used to find peak in pulse wave
volatile int T = 512;                     // used to find trough in pulse wave
volatile int thresh = 525;                // used to find instant moment of heart beat
volatile int amp = 100;                   // used to hold amplitude of pulse waveform
volatile boolean firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = true;       // used to seed rate array so we startup with reasonable BPM
word runningTotal;
int blinkPin = 13;

long testzeit = 1000;
volatile unsigned long Zeit1;

//LEDs
const int ledrot = 13;
const int ledgelb = 12;
const int ledgruen = 11;

int pulsbreite;
int BPM1;

unsigned long pulsefrequenz;

void setup() {

  Serial.begin(9600);

  pinMode(ledrot, OUTPUT);
  pinMode(ledgelb, OUTPUT);
  pinMode(ledgruen, OUTPUT);

  Serial.println("<Arduino is ready>");

  delay(1000);

}

void loop() {

  Rwert = ((Signal * 5.0) / 1023) * 1000;

  cli();                                      // disable interrupts while we do this
  Signal = analogRead(AnalogPin);              // read the Pulse Sensor
  //Serial.println(Signal);
  sampleCounter += 2;                         // keep track of the time in mS with this variable
  int N = sampleCounter - lastBeatTime;       // monitor the time since the last beat to avoid noise

  //  find the peak and trough of the pulse wave
  if (Signal < thresh && N > (IBI / 5) * 3) { // avoid dichrotic noise by waiting 3/5 of last IBI
    if (Signal < T) {                       // T is the trough
      T = Signal;                         // keep track of lowest point in pulse wave
    }
  }

  if (Signal > thresh && Signal > P) {        // thresh condition helps avoid noise
    P = Signal;                             // P is the peak
  }                                        // keep track of highest point in pulse wave

  //  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  // signal surges up in value every time there is a pulse
  if (N > 250) {                                  // avoid high frequency noise
    if ( (Signal > thresh) && (Pulse == false) && (N > (IBI / 5) * 3) ) {
      Pulse = true;                               // set the Pulse flag when we think there is a pulse
      digitalWrite(blinkPin, HIGH);               // turn on pin 13 LED
      IBI = sampleCounter - lastBeatTime;         // measure time between beats in mS
      lastBeatTime = sampleCounter;               // keep track of time for next pulse

      if (firstBeat) {                       // if it's the first time we found a beat, if firstBeat == TRUE
        firstBeat = false;                 // clear firstBeat flag
        return;                            // IBI value is unreliable so discard it
      }
      if (secondBeat) {                      // if this is the second beat, if secondBeat == TRUE
        secondBeat = false;                 // clear secondBeat flag
        for (int i = 0; i <= 9; i++) {   // seed the running total to get a realisitic BPM at startup
          rate[i] = IBI;
        }
      }

      // keep a running total of the last 10 IBI values
      runningTotal = 0;                   // clear the runningTotal variable

      for (int i = 0; i <= 8; i++) {          // shift data in the rate array
        rate[i] = rate[i + 1];            // and drop the oldest IBI value
        runningTotal += rate[i];          // add up the 9 oldest IBI values
      }

      rate[9] = IBI;                          // add the latest IBI to the rate array
      runningTotal += rate[9];                // add the latest IBI to runningTotal
      runningTotal /= 10;                     // average the last 10 IBI values
      int BPM = 60000 / runningTotal;             // how many beats can fit into a minute? that's BPM!
      int QS = true;                              // set Quantified Self flag
      // QS FLAG IS NOT CLEARED INSIDE THIS ISR
      Zeit1 = (runningTotal - ((0.1) * runningTotal));          

      digitalWrite(ledrot, HIGH);
      Serial.print(BPM);
      Serial.println(" Hz");
      Serial.print(runningTotal);
      Serial.println(" ms");
      Serial.print(Rwert);
      Serial.println(" mV");
      Serial.print(Zeit1);
      Serial.println(" ms");
      Serial.print("\n");
      digitalWrite(ledrot, LOW);

      sei();

      if (Serial.available() > 0) {

        pulsbreite = Serial.parseInt();
        BPM1 = Serial.parseInt();

        if (Serial.available() == '\n') {

          Serial.println("\n");

        }
      }

      if (Zeit1 < testzeit) {

        while (BPM > BPM1) {

          if (BPM > BPM1) {

            Serial.println("Start pacing");
            digitalWrite(ledgelb, HIGH);
            delay(pulsbreite);
            digitalWrite(ledgelb, LOW);
            delay(pulsbreite);

            
            Serial.print(pulsbreite);
            Serial.println(" ms");
            Serial.print(BPM1);
            Serial.println(" Hz");
            Serial.print(Zeit1);
            Serial.println(" ms");
            Serial.print(BPM);
            Serial.println(" Hz");
            Serial.println("End pacing");
            Serial.println("\n");
            return BPM;

          } else {

            break;

          }

        }

      }

    }

  }

  cli();

  if (Signal > thresh && Pulse == true) {    // when the values are going down, the beat is over
    digitalWrite(blinkPin, LOW);           // turn off pin 13 LED
    Pulse = false;                         // reset the Pulse flag so we can do it again
    amp = P - T;                           // get amplitude of the pulse wave
    thresh = amp / 2 + T;                  // set thresh at 50% of the amplitude
    P = thresh;                            // reset these for next time
    T = thresh;
  }

  if (N > 2500) {                            // if 2.5 seconds go by without a beat
    thresh = 525;                          // set thresh default
    P = 512;                               // set P default
    T = 512;                               // set T default
    lastBeatTime = sampleCounter;          // bring the lastBeatTime up to date
    firstBeat = true;                      // set these to avoid noise
    secondBeat = true;                     // when we get the heartbeat back
  }
  delay(60);
  sei();                                     // enable interrupts when youre done!

}

Thank you very much in advance for your help and time

Best regards

Kosta

        if (Serial.available() == '\n')

I have not looked at your sketch in detail but this looks very odd

Serial.available() returns the number of bytes available to read, not a character. What is the line of code intended to do in your sketch ?

Hello dear UKHeliBob,

thank you very much for your time and help and pointing out my mistake. I replaced the aforementioned codeline with " Serial.read() == 'n' " and it is working now as it should be.

I wish you a nice day and a restful weekend

Best regards

Kosta

I'm glad that it is working now and I assume that you understand why what you tried didn't work

Good luck with your project

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.