buffer, long read and selective read.

Hello everyone.
I’m trying hard to understand and adapt, several codes that, eventually, I find in the forums.

What I want to do:
1- Read and store, serial port data, in a variable, Example:
var1 = <000, 111, 222, 333, 444, 555, 666, 777, 888, 999, AAA…>

2- direct part of the variable to a new variable. Example:
var2 = 222, or var2 = var1 [7…9]
var5 = AAA, or var2 = var1 [31…33]

Searching, I saw that there is a user who published this code:

#include <Servo.h>

Servo myServo;
byte servoPin = 3;
byte servoMin = 10;
byte servoMax = 170;
byte servoPos = 0;
byte newServoPos = servoMin;

const byte numLEDs = 2;
byte ledPin[numLEDs] = {12, 13};
unsigned long LEDinterval[numLEDs] = {200, 400};
unsigned long prevLEDmillis[numLEDs] = {0, 0};

const byte buffSize = 40;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;

char messageFromPC[buffSize] = {0};
int newFlashInterval = 0;
float servoFraction = 0.0; // fraction of servo range to move


unsigned long curMillis;

unsigned long prevReplyToPCmillis = 0;
unsigned long replyToPCinterval = 1000;

//=============

void setup() {
  Serial.begin(9600);
 
    // flash LEDs so we know we are alive
  for (byte n = 0; n < numLEDs; n++) {
     pinMode(ledPin[n], OUTPUT);
     digitalWrite(ledPin[n], HIGH);
  }
  delay(500); // delay() is OK in setup as it only happens once
 
  for (byte n = 0; n < numLEDs; n++) {
     digitalWrite(ledPin[n], LOW);
  }
 
    // initialize the servo
  myServo.attach(servoPin);
  moveServo();
 
    // tell the PC we are ready
  Serial.println("<Arduino is ready>");
}

//=============

void loop() {
  curMillis = millis();
  getDataFromPC();
  updateFlashInterval();
  updateServoPos();
  replyToPC();
  flashLEDs();
  moveServo();
}

//=============

void getDataFromPC() {

    // receive data from PC and save it into inputBuffer
   
  if(Serial.available() > 0) {

    char x = Serial.read();

      // the order of these IF clauses is significant
     
    if (x == endMarker) {
      readInProgress = false;
      newDataFromPC = true;
      inputBuffer[bytesRecvd] = 0;
      parseData();
    }
   
    if(readInProgress) {
      inputBuffer[bytesRecvd] = x;
      bytesRecvd ++;
      if (bytesRecvd == buffSize) {
        bytesRecvd = buffSize - 1;
      }
    }

    if (x == startMarker) {
      bytesRecvd = 0;
      readInProgress = true;
    }
  }
}

//=============
 
void parseData() {

    // split the data into its parts
   
  char * strtokIndx; // this is used by strtok() as an index
 
  strtokIndx = strtok(inputBuffer,",");      // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  newFlashInterval = atoi(strtokIndx);     // convert this part to an integer
 
  strtokIndx = strtok(NULL, ",");
  servoFraction = atof(strtokIndx);     // convert this part to a float

}

//=============

void replyToPC() {

  if (newDataFromPC) {
    newDataFromPC = false;
    Serial.print("<Msg ");
    Serial.print(messageFromPC);
    Serial.print(" NewFlash ");
    Serial.print(newFlashInterval);
    Serial.print(" SrvFrac ");
    Serial.print(servoFraction);
    Serial.print(" SrvPos ");
    Serial.print(newServoPos);
    Serial.print(" Time ");
    Serial.print(curMillis >> 9); // divide by 512 is approx = half-seconds
    Serial.println(">");
  }
}

//============

void updateFlashInterval() {

   // this illustrates using different inputs to call different functions
  if (strcmp(messageFromPC, "LED1") == 0) {
     updateLED1();
  }
 
  if (strcmp(messageFromPC, "LED2") == 0) {
     updateLED2();
  }
}

//=============

void updateLED1() {

  if (newFlashInterval > 100) {
    LEDinterval[0] = newFlashInterval;
  }
}

//=============

void updateLED2() {

  if (newFlashInterval > 100) {
    LEDinterval[1] = newFlashInterval;
  }
}

//=============

void flashLEDs() {

  for (byte n = 0; n < numLEDs; n++) {
    if (curMillis - prevLEDmillis[n] >= LEDinterval[n]) {
       prevLEDmillis[n] += LEDinterval[n];
       digitalWrite( ledPin[n], ! digitalRead( ledPin[n]) );
    }
  }
}

//=============

void updateServoPos() {

  byte servoRange = servoMax - servoMin;
  if (servoFraction >= 0 && servoFraction <= 1) {
    newServoPos = servoMin + ((float) servoRange * servoFraction);
  }
}

//=============

void moveServo() {
  if (servoPos != newServoPos) {
    servoPos = newServoPos;
    myServo.write(servoPos);
  }
}

APPARENTLY (I’m not sure), the author is the Robin2

I would be very grateful if someone could help me.

For those who have the same doubts as me, I recommend checking this link:

That code is from Robin2's Serial Input Basics

That code already reads everything into a variable 'inputBuffer'.

Inside the parseData() function is where things are split on commas so that is where you can accomplish getting some other parts assigned to different variables

limaosilva:
I would be very grateful if someone could help me.

If you still have a problem after carefully studying the advice that has already been given please tell us exactly what you need help with.

...R

Robin2:
If you still have a problem after carefully studying the advice that has already been given please tell us exactly what you need help with.

...R

I notice that the script uses "millis ()" for its operation, and that it is assigned to timer0 (pin 5 and 6 - uno r3)

Do you know any way to change the frequencies of pins 3,9,10,11 (25khz) without affecting the operation of this code?

limaosilva:
Do you know any way to change the frequencies of pins 3,9,10,11 (25khz) without affecting the operation of this code?

That seems to be a completely different issue from your Original Post - or have I got my wires completely confused?

What have frequencies got to do with reading Serial data?

...R

Robin2:
That seems to be a completely different issue from your Original Post - or have I got my wires completely confused?

Forgive me. You're right.

Robin2:
What have frequencies got to do with reading Serial data?

in my readings, I saw excessive alerts about the change in timer0, and the problems that this causes, mainly because delay (), millis () and other functions stop working.

But I understand that this matter is outside the scope of the topic. Once again, I apologize.

I will search further and, if necessary, create a new topic with better development.

note: In case I have new doubts about the initial objective of the topic, I will comment again.