Trying to segregate serial data and drive outputs

Good Day,

I am reading a serial value eg: ?A123.45?B246.80, and I am trying to read them as 123.45 and 246.80.
The setup is almost there, but for some reason, the "B value" 246.80 value is being ignored.

Appreciate the help....

Code is mostly credited to jimspage.co.nz

void QUESTION(){ // The first identifier was "?"
CodeIn = getChar(); // Get another character
switch(CodeIn) { // Now lets find what to do with it

case'A':                                  // Found the second identifier ("N" Eng 2 Temp position)
  egtb = "";
  egtb += getChar();
  egtb += getChar();
  egtb += getChar();
  int egtbi = egtb.toInt();               // convert it to an integer
  egtbangle = egtbi*5;                    // Range calculation to suit servo 180deg.
  egtbServo.write(egtbangle);             // Scaled output to servo
                                                 // WORKS UP TO HERE,
   break;                                  //THIS IS WHERE THE PROGRAM STOPS??? NEED HELP. THANKS
   
case'B':                                  // Found the second identifier ("V" Fuel Flow 1 position)
  ffa = "";
  ffa += getChar();
  ffa += getChar();
  ffa += getChar();
  ffa += getChar();
  ffa += getChar();
  ffa += getChar();
  int ffai = ffa.toInt();                 // convert it to an integer
  ffaangle = ffai+ffai*(1./2.);           // Range calculation to suit servo 180deg.
  ffaServo.write(ffaangle);               // Scaled output to servo

toInt() likely returns 0 if you give it a floating point string.
Alan

You could grab the entire string into a c string buffer. Then use strtok() to slice it up. That would give you the two numbers preceded by the A & B letters. At that point you can point at the next char and pass that into atof() for a float version of the number.

-jim lee

Thanks for the response,

The case A works, writing to the servo. When I // the case A code, case B works, writing to the servo.

It seems that the first expression "A" is read and processed, but the second expression "B" is ignored?

Appreciate your thoughts?
Thanks

Hi, @mapain
Welcome to the forum.

To add code please click this link;

What model Arduino are you using?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Reading in the whole set of chars first is that way to go.
Arduino Software Solutions has numerous examples of reading from Serial and then parsing.
Using atof() or my SafeString toFloat() methods are the way to go after parsing out numbers

A fuller sketch would help us sort out what is happening.

Good Day Tom,
Thanks for the heads up on the forum.
As asked for I am using Arduino UNO.

I have only spent a few weeks on the Arduino programming, doing excercises etc., but challenged by what I hope to achieve within my project, just don't quite know how?

Cheers,
Mark

#include <Servo.h>

Servo flapsServo;
Servo proparpmServo;
Servo egtbServo;
Servo ffaServo;
Servo ffbServo;
Servo adfServo;

int CodeIn; // used on all serial reads
int KpinNo;
int Koutpin;

String flaps;
String proparpm;
String egtb;
String ffa;
String ffb;
String adf;

int flapsangle;
int proparpmangle;
int egtbangle;
int ffaangle;
int ffbangle;
int adfiangle;

String KoldpinStateSTR, KpinStateSTR, Kstringnewstate, Kstringoldstate;

void setup()
{
Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";

for (int KoutPin = 2; KoutPin < 20; KoutPin++) // Get all the pins ready for "Keys"
{
pinMode(KoutPin, INPUT);
digitalWrite(KoutPin, HIGH);
}
Serial.begin(115200);

pinMode(3, OUTPUT); // For the flap position servo.
flapsServo.attach(3);
pinMode(5, OUTPUT); // For the eng 1 rpm servo.
proparpmServo.attach(5);
pinMode(6, OUTPUT); // For the egt 2 servo.
egtbServo.attach(6);
pinMode(9, OUTPUT); // For fuel flow 1 servo.
ffaServo.attach(9);
pinMode(10, OUTPUT); // For fuel flow 2 servo.
ffbServo.attach(10);
pinMode(11, OUTPUT); // For adf servo.
adfServo.attach(11);

}

void loop() {
{KEYS();} //Check the "keys" section
if (Serial.available()) {
CodeIn = getChar();
if (CodeIn == '=') {EQUALS();} // The first identifier is "="
if (CodeIn == '<') {LESSTHAN();} // The first identifier is "<"
if (CodeIn == '?') {QUESTION();} // The first identifier is "?"
if (CodeIn == '/') {SLASH();} // The firt identifier is "/" (Annunciators)
}

}

char getChar() // Get a character from the serial buffer
{
while(Serial.available() == 0); // wait for data
return((char)Serial.read()); // Thanks Doug
}

void EQUALS(){ // The first identifier was "="
CodeIn = getChar(); // Get another character
switch(CodeIn) { // Now lets find what to do with it
case'A': // Found the second identifier
// Do something
break;

case'B':
                                          // Do something
break;
 
case'C':
                                          // Do something
break;
 }

}

void LESSTHAN(){ // The first identifier was "<"
CodeIn = getChar(); // Get another character
switch(CodeIn) { // Now lets find what to do with it
case'A': // Found the second identifier
// Do something
break;

case'B':
                                          // Do something
break;
 
case'G':;                                  //("G" Flaps position)
  flaps = "";
  flaps += getChar();
  flaps += getChar();
  flaps += getChar();
  int flapsi = flaps.toInt();             // Convert to an integer.
  flapsangle = flapsi+flapsi*(4./5.);     // Manual correction to suit servo 180 deg.
  flapsServo.write(flapsangle);           // Scaled output to servo.

break;

case'T':;                                  //("T" Prop 1 rpm position)
  proparpm = "";
  proparpm += getChar();
  proparpm += getChar();
  proparpm += getChar();
  proparpm += getChar();
  proparpm += getChar();
  int proparpmi = proparpm.toInt();         // Convert to an integer.
  proparpmangle = proparpmi*1./200.;        // Manual correction to suit servo 180 deg.
  proparpmServo.write(proparpmangle);       // Scaled output to servo.
  
  break;
 }

}

void QUESTION(){ // The first identifier was "?"
CodeIn = getChar(); // Get another character
switch(CodeIn) { // Now lets find what to do with it

case'K':                                  // Found the second identifier ("N" Eng 2 Temp position)
  egtb = "";
  egtb += getChar();
  egtb += getChar();
  egtb += getChar();
  int egtbi = egtb.toInt();               // convert it to an integer
  egtbangle = egtbi*5;                    // Range calculation to suit servo 180deg.
  egtbServo.write(egtbangle);             // Scaled output to servo

  ;
   
case'V':                                  // Found the second identifier ("V" Fuel Flow 1 position)
  ffa = "";
  ffa += getChar();
  ffa += getChar();
  ffa += getChar();
  ffa += getChar();
  ffa += getChar();
  ffa += getChar();
  int ffai = ffa.toInt();                 // convert it to an integer
  ffaangle = ffai+ffai*(1./2.);           // Range calculation to suit servo 180deg.
  ffaServo.write(ffaangle);               // Scaled output to servo

   break;
   
  case'W':                                // Found the second identifier ("W" Fuel Flow 2 position)
  ffb = "";
  ffb += getChar();
  ffb += getChar();
  ffb += getChar();
  ffb += getChar();
  ffb += getChar();
  ffb += getChar();
  int ffbi = ffb.toInt();                 // convert it to an integer (Thanks Phill)
  ffbangle = ffbi+ffbi*(1./2.);           // Range calculation to suit servo 180deg.
  ffbServo.write(ffbangle);               // Scaled output to servo
                                       
  break;

case'n':                                  // Found the second identifier ("n" ADF position)
  adf = "";
  adf += getChar();
  adf += getChar();
  adf += getChar();
  adf += getChar();
  adf += getChar();
  int adfi = adf.toInt();                 // Convert to an integer.
  adfiangle = adfi/2;                     // Range calculation to suit servo 180deg.
  adfServo.write(adfiangle);              // Scaled output to servo.
                                         
  break;

  
 }

}
void SLASH(){ // The first identifier was "/" (Annunciator)
// Do something
}
void KEYS()
{
Kstringnewstate = "";
for (int KpinNo = 2; KpinNo < 20; KpinNo++){
KpinStateSTR = String(digitalRead(KpinNo));
KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));
if (KpinStateSTR != KoldpinStateSTR)
{
if (KpinNo != 13){
Serial.print ("D");
if (KpinNo < 10) Serial.print ("0");
Serial.print (KpinNo);
Serial.println (KpinStateSTR);
}
}
Kstringnewstate += KpinStateSTR;
}
Kstringoldstate = Kstringnewstate;
}

Can you please edit your post with the code, select all code and click </> (that will place code tags around it). Next save the edited post.

Code tags will make it easier to copy and less annoying to read.

Here is the code in one piece
segregateSerial.ino (6.1 KB)

Question. I can see where you read a char and identify the start of a message. How do you know when the message ends / should end?

Thanks for the help,

As done below....

[code]
#include <Servo.h>

Servo flapsServo;
Servo proparpmServo;
Servo egtbServo;
Servo ffaServo;
Servo ffbServo;
Servo adfServo;


int CodeIn;                  // used on all serial reads
int KpinNo; 
int Koutpin;

String flaps;
String proparpm;
String egtb;
String ffa;
String ffb;
String adf;

int flapsangle;
int proparpmangle;
int egtbangle;
int ffaangle;
int ffbangle;
int adfiangle;

String KoldpinStateSTR, KpinStateSTR, Kstringnewstate, Kstringoldstate;

void setup() 
  {
  Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
  
  for (int KoutPin = 2; KoutPin < 20; KoutPin++)          // Get all the pins ready for "Keys"  
  {
    pinMode(KoutPin, INPUT);
    digitalWrite(KoutPin, HIGH);  
  }
 Serial.begin(115200); 
 
 pinMode(3, OUTPUT);         // For the flap position servo.
 flapsServo.attach(3);
 pinMode(5, OUTPUT);         // For the eng 1 rpm servo.
 proparpmServo.attach(5);
 pinMode(6, OUTPUT);         // For the egt 2 servo.
 egtbServo.attach(6);
 pinMode(9, OUTPUT);         // For fuel flow 1 servo.
 ffaServo.attach(9); 
 pinMode(10, OUTPUT);        // For fuel flow 2 servo.
 ffbServo.attach(10);
 pinMode(11, OUTPUT);        // For adf servo.
 adfServo.attach(11);
  
}

void loop() {
  {KEYS();}                                   //Check the "keys" section
  if (Serial.available()) {
    CodeIn = getChar();
    if (CodeIn == '=') {EQUALS();}            // The first identifier is "="
    if (CodeIn == '<') {LESSTHAN();}          // The first identifier is "<"
    if (CodeIn == '?') {QUESTION();}          // The first identifier is "?"
    if (CodeIn == '/') {SLASH();}             // The firt identifier is "/" (Annunciators)
  }

}

char getChar()                                // Get a character from the serial buffer
{
  while(Serial.available() == 0);             // wait for data
  return((char)Serial.read());                // Thanks Doug
}

void EQUALS(){                                // The first identifier was "="
 CodeIn = getChar();                          // Get another character
  switch(CodeIn) {                            // Now lets find what to do with it
    case'A':                                  // Found the second identifier
                                              // Do something
    break;
     
    case'B':
                                              // Do something
    break;
     
    case'C':
                                              // Do something
    break;
     }
}

void LESSTHAN(){                              // The first identifier was "<"
CodeIn = getChar();                           // Get another character
  switch(CodeIn) {                            // Now lets find what to do with it
    case'A':                                  // Found the second identifier
                                              // Do something
    break;
     
    case'B':
                                              // Do something
    break;
     
    case'G':;                                  //("G" Flaps position)
      flaps = "";
      flaps += getChar();
      flaps += getChar();
      flaps += getChar();
      int flapsi = flaps.toInt();             // Convert to an integer.
      flapsangle = flapsi+flapsi*(4./5.);     // Manual correction to suit servo 180 deg.
      flapsServo.write(flapsangle);           // Scaled output to servo.

    break;
    
    case'T':;                                  //("T" Prop 1 rpm position)
      proparpm = "";
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      int proparpmi = proparpm.toInt();         // Convert to an integer.
      proparpmangle = proparpmi*1./200.;        // Manual correction to suit servo 180 deg.
      proparpmServo.write(proparpmangle);       // Scaled output to servo.
      
      break;
     }
   }

void QUESTION(){                              // The first identifier was "?"
CodeIn = getChar();                           // Get another character
  switch(CodeIn) {                            // Now lets find what to do with it
    
    case'K':                                  // Found the second identifier ("N" Eng 2 Temp position)
      egtb = "";
      egtb += getChar();
      egtb += getChar();
      egtb += getChar();
      int egtbi = egtb.toInt();               // convert it to an integer
      egtbangle = egtbi*5;                    // Range calculation to suit servo 180deg.
      egtbServo.write(egtbangle);             // Scaled output to servo

      ;
       
    case'V':                                  // Found the second identifier ("V" Fuel Flow 1 position)
      ffa = "";
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      int ffai = ffa.toInt();                 // convert it to an integer
      ffaangle = ffai+ffai*(1./2.);           // Range calculation to suit servo 180deg.
      ffaServo.write(ffaangle);               // Scaled output to servo

       break;
       
      case'W':                                // Found the second identifier ("W" Fuel Flow 2 position)
      ffb = "";
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      int ffbi = ffb.toInt();                 // convert it to an integer (Thanks Phill)
      ffbangle = ffbi+ffbi*(1./2.);           // Range calculation to suit servo 180deg.
      ffbServo.write(ffbangle);               // Scaled output to servo
                                           
      break;

    case'n':                                  // Found the second identifier ("n" ADF position)
      adf = "";
      adf += getChar();
      adf += getChar();
      adf += getChar();
      adf += getChar();
      adf += getChar();
      int adfi = adf.toInt();                 // Convert to an integer.
      adfiangle = adfi/2;                     // Range calculation to suit servo 180deg.
      adfServo.write(adfiangle);              // Scaled output to servo.
                                             
      break;

      
     }
}
void SLASH(){                                 // The first identifier was "/" (Annunciator)
                                              // Do something
}
void KEYS() 
{
  Kstringnewstate = "";
  for (int KpinNo = 2; KpinNo < 20; KpinNo++){
    KpinStateSTR = String(digitalRead(KpinNo)); 
    KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));
    if (KpinStateSTR != KoldpinStateSTR)
    {
      if (KpinNo != 13){
      Serial.print ("D"); 
      if (KpinNo < 10) Serial.print ("0");
      Serial.print (KpinNo);
      Serial.println (KpinStateSTR);
      }
    }
    Kstringnewstate += KpinStateSTR;
  }
  Kstringoldstate = Kstringnewstate;
}
[/code]

I would suggest something like

String inputStr;

void setup()
{
  Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";

  for (int KoutPin = 2; KoutPin < 20; KoutPin++) // Get all the pins ready for "Keys"
  {
    pinMode(KoutPin, INPUT);
    digitalWrite(KoutPin, HIGH);
  }
  Serial.begin(115200);

  pinMode(3, OUTPUT); // For the flap position servo.
  flapsServo.attach(3);
  pinMode(5, OUTPUT); // For the eng 1 rpm servo.
  proparpmServo.attach(5);
  pinMode(6, OUTPUT); // For the egt 2 servo.
  egtbServo.attach(6);
  pinMode(9, OUTPUT); // For fuel flow 1 servo.
  ffaServo.attach(9);
  pinMode(10, OUTPUT); // For fuel flow 2 servo.
  ffbServo.attach(10);
  pinMode(11, OUTPUT); // For adf servo.
  adfServo.attach(11);

  Serial.setTimeout(10); // short timeout
  inputStr.reserve(40); // enough for the expected input

}

void loop() {
  {
    KEYS(); //Check the "keys" section
  }
  if (Serial.available()) {
    inputStr = Serial.readString(); // reads until nothing arrives for 10mS
    char codeIn = inputStr[0];
    //CodeIn = getChar();
    if (codeIn == '=') {
      EQUALS(); // The first identifier is "="
    }
    if (codeIn == '<') {
      LESSTHAN(); // The first identifier is "<"
    }
    if (codeIn == '?') {
      QUESTION(); // The first identifier is "?"
    }
    if (codeIn == '/') {
      SLASH(); // The firt identifier is "/" (Annunciators)
    }
  }
}

and then in the EQUALS etc just replace the getChar with inputStr[ .. ]

Have you confirmed the behaviour of toInt() on a float string? I suspect it will be "0".

When you receive data from serial, how do you ensure it always has 3 leading digits and 3 trailing digits? For example will the value 1.0 will be sent as 001.000? If it is not then the following ?Bxxx.xxx will be lost (eaten by the unused getChar() (s)) .

There are no floats in your code so why convert an int into float and then implicitly back again:
ffaangle = ffai+ffai*(1./2.);
why not:
ffaangle = ffai+ffai/2;
or:
ffaangle = ffai*3/2;

At this point I would follow jimLee's advice.

Regards Alan

Good Day,

I am assuming that it ends based on how many integers I am reading, eg for proparpm, there are 5 getchar, based on the read value of 5 integers eg: 12532.

Taken these values, converted them for servo output, and I get a proper result, which indicates my values are good.

Not sure whether it is looking for additional values?

Thanks
Mark

[code]
#include <Servo.h>

Servo flapsServo;
Servo proparpmServo;
Servo egtbServo;
Servo ffaServo;
Servo ffbServo;
Servo adfServo;


int CodeIn;                  // used on all serial reads
int KpinNo; 
int Koutpin;

String flaps;
String proparpm;
String egtb;
String ffa;
String ffb;
String adf;

int flapsangle;
int proparpmangle;
int egtbangle;
int ffaangle;
int ffbangle;
int adfiangle;

String KoldpinStateSTR, KpinStateSTR, Kstringnewstate, Kstringoldstate;

void setup() 
  {
  Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
  
  for (int KoutPin = 2; KoutPin < 20; KoutPin++)          // Get all the pins ready for "Keys"  
  {
    pinMode(KoutPin, INPUT);
    digitalWrite(KoutPin, HIGH);  
  }
 Serial.begin(115200); 
 
 pinMode(3, OUTPUT);         // For the flap position servo.
 flapsServo.attach(3);
 pinMode(5, OUTPUT);         // For the eng 1 rpm servo.
 proparpmServo.attach(5);
 pinMode(6, OUTPUT);         // For the egt 2 servo.
 egtbServo.attach(6);
 pinMode(9, OUTPUT);         // For fuel flow 1 servo.
 ffaServo.attach(9); 
 pinMode(10, OUTPUT);        // For fuel flow 2 servo.
 ffbServo.attach(10);
 pinMode(11, OUTPUT);        // For adf servo.
 adfServo.attach(11);
  
}

void loop() {
  {KEYS();}                                   //Check the "keys" section
  if (Serial.available()) {
    CodeIn = getChar();
    if (CodeIn == '=') {EQUALS();}            // The first identifier is "="
    if (CodeIn == '<') {LESSTHAN();}          // The first identifier is "<"
    if (CodeIn == '?') {QUESTION();}          // The first identifier is "?"
    if (CodeIn == '/') {SLASH();}             // The firt identifier is "/" (Annunciators)
  }

}

char getChar()                                // Get a character from the serial buffer
{
  while(Serial.available() == 0);             // wait for data
  return((char)Serial.read());                // Thanks Doug
}

void EQUALS(){                                // The first identifier was "="
 CodeIn = getChar();                          // Get another character
  switch(CodeIn) {                            // Now lets find what to do with it
    case'A':                                  // Found the second identifier
                                              // Do something
    break;
     
    case'B':
                                              // Do something
    break;
     
    case'C':
                                              // Do something
    break;
     }
}

void LESSTHAN(){                              // The first identifier was "<"
CodeIn = getChar();                           // Get another character
  switch(CodeIn) {                            // Now lets find what to do with it
    case'A':                                  // Found the second identifier
                                              // Do something
    break;
     
    case'B':
                                              // Do something
    break;
     
    case'G':;                                  //("G" Flaps position)
      flaps = "";
      flaps += getChar();
      flaps += getChar();
      flaps += getChar();
      int flapsi = flaps.toInt();             // Convert to an integer.
      flapsangle = flapsi+flapsi*(4./5.);     // Manual correction to suit servo 180 deg.
      flapsServo.write(flapsangle);           // Scaled output to servo.

    break;
    
    case'T':;                                  //("T" Prop 1 rpm position)
      proparpm = "";
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      int proparpmi = proparpm.toInt();         // Convert to an integer.
      proparpmangle = proparpmi*1./200.;        // Manual correction to suit servo 180 deg.
      proparpmServo.write(proparpmangle);       // Scaled output to servo.
      
      break;
     }
   }

void QUESTION(){                              // The first identifier was "?"
CodeIn = getChar();                           // Get another character
  switch(CodeIn) {                            // Now lets find what to do with it
    
    case'K':                                  // Found the second identifier ("N" Eng 2 Temp position)
      egtb = "";
      egtb += getChar();
      egtb += getChar();
      egtb += getChar();
      int egtbi = egtb.toInt();               // convert it to an integer
      egtbangle = egtbi*5;                    // Range calculation to suit servo 180deg.
      egtbServo.write(egtbangle);             // Scaled output to servo

      ;
       
    case'V':                                  // Found the second identifier ("V" Fuel Flow 1 position)
      ffa = "";
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      int ffai = ffa.toInt();                 // convert it to an integer
      ffaangle = ffai+ffai*(1./2.);           // Range calculation to suit servo 180deg.
      ffaServo.write(ffaangle);               // Scaled output to servo

       break;
       
      case'W':                                // Found the second identifier ("W" Fuel Flow 2 position)
      ffb = "";
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      int ffbi = ffb.toInt();                 // convert it to an integer (Thanks Phill)
      ffbangle = ffbi+ffbi*(1./2.);           // Range calculation to suit servo 180deg.
      ffbServo.write(ffbangle);               // Scaled output to servo
                                           
      break;

    case'n':                                  // Found the second identifier ("n" ADF position)
      adf = "";
      adf += getChar();
      adf += getChar();
      adf += getChar();
      adf += getChar();
      adf += getChar();
      int adfi = adf.toInt();                 // Convert to an integer.
      adfiangle = adfi/2;                     // Range calculation to suit servo 180deg.
      adfServo.write(adfiangle);              // Scaled output to servo.
                                             
      break;

      
     }
}
void SLASH(){                                 // The first identifier was "/" (Annunciator)
                                              // Do something
}
void KEYS() 
{
  Kstringnewstate = "";
  for (int KpinNo = 2; KpinNo < 20; KpinNo++){
    KpinStateSTR = String(digitalRead(KpinNo)); 
    KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));
    if (KpinStateSTR != KoldpinStateSTR)
    {
      if (KpinNo != 13){
      Serial.print ("D"); 
      if (KpinNo < 10) Serial.print ("0");
      Serial.print (KpinNo);
      Serial.println (KpinStateSTR);
      }
    }
    Kstringnewstate += KpinStateSTR;
  }
  Kstringoldstate = Kstringnewstate;
}
[/code]

Thanks,

I will give it a go, makes good sense.

I will let you know how I get on,

Appreciated,
Mark

@Good Day Jim,

Pardon my inexperience, I searched the mentioned strtok() and atof() function to gain some understanding, and have not found any reference to them? Much the same, I have no idea how to include this function to my code.

Could you please be kind enough to include an example in my code,

Thank you,
Appreciated

[code]
#include <Servo.h>

Servo flapsServo;
Servo proparpmServo;
Servo egtbServo;
Servo ffaServo;
Servo ffbServo;
Servo adfServo;

int CodeIn; // used on all serial reads
int KpinNo;
int Koutpin;

String flaps;
String proparpm;
String egtb;
String ffa;
String ffb;
String adf;

int flapsangle;
int proparpmangle;
int egtbangle;
int ffaangle;
int ffbangle;
int adfiangle;

String KoldpinStateSTR, KpinStateSTR, Kstringnewstate, Kstringoldstate;

void setup()
{
  Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";

  for (int KoutPin = 2; KoutPin < 20; KoutPin++) // Get all the pins ready for "Keys"
  {
    pinMode(KoutPin, INPUT);
    digitalWrite(KoutPin, HIGH);
  }
  Serial.begin(115200);

  pinMode(3, OUTPUT); // For the flap position servo.
  flapsServo.attach(3);
  pinMode(5, OUTPUT); // For the eng 1 rpm servo.
  proparpmServo.attach(5);
  pinMode(6, OUTPUT); // For the egt 2 servo.
  egtbServo.attach(6);
  pinMode(9, OUTPUT); // For fuel flow 1 servo.
  ffaServo.attach(9);
  pinMode(10, OUTPUT); // For fuel flow 2 servo.
  ffbServo.attach(10);
  pinMode(11, OUTPUT); // For adf servo.
  adfServo.attach(11);

}

void loop() {
  {
    KEYS(); //Check the "keys" section
  }
  if (Serial.available()) {
    CodeIn = getChar();
    if (CodeIn == '=') {
      EQUALS(); // The first identifier is "="
    }
    if (CodeIn == '<') {
      LESSTHAN(); // The first identifier is "<"
    }
    if (CodeIn == '?') {
      QUESTION(); // The first identifier is "?"
    }
    if (CodeIn == '/') {
      SLASH(); // The firt identifier is "/" (Annunciators)
    }
  }

}

char getChar() // Get a character from the serial buffer
{
  while (Serial.available() == 0); // wait for data
  return ((char)Serial.read()); // Thanks Doug
}

void EQUALS() { // The first identifier was "="
  CodeIn = getChar(); // Get another character
  switch (CodeIn) { // Now lets find what to do with it
    case'A': // Found the second identifier
      // Do something
      break;

    case'B':
      // Do something
      break;

    case'C':
      // Do something
      break;
  }
}

void LESSTHAN() { // The first identifier was "<"
  CodeIn = getChar(); // Get another character
  switch (CodeIn) { // Now lets find what to do with it
    case'A': // Found the second identifier
      // Do something
      break;

    case'B':
      // Do something
      break;

    case'G':                                  //("G" Flaps position)
      flaps = "";
      flaps += getChar();
      flaps += getChar();
      flaps += getChar();
      int flapsi = flaps.toInt();             // Convert to an integer.
      flapsangle = flapsi + flapsi * (4. / 5.); // Manual correction to suit servo 180 deg.
      flapsServo.write(flapsangle);           // Scaled output to servo.

      break;

    case'T':                                  //("T" Prop 1 rpm position)
      proparpm = "";
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      proparpm += getChar();
      int proparpmi = proparpm.toInt();         // Convert to an integer.
      proparpmangle = proparpmi * 1. / 200.;    // Manual correction to suit servo 180 deg.
      proparpmServo.write(proparpmangle);       // Scaled output to servo.

      break;
  }
}

void QUESTION() { // The first identifier was "?"
  CodeIn = getChar(); // Get another character
  switch (CodeIn) { // Now lets find what to do with it

    case'K':                                  // Found the second identifier ("N" Eng 2 Temp position)
      egtb = "";
      egtb += getChar();
      egtb += getChar();
      egtb += getChar();
      int egtbi = egtb.toInt();               // convert it to an integer
      egtbangle = egtbi * 5;                  // Range calculation to suit servo 180deg.
      egtbServo.write(egtbangle);             // Scaled output to servo

      ;

    case'V':                                  // Found the second identifier ("V" Fuel Flow 1 position)
      ffa = "";
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      ffa += getChar();
      int ffai = ffa.toInt();                 // convert it to an integer
      ffaangle = ffai + ffai * (1. / 2.);     // Range calculation to suit servo 180deg.
      ffaServo.write(ffaangle);               // Scaled output to servo

      break;

    case'W':                                // Found the second identifier ("W" Fuel Flow 2 position)
      ffb = "";
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      ffb += getChar();
      int ffbi = ffb.toInt();                 // convert it to an integer (Thanks Phill)
      ffbangle = ffbi + ffbi * (1. / 2.);     // Range calculation to suit servo 180deg.
      ffbServo.write(ffbangle);               // Scaled output to servo

      break;

    case'n':                                  // Found the second identifier ("n" ADF position)
      adf = "";
      adf += getChar();
      adf += getChar();
      adf += getChar();
      adf += getChar();
      adf += getChar();
      int adfi = adf.toInt();                 // Convert to an integer.
      adfiangle = adfi / 2;                   // Range calculation to suit servo 180deg.
      adfServo.write(adfiangle);              // Scaled output to servo.

      break;


  }
}
void SLASH() { // The first identifier was "/" (Annunciator)
  // Do something
}
void KEYS()
{
  Kstringnewstate = "";
  for (int KpinNo = 2; KpinNo < 20; KpinNo++) {
    KpinStateSTR = String(digitalRead(KpinNo));
    KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 2));
    if (KpinStateSTR != KoldpinStateSTR)
    {
      if (KpinNo != 13) {
        Serial.print ("D");
        if (KpinNo < 10) Serial.print ("0");
        Serial.print (KpinNo);
        Serial.println (KpinStateSTR);
      }
    }
    Kstringnewstate += KpinStateSTR;
  }
  Kstringoldstate = Kstringnewstate;
}
[/code]

https://www.cplusplus.com/reference/cstring/strtok/

https://www.cplusplus.com/reference/cstdlib/atof/

-jim lee

Thanks for the links,

Got some reading and homework for the week.

Appreciate the help,
Regards,
Mark

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.