who to read four values from serial and use them ?

i m working on PID project and i need to read four values that i will sending them from matlab gui to arduino .
my question is who to read this four integers values (set,aggKp,aggKi,aggKd) form serial ?
my arduion codes :
#include <PID_v1.h>
#include<Servo.h>
int ServoOutput;
int ADC1;
int giris ;
int set;
int b ;
int balans;
int x;
Servo myServo;

double Setpoint, Input, Output;
int aggKp, aggKi, aggKd;
double consKp=0.1, consKi=0, consKd=1.2;

PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);

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

myServo.attach(9);

myPID.SetMode(AUTOMATIC);
myPID.SetOutputLimits(-100,100);
}
void pid_set()
{
while (Serial.available() == 0 ){
}
for ( int i = 0 ; i < 1 ; i++ ){
buffer = Serial.parseInt();
buffer [i+1]= ‘\0’ ;
}
set = atoi (buffer) ;
while (Serial.available() == 0 ){
}
for ( int i = 0 ; i < 1 ; i++ ){
buffer = Serial.parseInt();
buffer [i+1]= ‘\0’ ;
}
aggKp = atoi (buffer) ;
while (Serial.available() == 0 ){
}
for ( int i = 0 ; i < 1 ; i++ ){
buffer = Serial.parseInt();
buffer [i+1]= ‘\0’ ;
}
aggKi = atoi (buffer) ;
while (Serial.available() == 0 ){
}
for ( int i = 0 ; i < 1 ; i++ ){
buffer = Serial.parseInt();
buffer [i+1]= ‘\0’ ;
}
aggKd = atoi (buffer) ;
}

void loop()
{
giris = map(analogRead(A0),0,1023,0,50);
Input = giris;
ADC1 = Input;
balans = (set/25)*2;
Setpoint =set - 27 - balans;
double gap = abs(Setpoint-Input);

if (gap < 10)
{
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
myPID.SetTunings(aggKp, aggKi, aggKd);
}

myPID.Compute();
ServoOutput= 117 + Output;
ServoOutput = 180 - ServoOutput;
myServo.write(ServoOutput);

if ( b==4){
Serial.println(ADC1);
Serial.println(ADC1);
Serial.println(aggKp);
Serial.println(aggKi);
Serial.println(aggKd);

b = 0;
}

b = b+1;
delay(50);
}

Read Robin2's excellent tutorial on reading/using the Serial port here and them implement one of those.

I made such a function many times. I added notes for you so you can adjust it easily. You really did over complicate your code, I hope you learn something from my attempt. To use my code you must separate each value with a comma ‘CSV’ format with the final value ending either with a comma or a newline. You can test this code with your Serial monitor, just place serialDebug(); in the loop() function if you wish to keep sending new commands over time, or in the setup() if you only need to send values once.

void serialDebug () {
  const byte numberOfValues = 4; // number of values to be recived
  byte charPos = 0, valPos = 0; // ESP32 requires values to be preset // I.E. 'valPos' may be used uninitialized in this function [-Werror=maybe-uninitialized]
  const byte maxChars = 8; // max digits / chars, includes decimal points and minus signs ! I.E. -123.123 is 8 chars
  char buffer[maxChars];
  float values[numberOfValues]; // allows for byte, int or float, else change data type to what you require 
  while (Serial.available() > 0) {
    char c = Serial.read();
    if ((c >= '0' && c <= '9') || c == '-' || c == '.') // except just numbers, minus sign and decimal point
      buffer[charPos++] = c;
    else if (c == ',' || c == '\n') { // values must be seperated by a comma, and the final value can end with a new line or comma.
      values[valPos++] = atof(buffer); // if just using ints change to atoi
      charPos = 0; // reset char position
      for (byte i = 0; i < maxChars; i++) { // clear char buffer
        buffer[i] = 0;
      }
    }
    if (valPos == numberOfValues) { // once we reach the 'numberOfValues' print them out
      valPos = 0; // reset value position
      for (byte i = 0; i < numberOfValues; i++) {
        Serial.print(F("values[")); Serial.print(i); Serial.print(F("] = "));Serial.println(values[i]);
      }
    }
  }
}

thank you so much

KawasakiZx10r:
I made such a function many times. I added notes for you so you can adjust it easily. You really did over complicate your code, I hope you learn something from my attempt. To use my code you must separate each value with a comma ‘CSV’ format with the final value ending either with a comma or a newline. You can test this code with your Serial monitor, just place serialDebug(); in the loop() function if you wish to keep sending new commands over time, or in the setup() if you only need to send values once.

void serialDebug () {

const byte numberOfValues = 4; // number of values to be recived
  byte charPos = 0, valPos = 0; // ESP32 requires values to be preset // I.E. ‘valPos’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
  const byte maxChars = 8; // max digits / chars, includes decimal points and minus signs ! I.E. -123.123 is 8 chars
  char buffer[maxChars];
  float values[numberOfValues]; // allows for byte, int or float, else change data type to what you require
  while (Serial.available() > 0) {
    char c = Serial.read();
    if ((c >= ‘0’ && c <= ‘9’) || c == ‘-’ || c == ‘.’) // except just numbers, minus sign and decimal point
      buffer[charPos++] = c;
    else if (c == ‘,’ || c == ‘\n’) { // values must be seperated by a comma, and the final value can end with a new line or comma.
      values[valPos++] = atof(buffer); // if just using ints change to atoi
      charPos = 0; // reset char position
      for (byte i = 0; i < maxChars; i++) { // clear char buffer
        buffer[i] = 0;
      }
    }
    if (valPos == numberOfValues) { // once we reach the ‘numberOfValues’ print them out
      valPos = 0; // reset value position
      for (byte i = 0; i < numberOfValues; i++) {
        Serial.print(F(“values[”)); Serial.print(i); Serial.print(F("] = "));Serial.println(values[i]);
      }
    }
  }
}

i tride this code and it s working thanks for your help. i am just wondering if i want read more than 4 values what should I change?

i am just wondering if i want read more than 4 values what should I change?

This would seem to be a good place to start. don't you think ?

    byte charPos = 0, valPos = 0; // ESP32 requires values to be preset // I.E. 'valPos' may be used uninitialized in this function [-Werror=maybe-uninitialized]
  const byte maxChars = 8; // max digits / chars, includes decimal points and minus signs ! I.E. -123.123 is 8 chars
  char buffer[maxChars];

UKHeliBob:
This would seem to be a good place to start. don’t you think ?

    byte charPos = 0, valPos = 0; // ESP32 requires values to be preset // I.E. 'valPos' may be used uninitialized in this function [-Werror=maybe-uninitialized]

const byte maxChars = 8; // max digits / chars, includes decimal points and minus signs ! I.E. -123.123 is 8 chars
  char buffer[maxChars];

i think so . But first what “const byte maxchars = 8” what does it mean? I could not understand some things
i

HUSAMALTIN:
i think so . But first what "const byte maxchars = 8" what does it mean? I could not understand some things
i

Did you read the comments ? They even give an example

UKHeliBob:
Did you read the comments ? They even give an example

Yes I have read the comments but could not understand some parts

to adjust the number of values change the numberOfValues to the number of Values to be received, and maxchars is the number of digits including decimal point and minus sign if the number is negative or a float.

maxChars is a char buffer for the numbers received "NOT THE TOTAL ARRAY SIZE" so if you have an CSV format such as 123, 456, 789, and all numbers are just 3 digits long and all positive then maxChars could be set to 3 but your then be limited to 3 digits and no decimal point or minus sign.

Else for instance -123.456, -123.456, -123.456, -123.456 that would be 8 chars "note the minus sign and decimal point", i say chars as again numbers include other signs "minus, decimal points..." I hope i drilled that into you :stuck_out_tongue:

So in short maxChars in the max size of any given number in the array, NOT the total size of the array. If number are larger than 8 digits including decimal point and minus sign increase it to what ever 16 for instance, and numberOfValues is the numberOfValues...

KawasakiZx10r:
to adjust the number of values change the numberOfValues to the number of Values to be received, and maxchars is the number of digits including decimal point and minus sign if the number is negative or a float.

maxChars is a char buffer for the numbers received "NOT THE TOTAL ARRAY SIZE" so if you have an CSV format such as 123, 456, 789, and all numbers are just 3 digits long and all positive then maxChars could be set to 3 but your then be limited to 3 digits and no decimal point or minus sign.

Else for instance -123.456, -123.456, -123.456, -123.456 that would be 8 chars "note the minus sign and decimal point", i say chars as again numbers include other signs "minus, decimal points..." I hope i drilled that into you :stuck_out_tongue:

So in short maxChars in the max size of any given number in the array, NOT the total size of the array. If number are larger than 8 digits including decimal point and minus sign increase it to what ever 16 for instance, and numberOfValues is the numberOfValues...

i got it thank you