Serial Byte Array Help!!!!!

hi i’m trying to send byte arrays from pc to arduino using bluetooth serial communication

i wanna read the data stream into an array[8]
the arduino should wait for the stream to fill up the array
each array element should contain values ranging from 0-255

this is to control the brightness of 9 LEDs using PWM

im totally lost
any help will be greatly appreciated thanks in advance.

void setup()
{
  Serial.begin(9600);
}
byte array[8];
void loop()
{
  if(Serial.available()){
  while(Serial.available() <=8)
{
    for (int i=0; i <=8 ; i++)
    {
        array[i] = Serial.read();
    }
    for (int i=0; i <=8 ; i++)
    {
        Serial.println(array[i]);
    }
}}

}

The first thing to do is to make the size of the array match the number of LEDs

Have you got any code to read serial input ?

Have you read the sticky note at the top of this forum?

i wanna read the data stream into an array[8]

to control the brightness of 9 LEDs using PWM

Hmmm. Which two LEDs share a pin?

AWOL:

i wanna read the data stream into an array[8]

to control the brightness of 9 LEDs using PWM

Hmmm. Which two LEDs share a pin?

well im using a Mega 1280 and i believe the array has 9 elements 0-8 or is it 1-8 8 elements?

Your code with some added comments.

byte array[8]; // this should be 9
void loop()
{
  if(Serial.available()) { // just one byte available will make the condition true
   while(Serial.available() <=8) // and this condition will always be true, because 0 <= 8, and also 0 <= 9
   {
    for (int i=0; i <=8 ; i++)
    {
     array[i] = Serial.read(); // you are going to get -1 sooner or later, and some nasty error if you don't set the dimension of the array to 9
    }
  }
}

What you may want to do is:

byte array[9];
void loop() {
  if (Serial.available() >= 9) // this will only be true if there are at least 9 bytes to read
  {
    for (int i=0; i <=8 ; i++)
    {
     array[i] = Serial.read(); 
    }
  }
}

spatula:
Your code with some added comments.

byte array[8]; // this should be 9

void loop()
{
 if(Serial.available()) { // just one byte available will make the condition true
  while(Serial.available() <=8) // and this condition will always be true, because 0 <= 8, and also 0 <= 9
  {
   for (int i=0; i <=8 ; i++)
   {
    array[i] = Serial.read(); // you are going to get -1 sooner or later, and some nasty error if you don’t set the dimension of the array to 9
   }
 }
}




What you may want to do is:



byte array[9];
void loop() {
 if (Serial.available() >= 9) // this will only be true if there are at least 9 bytes to read
 {
   for (int i=0; i <=8 ; i++)
   {
    array[i] = Serial.read();
   }
 }
}

hi thanks for the reply and clearing up stuff but this code only accepts 4 inputs
and returning ascii values
and bluetooth module is connected to the Serial1 terminals

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  Serial1.begin(9600);
}

int array[9];
void loop() {
  if (Serial1.available() >= 9) // this will only be true if there are at least 9 bytes to read
  {
    for (int i=0; i <=8 ; i++)
    {
     array[i] = Serial1.read(); 
    }
  
   for (int i=0; i <=8 ; i++)
    {
        Serial.println(array[i]);
    }
  }
}

I can't tell how the bluetooth part of your code is set up, but when you say that the function is "returning ascii values" I suspect you are sending the numbers as strings or maybe as integers, not bytes. E.g. if you want to send the value 32 (decimal) you can send two chars ('3' and '2') or an int, which is 2 bytes long (0x0020), or a byte (0x20). Depending on how you send your value you'll need to adjust your read() function.

Using a byte is best because read() reads one byte. If you are sending an int you'll have to make two read(), and one of the two bytes will always be 0. If you are sending chars there will be no predefined length and you'll need to use a terminating byte.

Hi Spatula , can you give me a code which will read read and display the array of bytes ??? if you can it would be awesome . thanks . im totally lost.

well im using a Mega 1280 and i believe the array has 9 elements 0-8 or is it 1-8 8 elements?

None of the above. The array has 8 elements, numbered 0 to 7.

CyrilAnthony: can you give me a code which will read read and display the array of bytes ???

The code you adapted in reply #6 already does what you want, I tested it (on a Uno). The part nobody can test is the one which sends the data. How are you sending the numbers? As int, char[], byte?

If you want to Serial1.read() twice for each number you can just do that, assigning the result to a local variable of type byte instead of assigning it directly to array[ i ]. But you'll get what you sent, so I can't tell you how to reduce the two bytes to the one you want to put into the array. You'll get more help if you post the code that transmits the data.

spatula:

CyrilAnthony: can you give me a code which will read read and display the array of bytes ???

The code you adapted in reply #6 already does what you want, I tested it (on a Uno). The part nobody can test is the one which sends the data. How are you sending the numbers? As int, char[], byte?

If you want to Serial1.read() twice for each number you can just do that, assigning the result to a local variable of type byte instead of assigning it directly to array[ i ]. But you'll get what you sent, so I can't tell you how to reduce the two bytes to the one you want to put into the array. You'll get more help if you post the code that transmits the data.

Hi thank you very much i found the problem was with PUTTY and visual basic now i am using lab view and it works well. one more problem is raised the values are out of sync is there any way to sync the values with the array ? suppose im sending the values (10,7,5,1) (in a 4 element array) initially : array[0]=10 array[1]=7 array[2]=5 array[3]=1 but when i transmit the data without resetting the board this occurs

array[0]=5 array[1]=1 array[2]=10 array[3]=7

is there any way of eliminating these type of errors ? thanks.

is there any way of eliminating these type of errors ? thanks.

Yes, there is. There is even a clue in your question. Notice the ? and the . at the ends of the sentences? They are delimiters. Once knows that a sentence (packet) starts after the next punctuation mark.

Notice that sentences start with capital letters. Why yours don’t is a mystery. Laziness, I’ll assume.

The packets you are sending need to have start and end markers. In “<10,7,5,1><10,7,5,1>”, it is much easier to determine where a packet starts than in “10,7,5,1,10,7,5,1”.

PaulS:

is there any way of eliminating these type of errors ? thanks.

Yes, there is. There is even a clue in your question. Notice the ? and the . at the ends of the sentences? They are delimiters. Once knows that a sentence (packet) starts after the next punctuation mark.

Notice that sentences start with capital letters. Why yours don’t is a mystery. Laziness, I’ll assume.

The packets you are sending need to have start and end markers. In “<10,7,5,1><10,7,5,1>”, it is much easier to determine where a packet starts than in “10,7,5,1,10,7,5,1”.

Thank you Paul , i’m new to Arduino and Labview so can you show me an example code of the delimiters ? and once again thank you for your fast reply :slight_smile: .

can you show me an example code of the delimiters ?

This code can read delimited serial data, as fast as it arrives.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says “Process the packet” is where you put code to parse and use the data.

Making LabView add delimiters is something you need to figure out.

so can you show me an example code of the delimiters ?

Below is some serial test code that might be of use. This uses a comma , as a delimiter, but you could substitute a ? if that is what labview is using.

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

PaulS:

can you show me an example code of the delimiters ?

This code can read delimited serial data, as fast as it arrives.

#define SOP '<'

#define EOP ‘>’

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff…
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = ‘\0’;
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = ‘\0’;
      }
    }
  }

// We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

// Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = ‘\0’;
  }
}



Where it says "Process the packet" is where you put code to parse and use the data.

Making LabView add delimiters is something you need to figure out.

The code works but it is in char i need it in byte :frowning: well i have a concept :slight_smile: assigning two constant values for SOP and EOP which is of higher values than that i use lets say 254 and 255
i use values from 0-200 only so is there a way to do that ? thank you very much for helping me out :slight_smile:

zoomkat:

so can you show me an example code of the delimiters ?

Below is some serial test code that might be of use. This uses a comma , as a delimiter, but you could substitute a ? if that is what labview is using.

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {   Serial.begin(9600);   Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded }

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,   //or like hello world,who are you?,bye!,

  if (Serial.available())  {     char c = Serial.read();  //gets one byte from serial buffer     if (c == ',') {       if (readString.length() >1) {         Serial.println(readString); //prints string to serial port out         //do stuff with the captured readString         readString=""; //clears variable for new input       }     }      else {            readString += c; //makes the string readString     }   } }

thanks for the code as i am doing it only for sending an array of bytes im unable to modify the code to read bytes im just a beginner .

CyrilAnthony: is there any way of eliminating these type of errors ? thanks.

Can you spare one value as a "reset" value? Suppose you limit the range of acceptable values to 0-254, then when you send 255 you can interpret it as "restart from position 0 and clear the buffer". If this is acceptable to you, you'll have to change the available() condition because you don't want to wait to receive 9 bytes just to find a "reset" in the buffer, but we can get to this later.