Serial not working as I intend. Please help.

Im trying to store data from a light sensor into a double variable. My though process was to store the individual characters received from the sensor in a char array and then convert it to a double using atof(). The Serial.print("MEASure\n"); statement is needed to receive data back from the sensor. However, I'm running into a few problems with the serial monitor and I'm lost as to how to fix it. For example:

This code works well, but the output doesn't have a newline.

const byte NUM = 64;
char string[NUM];
static byte ndx = 0;
char inByte;
double val;

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

void loop() 
{
  Serial.print("MEASure\n");
  delay(1000);
  record();
  output();
}

void record()
{
  while (Serial.available() > 0)
    {
      inByte = Serial.read();
      while (isDigit(inByte) or (inByte == '.'))
        {
         string[ndx] = inByte;
         ndx++;
         inByte = Serial.read();
        } 
      string[ndx] = '\0';
      ndx = 0;
    }
}

void output()
{
  //val = atof(string);
  //Serial.print(val);
  Serial.write(string);
  //Serial.write('\n');
}

However as soon as I un-comment the Serial.write('\n'); at the bottom, the serial monitor only prints through 1 cycle of the 'loop' function and then just stops printing 'string' but keeps on printing MEASure.

When I try to print the 'val' variable it only prints one time and then stops printing again, exactly like the error described above. My goal is to be able to print the 'val' variable and I can't seem to do that. Sorry if this dumb, any help is appreciated.

Sorry if this is a dumb question, I'm just lost on how to fix this. Any help is appreciated.

Serial.println(“MEASure”);

Thanks for your reply.

I just tried that and the "error" persists, it still just print out the sensor response once and then just contiually prints out MEASure

How many bytes of data are being received? Your buffer is sized for 64 bytes, but that needs to include the null terminator, so you can only receive 63 bytes without writing past the bounds of the buffer. Your code has no provisions to prevent that if more than 63 bytes comes in. I can reproduce the issue if I write more bytes than the buffer can hold.

The highest number it sends back to the arduino is 99999.999999, so an array of size 12 to hold the 11 numbers and the '.' should be good, at least that is what I thought. I had it at 64 because I thought the array size might be the issue as well, but its still there even if I change it 128.

Thanks for your reply :slight_smile:

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Thanks for your reply,

I was looking through example 2 and 3 and comparing my code that code to see if I could get any ideas on how to fix this issue. I also tried changing my while(isDigit(inByte) or inByte == '.') to an if statement and the code ran well but the issue was still persistent.

I think the main issue is either blocking or something to do with array, but i just can't seem to fix it.

Any further help is appreciated and thank you for your time :slight_smile:

However as soon as I un-comment the Serial.write('\n'); at the bottom, the serial monitor only prints through 1 cycle of the 'loop' function and then just stops printing 'string' but keeps on printing MEASure.

When I try to print the 'val' variable it only prints one time and then stops printing again, exactly like the error described above.

What is sending the serial data. When I run your program with input from the serial monitor I do not see these problems.