user3154924
user3154924

Reputation: 13

Arduino serial.available strange error

I am trying to read data from an Arduino serial port. My current code:

if(Serial.available()>0){
if(Serial.available()==9){...}

When I type 9 characters in the serial monitor it works fine. So when I add a second if in the

if(Serial.available()>0)

after the

if(Serial.available()==9){}

it recognizes each character as a single reading. For example when i type 4 characters it says Serial.available()=1 4 times.

real code:

    if(Serial.available() > 0){
        if(Serial.available()==9){
        for(int i =0;i<9;i++){
           RGB[i]=Serial.read() - '0';
        }

        //get the data from the integer array
        R= RGB[0]*100+RGB[1]*10+RGB[2];
        G= RGB[3]*100+RGB[4]*10+RGB[5];
        B= RGB[6]*100+RGB[7]*10+RGB[8];
        for(int q=0; q<=255; q++){
           if(R>Rp){
              Rp+=1;
              analogWrite(8, Rp);
           }else if(R<Rp){
              Rp-=1;
              analogWrite(8, Rp);
           }
           if(G>Gp){
              Gp+=1;
              analogWrite(9, Gp);
           }else if(G<Gp){
              Gp-=1;
              analogWrite(9, Gp);
           }
           if(B>Bp){
              Bp+=1;
              analogWrite(10, Bp);
           }else if(B<Bp){
              Bp-=1;
              analogWrite(10, Bp);
           }
           delay(10);

       }
  }
  //  if(Serial.read()=='r'){
  //        if(readinglstate==0){
  //          analogWrite(readinglight, 5);
  //          readinglstate=1;
  //        }else if(readinglstate==1){
  //          analogWrite(readinglight, 70);
  //          readinglstate=2;
  //        }else if(readinglstate==2){
  //          analogWrite(readinglight, 255);
  //          readinglstate=3;
  //        }else if(readinglstate==3){
  //          analogWrite(readinglight, 0);
  //          readinglstate=0;
  //        }
  //      }
  }

The commented code is the one that changes the things...

Upvotes: 1

Views: 851

Answers (3)

dfowler7437
dfowler7437

Reputation: 461

why not just read the data till you have 9 bytes then process.

char buffer[10];
int index=0;
while (Serial.available()){
    buffer[index++]=Serial.read();
    if (index>8) {
        ProcessData(buffer);
        index=0;
    }
}

Upvotes: 0

mpflaga
mpflaga

Reputation: 2827

The

Serial.read()=='r'

is popping the byte off the receive buffer. Think of it more like...

input = Serial.read(); // pop the next byte off, regardless of its value.
if (input == 'r') {

hence all your bytes in the buffer are read off the buffer. until "Serial.available() == 0" I recommend the peek function.

if(Serial.peek()=='r'){
  Serial.read(); // already know it, so pop it.
  if(readinglstate==0){
...

Upvotes: 3

vershov
vershov

Reputation: 928

Input data can arrive in several chunks, so you need to slightly rework approach to read data, please check the following code for reference:

int bytesToRead = 0;
int currBytePtr = 0;

int RGB[9];
while (9 > currBytePtr) { // we need 9 bytes of data
    if (0 < (bytesToRead = Serial.available())) { // have something to read
        for (int i = 0; i < bytesToRead; ++i ) {
            RGB[currBytePtr++] = Serial.read();
            if (9 == currBytePtr) { // we have enough data
                break;
            }
        }
    } else {
        delay(1000); // sleep a bit
    }
} // while

// process data received
...

Upvotes: 0

Related Questions