Reading specific data from arduino serial monitor

Hello, i am facing a problem in which I am getting data from arduino to esp32.The data is displayed well but i want to declare the data seprately and store the in seprate variables.I have been already working on it for past 3 days but there hasnt been a single progress .I have posted the simple code because the other codes which i tried werent working. It would be a huge help if someone would help me out.

The code is this



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(100);
}

  void loop()
{
  if (Serial.available())
  {
    
    Serial.println(Serial.read());

   
  }
}

The picture from serial monitor is this

The serial input basics tutorial.

here's one approach with values preceding single letter cmds

output with input: "234B678AZ" ... corrected

process: A 678, B 234

int varA;
int varB;

char s [80];

// -----------------------------------------------------------------------------
void
process ()
{
    sprintf (s, "%s: A %d, B %d", __func__, varA, varB);
    Serial.println (s);
}

// ---------------------------------------------------------
void
readCmd (void)
{
    static int  val = 0;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            break;

        case 'A':
            varA = val;
            val  = 0;
            break;

        case 'B':
            varB = val;
            val  = 0;
            break;

        case 'Z':
            process ();
            break;
        }
    }
}

// ---------------------------------------------------------
void
loop (void)
{
    readCmd ();
}

// ---------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);
}
2 Likes

Nick Gammon's tutorial is good:

https://www.gammon.com.au/serial

The Txtzyme project has a good serial parser:

I am sorry for asking a dumb question but i wanna know that in this you have given ASCII digits as an input but in my scenario i am getting simple integers so can you guide me how am i supposed to do that?

Does the code you posted in #1 above really make the output you claim?

Looks like you are receiving characters, one at a time, and printing each one with Serial.println(), which would put them each on a line of its own.

You are probably receiving characters that represent digits, like '1', not simple integers like a byte that was 0x01 (0b00000001).

Can you post the code that is sending these items?

a7

This is the code for the project which is based on arduino in which i am getting systolic and diasystolic pressure from EEprom and these are the values of getting . After that as you can see, i am then transferring that data to esp32 using serial.write() .

#include <Wire.h>

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 7);

uint32_t current_millis;
uint32_t prev_millis;

volatile byte XEEPROM[256] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9c, 0xb0, 0xc4, 0x54, 0xf0, 0x69, 0xca, 0xb3, 0x28, 0x84, 0xa5, 0xc1, 0x80, 0x53, 0x69, 0x0c, 0x38, 0x4f, 0x0c, 0x74, 0xa1, 0x5b, 0x8c, 0x71, 0x34, 0x30, 0x38, 0x30, 0x30, 0x38, 0x31, 0x35, 0x33, 0x31, 0x30, 0x35, 0x00, 0x12, 0x5a, 0x1e, 0xbd, 0x9e, 0x00, 0x00, 0x02, 0x82, 0x30, 0xaa, 0x10, 0xb7, 0x7f, 0x8d, 0x0a, 0xcb, 0x2e, 0xc1, 0xeb, 0x52, 0x20, 0xdc, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x55, 0x57, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x47, 0x4d, 0x54, 0x00, 0x42, 0x53, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x05, 0x00, 0x02, 0x03, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc4, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x68, 0xaa, 0xdd, 0x02, 0x46, 0x45, 0x8f, 0x8f, 0x78, 0x56, 0xbb, 0x69, 0x01, 0x04, 0x46, 0x45, 0x8f, 0x8f, 0x78, 0x56, 0xbb, 0x69, 0x01, 0x04, 0x46, 0x45, 0x8f, 0x8f, 0x78, 0x56, 0xbb, 0x69, 0x12, 0x03, 0x46, 0x45, 0x8f, 0x8f, 0x78, 0x56, 0xbb, 0x69, 0x02, 0x02, 0x46, 0x45, 0x8f, 0x8f, 0x78, 0x56, 0xbb, 0x69, 0x12, 0x02, 0x84, 0x79, 0x80, 0x00, 0x00, 0x00, 0x16, 0x01, 0x00, 0x00, 0x00 };
volatile byte rQ, rD;
volatile int data_count;
volatile int wrote;
volatile uint16_t count;
volatile uint8_t sys, dia, hr;

void setup()
{
  Serial.begin(115200);
  mySerial.begin(115200);
  Wire.begin(0x50);
  Wire.onReceive(receiveEvent);
 // Wire.onRequest(requestEvent);

  uint16_t counter = 0;
  while (counter <= 256)
  {
    XEEPROM[counter] = 0x00;
    counter++;
  }

}

void loop()
{
//  mySerial.println("1234");
  current_millis = millis();

  if (current_millis - prev_millis > 5000)
  {
    prev_millis = current_millis;


    if (count == 0)
    {
      Serial.print(":");
     // Serial.print(data_count);
     // Serial.print("  count--->");
     // Serial.println(count);
    }
    else if (count == 35)
    {
      Serial.print("Blood Presure Data: ");
      Serial.print(sys);
      Serial.print("/");
      Serial.println(dia);
      Serial.println("");
    }
    else
    {
     // Serial.println("error");
    }


    count = 0;
  }
}

void receiveEvent(int iData)
{
  data_count = iData;
  if ( iData > 0 )
  {
    while ( iData-- )
    {
      rQ = Wire.read();
      //Serial.println(rQ);
      count++;

      if (count == 10)
      {
        sys = rQ;
        Serial.println(sys);
        mySerial.write(sys);
        
      }
      if (count == 14)
      {
        dia = rQ;
        Serial.println(dia);
        mySerial.write(dia);
      }
      if (count == 16)
      {
        hr = rQ;
      }

    }
  }
}

Please say that the code in #1 produced the output you show.

I have a hat here I will happily toast and eat.

a7

Yes the output of code 1 shows the exact output

not clear what you mean by "simple integers".

since you said in the title " Reading specific data from arduino serial monitor", the assumption is ASCII characters (see ASCII chart) the character '1' is 0x31 vs and binary value of 0x01.

it's not clear (to mean) the form of the data in EEPROM: binary or ASCII. if not obvious, Serial.println () translates a binary value (e.g. int var = 123) into an ASCII character sequence: '1', '2', '3', or 0x31, 0x32, 0x33

so i'm not sure what your question is?

As you can see, i am receiving data in the form of integers from the EEPROM code i just sent and then that data is sent to esp32 via Serial.write . Now , as you can see i have 2 outputs from serial monitor pic .I basically want to assign these 2 outputs to different variables so i can utilise them later on.

Software serial baud is too high..
counter should be less than 256 not equal..
sorry.. ~q

there isnt any problem with receiving the data ,the only problem is how am i supposed to allocate this data into seperate variables


s

sorry, not clear. i don't see where the EEPROM is used. i just see an XEEPROM array containing initialized values being overwritten to zero at startup

and when you say integers, does this mean binary or ASCII, or simply not floats?

Is this what you want to achieve -- ?
You send these three (for example) comma separated integer numbers: 123,456,789 from the InputBox of Serial Monitor; these numbers will be received by the UNO and will store them as binary numbers into three iteger type variables like:

int x =  0x007B (123) 
int y =  0x01C8 (456)
int z =  0x0315 (789)
  • Save the data as you read it,
  • Maintain a state variable to keep track of where to store it
  • Store the data as indicated by the state variable.

Here's some untested code that may not be fit for purpose:

int x =0;
int y = 0;
bool xNotY = true;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(100);
}

void loop()
{
  int val = 0;
  if (Serial.available())
  {
    val = Serial.read();
    Serial.println(val);
    if(xNotY == true){
       x = val;
       xNotY = false;
    } else {
       y = val;
       xNotY = true;
    }
    // do stuff with the data:
    Serial.print("x:");
    Serial.print(x);
    Serial.print(" y:");
    Serial.print(y);
    Serial.println();
  }
}

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