[SOLVED] array problem

hi

im working on my serial communication

void serieel(){
  
if (Serial.available() > 0) {
  char incoming [4] ;
  char mode [2] ;
  char func [2];
  char val [1] ;
    while(Serial.available() < 5) {
      ;
    }
    for (int i=0; i < 5; i++) {
      incoming[i] = Serial.read() ;
    
    }
    
      mode[0] = incoming[0] ; 
      mode[1] = incoming[1] ; 
      func[0] = incoming[2] ; 
      func[1] = incoming[3] ; 
      val[0]  = incoming[4] ;       
      


    Serial.println(incoming);  //debug
    Serial.println(mode);  //debug
    Serial.println(func);  //debug
    Serial.println(val);  //debug
    
   
  Serial.flush();
  
}

}

the problem is that i cant get it to work the way i want

the serial monitor returns:

12345 incomming
123412345 mode
3412345 func
5123412345 var

but it needs to be :

12345
12
34
5

what is going wrong?

the serial monitor returns:

12345 incomming
123412345 mode
3412345 func
5123412345 var

How do you know which line represents which value? You should have something like:
Serial.print("incoming: [");
Serial.print(incoming);
Serial.println("]");
so that you KNOW what is being stored in each variable.

  char incoming [4] ;

You are trying to store 5 elements in an array sized to hold 4.

  Serial.flush();

Throwing away random amounts of unread data is always a good idea. NOT!

All of you character arrays need to be NULL-terminated before they will print properly. None of them currently are.

PaulS:

the serial monitor returns:

12345 incomming
123412345 mode
3412345 func
5123412345 var

How do you know which line represents which value? You should have something like:
Serial.print("incoming: [");
Serial.print(incoming);
Serial.println("]");
so that you KNOW what is being stored in each variable.

Serial.println(incoming); //debug
Serial.println(mode); //debug
Serial.println(func); //debug
Serial.println(val); //debug

it doesnt put in a random order or something :wink:

but how do i get them to null terminate??

but how do i get them to null terminate??

Make each array one char bigger than it is now, and put '\0' as the last element.

mode[0] = incoming[0] ; 
      mode[1] = incoming[1] ; 
      mode [2] = '\0';

thanks for the help, it solved half of my problem

void serieel(){
  
if (Serial.available() > 0) {
  char incoming [5] ;
  char mode [2] ;
  char func [2];
  char val [1] ;
    while(Serial.available() < 5) {
      ;
    }
    for (int i=0; i < 5; i++) {
      incoming[i] = Serial.read() ;
    
    }
    
      mode[0] = incoming[0] ; 
      mode[1] = incoming[1] ;
      mode[2] = '\0'; 
      func[0] = incoming[2] ; 
      func[1] = incoming[3] ; 
      func[2] = '\0';
      val[0]  = incoming[4] ;    
      val[1] = '\0';  
      


    Serial.println(incoming);  //debug
    Serial.println(mode);  //debug
    Serial.println(func);  //debug
    Serial.println(val);  //debug
    
  
}

}

the serial monitor returns:

( nothing ) ( incomming )
(nothing ) ( mode )
34 ( func )
5 ( val )

sorry said nothing

forgot to make the arrays 1 size bigger :blush:

thanks for the help