Byte array from serial

Hi,

I have built a camera slider (working) with the following basic setup:

-Stepper motor driving belt,
-uStepperS motor driver/ micro controller
-HC06 bluetooth connected to uStepperS
-MIT AI2 built app sending 1 byte information in array with a start end byte eg. StartByte, 1byte, 1byte, EndByte

  • serial data is mapped in arduino code and fed to stepper motor

It struck me as more efficient to send the 2byte actual values, from the app.
E.g. send 2byte "1500" steps rather than a 1byte 253 value that is mapped 0-253 to 0-1500 in code on the uStepperS

Currently I am using the following to get serial
Code from "Little French Kev" 'nerf cannon'

byte byte_from_app;
const byte buffSize = 30;
byte inputBuffer[buffSize];
const byte startMarker = 255;
const byte endMarker = 254;
byte bytesRecvd = 0;

Void setup()

Void loop(
getDataFromPC();
)

void getDataFromPC() {

  //expected structure of data [start byte, run byte, pause byte, 
speed byte, pan byte, end byte]
  //start byte = 255
  //start run 0 and 1
  //pause run 0 and 1
  //speed = byte between 0 and 253
  //pan = byte between 0 and 253
  //end byte = 254

  if (Serial.available()) {  // If data available in serial

    byte_from_app = Serial.read();   //read the next character available

    if (byte_from_app == 255) {     // look for start byte, if found:
      bytesRecvd = 0;                   //reset byte received to 0(to start populating inputBuffer from start)
      data_received = false;
    }

    else if (byte_from_app == 254) {    // look for end byte, if found:
      data_received = true;                // set data_received to true so the data can be used
    }

    else {                            // add received bytes to buffer
      inputBuffer[bytesRecvd] = byte_from_app;     //add character to input buffer
      bytesRecvd++;                                // increment byte received (this act as an index)
      if (bytesRecvd == buffSize) {    // just a security in case the inputBuffer fills up (shouldn't happen)
        bytesRecvd = buffSize - 1;    // if bytesReceived > buffer size set bytesReceived smaller than buffer size
      }
    }
  }
}

I always try to fully understand any code I use from others, and do understand the above.

But I would like to do this without the mapping.

So my array from android would need to be something like;
1byteStart, 1byteRun, 1bytePause, 2byteSpeed, 2bytePan, 1byteEnd.

4hrs 23minutes later I am more confused now than at the start about how to process the order suggested above. Hence a call for help!

Would some kind soul put me out of my misery and throw me a snippet?

Void setup(
  byte byte_from_app;
  const byte buffSize = 30;
  byte inputBuffer[buffSize];
  const byte startMarker = 255;
  const byte endMarker = 254;
  byte bytesRecvd = 0;
)

I assume that is not your real code

UKHeliBob:

Void setup(

byte byte_from_app;
  const byte buffSize = 30;
  byte inputBuffer[buffSize];
  const byte startMarker = 255;
  const byte endMarker = 254;
  byte bytesRecvd = 0;
)



I assume that is not your real code

It's the code (in setup) I used from the original author that relates to serial data,

I assume you find it problematic?

I assume you find it problematic?

I have no problem with it but the compiler will

@Cameramantom

Hove you known why the following codes are problematic for the Compiler and not for @UKHeliBob?

Void setup(
  byte byte_from_app;
  const byte buffSize = 30;
  byte inputBuffer[buffSize];
  const byte startMarker = 255;
  const byte endMarker = 254;
  byte bytesRecvd = 0;
)

There is something that can be easily resolved by a human being, but not so easily by the Compiler.

GolamMostafa:
@Cameramantom

Hove you known why the following codes are problematic for the Compiler and not for @UKHeliBob?

Void setup(

byte byte_from_app;
  const byte buffSize = 30;
  byte inputBuffer[buffSize];
  const byte startMarker = 255;
  const byte endMarker = 254;
  byte bytesRecvd = 0;
)




There is something that can be easily resolved by a human being, but not so easily by the Compiler.

All compiles without error or alert, everything works. So no I do not know to what you are referring, unless the const buffsize is a potential problem.

Please do enlighten me

Just to clarify the code I posted is handling 1byte data, it is my existing code.

I am aware 1byte is read, the next byte read at the next iteration etc...

I was hoping for help with how to do something similar where the incoming array is mixed 1 and 2 byte elements

When the given codes are compiled in my Arduino UNO-IDE1.8.9-WIN10 Platform, I encounter a compilation error? Don't forget to add this line: void loop(){}.

Void setup(                   //Void does not name a data type
  byte byte_from_app;         //all of the following variables are declared in setup()
  const byte buffSize = 30;   //so they will only be in scope (ie useable)
  byte inputBuffer[buffSize]; //in setup() and not the rest of the program
  const byte startMarker = 255;
  const byte endMarker = 254;
  byte bytesRecvd = 0;
)

Incidentally, I assume that this is not your loop() function either

Void loop(
getDataFromPC();
)

Cameramantom:
Just to clarify the code I posted is handling 1byte data, it is my existing code.

The code you posted does not compile:

sketch_jan03a:2:1: error: 'Void' does not name a type; did you mean 'void'?

 Void setup(

 ^~~~

 void

sketch_jan03a:9:1: error: expected unqualified-id before ')' token

 )

 ^

sketch_jan03a:14:1: error: expected unqualified-id before ')' token

 )

 ^

C:\Users\tr001221\AppData\Local\Temp\arduino_modified_sketch_426594\sketch_jan03a.ino: In function 'void getDataFromPC()':

sketch_jan03a:19:1: error: 'speed' was not declared in this scope

 speed byte, pan byte, end byte]

 ^~~~~

exit status 1
'Void' does not name a type; did you mean 'void'?

Ok, my bad, I fixed the code snippet in the first example I.e. removed my declarations from setup

They are set before void setup()

Apologies.... :slight_smile:

Nope:

Arduino: 1.8.10 (Windows 10), TD: 1.48, Board: "Arduino Uno"

sketch_jan03a:11:1: error: 'Void' does not name a type; did you mean 'void'?

 Void loop(

 ^~~~

 void

sketch_jan03a:13:1: error: expected unqualified-id before ')' token

 )

 ^

C:\Users\tr001221\AppData\Local\Temp\arduino_modified_sketch_656624\sketch_jan03a.ino: In function 'void getDataFromPC()':

sketch_jan03a:18:1: error: 'speed' was not declared in this scope

 speed byte, pan byte, end byte]

 ^~~~~

exit status 1
'Void' does not name a type; did you mean 'void'?

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Why are you wasting our time?

Cameramantom:
Ok, my bad, I fixed the code snippet in the first example I.e. removed my declarations from setup

They are set before void setup()

Apologies.... :slight_smile:

Congratulations. By changing a post that has been commented on you have made a nonsense of this topic and your code still does not compile

Why not simply copy your complete actual code here

Please, compile the codes of Post#4 and tell us what 'compilation error' you are getting. Don't forget to include this line in the sketch: void loop(){}.

Its not my intention to confuse anyone or waste their time,

UKHeliBob:
Congratulations. By changing a post that has been commented on you have made a nonsense of this topic and your code still does not compile

Why not simply copy your complete actual code here

My apologies for changing the first post;

I haven't posted my complete code because it relates to the ustepper board and library so wont compile for those that don't have them installed.

My code relating to the serial processing is below and compiles (for me here testing it)

byte byte_from_app;
const byte buffSize = 30;
byte inputBuffer[buffSize];
const byte startMarker = 255;
const byte endMarker = 254;
byte bytesRecvd = 0;

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

void loop() {
getDataFromPC();
}

void getDataFromPC() {

  //expected structure of data [start byte, run byte, pause byte, speed byte, pan byte, end byte]
  //start byte = 255
  //start run 0 and 1
  //pause run 0 and 1
  //speed = byte between 0 and 253
  //pan = byte between 0 and 253
  //end byte = 254

  if (Serial.available()) {  // If data available in serial

    byte_from_app = Serial.read();   //read the next character available

    if (byte_from_app == 255) {     // look for start byte, if found:
      bytesRecvd = 0;                   //reset byte received to 0(to start populating inputBuffer from start)
      
    }

    else if (byte_from_app == 254) {    // look for end byte, if found:
      
    }

    else {                            // add received bytes to buffer
      inputBuffer[bytesRecvd] = byte_from_app;     //add character to input buffer
      bytesRecvd++;                                // increment byte received (this act as an index)
      if (bytesRecvd == buffSize) {    // just a security in case the inputBuffer fills up (shouldn't happen)
        bytesRecvd = buffSize - 1;    // if bytesReceived > buffer size set bytesReceived smaller than buffer size
      }
    }
  }
}

So..

for the above code which works for 1byte data. Would someone help with how I can modify it so that I can send 1 and 2 byte data e.g. 1byteStart, 1byteRun, 1bytePause, 2byteSpeed, 2bytePan, 1byteEnd.

Can you please give an example of the 2 bytes that would be received for Speed and Pan and the values that they represent ? How much control do you have over the format of the data being sent ?

Do you have any answer for the question of Post#13?

UKHeliBob:
Can you please give an example of the 2 bytes that would be received for Speed and Pan and the values that they represent ? How much control do you have over the format of the data being sent ?

I am sending data from an android app made in app inventor 2

currently

I send a 1byte numbers for each global (see attachment)

serial monitor looks like startbyte,1,0,253,253,endbyte

I can simply choose a to send a 2byte number from the app by selecting a different block type.

App inventor is by all accounts limited, but I can send 1, 2 or 4 byte numbers or text

Values;
at present I send a value for each global (pan and speed) between 0 and 253 then map those to the physical values for the stepper motor.

speed: 0-253 is mapped to 50-300 (steps per S)
pan: 0 - 253 is mapped to 0 - 1550 (max angle on the linear rail)

blocks.jpg

I can simply choose a to send a 2byte number from the app by selecting a different block type.

So what would say 300 or 1550 look like if sent as 2 bytes each ?
Instead of something like startbyte,1,0,253,253,endbyte what would be received ?

UKHeliBob:
So what would say 300 or 1550 look like if sent as 2 bytes each ?
Instead of something like startbyte,1,0,253,253,endbyte what would be received ?

the direct values with no need to map

startbyte,1,0,300,1550,endbyte