0
\$\begingroup\$

This question is a follow up from my previous question.

I am working with some wireless communication in Arduino.

979797 is the address of the transmitter. I have written the following code to check the address first before processing the data. My main concern is speed of computation.

void printBuffer(const uint8_t *serialBuffer)
{
  //print contents of buffer
  for (int i = 0; i<100; i++) 
    Serial.println(serialBuffer[i]); //remove the print in binary option to decode
}

void processBytes(const uint8_t value)
{ 
  static uint8_t serialBuffer[bufferSize]; //buffer to hold values
  static unsigned int startPosition=0;

  if(startPosition < bufferSize)
  {
    serialBuffer[startPosition] = value; 
    startPosition++;
  }

  else
  {
    printBuffer(serialBuffer);
    startPosition = 0;
  }

}//end of processBytes
void loop() { 

  //check if data is available in serial buffer
  while(RX.available())
  {
    //check for start key
    address(RX.read())
    //process data
    for(int i = 0; i< 68; i++)
        processBytes(RX.read());
  }   
}
void address(const uint8_t value)
{
  int counter = 0;
  int newVal = value;
  while(value == 97 && counter < 3)
  {
    counter++;
    newVal = RX.read();
  }
}

The data packet just has the following components: start key (979797) and 68 bytes of data(payload). There is no termination key. Also, the transmitter transmits one byte at a time. Thus, each value received is less than 256. Hence why I am saving the data into a uint8_t[] array.

Example of possible data:

97 97 97 (68 bytes of random positive integers all less than 256) 97 97 97

Other than the address() method, most of it was borrowed from Gammon. Here is the reference page for the arduino commands I am using. Are there ways I can optimize this computation?

\$\endgroup\$
2
\$\begingroup\$

Still problematic

The code still has the following issues:

  1. newVal is set but not used:
    while(value == 97 && counter < 3)

should be:

    while(newVal == 97 && counter < 3)
  1. Assuming the byte stream follows the protocol, address() will eat the first byte of the message. In other words, the byte following the 97 97 97 header will be discarded. This will in turn cause the message processor to eat one byte of the next address section.

  2. There is no recovery mechanism for if the byte stream stops following the protocol. The address() function doesn't look for a header, it just returns in the absence of a header.

Rewrite

I suggest this rewrite:

void loop(void)
{    
    // Check if data is available in serial buffer
    while (RX.available()) {
        // Search for start key
        FindStartKey();
        // Process data
        for(int i = 0; i < 68; i++)
            processBytes(RX.read());
    }   
}

void FindStartKey(void)
{
    int counter = 0;

    do {
        if (RX.read() == 97)
            counter++;
        else
            counter = 0;
    } while (counter < 3);
}
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.