Arduino and Serial monitor data seperation

I am trying to be able to send data to the serial monitor like this for example : 135,210

And have the Serial monitor send it back to me.

How come this code wont work?

int cnt = 0;

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

int integerValue=0;

char storedData[2];
// Max value is 65535
char incomingByte;

void loop() {
if (Serial.available() > 0) { // something came across serial
integerValue = 0; // throw away previous integerValue
while(1) { // force into a loop until ‘n’ is received
incomingByte = Serial.read();
if (incomingByte == ‘\n’) break;
if (incomingByte == ‘,’){
cnt++;
break;
} // exit the while(1), we’re done receiving
if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
integerValue *= 10; // shift left 1 decimal place
// convert ASCII to integer, add, and shift left 1 decimal place
integerValue = ((incomingByte - 48) + integerValue);

}

storedData[cnt] = integerValue;

for (int i=0; i<2; i++){
Serial.println(storedData*);*

  • }*
  • }*
    Thank you!
    }
    while(1) {            // force into a loop until 'n' is received

N000000000000000000000000000000000000!!!!!

You're in a loop function. It does the looping for you. It's between iterations of the loop that it does it's housekeeping with the serial stuff.

If you love it, you will let it go :(

your code continues reading from Serial without checking if there is new data. You should check that for every read!

How would i check if theres new data?

if (Serial.available() > 0)

The demos here and here may be helpful.

...R

I am trying to be able to send data to the serial monitor like this for example : 135,210

And have the Serial monitor send it back to me.

How come this code wont work?

The serial monitor is not designed to receive data from a sender and then echo back to the sender what was received. Do you actually want to send something to the arduino from the serial monitor and have the arduino send it back to the serial monitor?

Just for the sheer hell of it, here’s another demo WITHOUT constipating the loop function.

This one has the added advantage that it’s totally untested, so it’s actually an adventure into the unknown.

int storedData[2];
int whichOne;


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

storedData[0]=0;
storedData[1]=0;
whichOne=0;
Serial.println("");
Serial.println("Input 2 numbers separated by a comma");
}


void loop()
{
char character=0;
if(Serial.available())
  character=Serial.read();

if((character>='0')&&(character<='9'))  
  {storedData[whichOne]*=10;
  storedData[whichOne]+=(character-'0');
  }
  
if (character==',')
  whichOne+=(whichOne<1);

if((character=='\r')||(character=='\n'))
  {
   Serial.print("1st data item ");
   Serial.println(storedData[0],DEC);
   Serial.print("2nd data item ");
   Serial.println(storedData[1],DEC);
   delay(2000);
   setup();

   //discard any remaining newline chars
   while(Serial.available())
      Serial.read();   
  }
}

Hahaha ok thank you everyone!!