Could this be written smarter???

Hi Group,

After 5 years of no programmin at all I am a bit rusty so I need some advise on the code here. firdt the def's

int posXaxis = 90;    // variable to store the servo position
int posYaxis = 90;    // variable to store the servo position
String axisValueString ;

and from the main-loop this routine is called

/**===========================================
 * This routine wil decode a received string 
 * in this format "123.4,567.8\n" into two   
 * numbers to control a servo
  *===========================================**/

void readData(char ch) {
  if (ch == ',') {
    // create servo-X value to move to
    posXaxis = 0;
    posXaxis = axisValueString.toInt();
    axisValueString = "";
  } else {
    // skip decimal point
    if (ch != '.') {
      axisValueString += ch;
    } 
    if (ch == '\n') {
    // create servo-Y value to move to
    posYaxis = 0;
    posYaxis = axisValueString.toInt();
    axisValueString = "";
    }
  }
}

Maybe it can be written smarter???
Harry

Why is it skipping the decimal points? Is it guaranteed that there will be only ever be one digit after?

Define "smart code"! Is it the shortest code, the fastest code or the most understandable code some years later?
I've seen "smart code" that almost nobody understand, almost nobody could maintain, update...

I would not use a ‘String’ just for the conversion. I would add each digit to the number individually.

/**===========================================
   This routine wil decode a received string
   in this format "123.4,567.8\n" into two
   numbers to control a servo
   ===========================================**/
void readData(char ch)
{
  static unsigned int value = 0;


  if (ch >= '0' && ch <= '9')
    value = (value * 10) + (ch - '0');


  if (ch == ',')
  {
    posXaxis = value;
    value = 0;
  }
  
  if (ch == '\n')
  {
    posYaxis = value;
    value = 0;
  }
}
posXaxis = Serial.parseInt(SKIP_ALL, '.'); // read an int, skipping any preceding \n, ignoring decimal point
posYaxis = Serial.parseInt(SKIP_ALL, '.'); // read an int, skipping the comma, ignoring decimal point

PaulRB:

posXaxis = Serial.parseInt(SKIP_ALL, '.'); // read an int, skipping any preceding \n, ignoring decimal point

posYaxis = Serial.parseInt(SKIP_ALL, '.'); // read an int, skipping the comma, ignoring decimal point



Where to put this code

In a part of your sketch that you are keeping secret from us.

johnwasser:
I would not use a ‘String’ just for the conversion. I would add each digit to the number individually.

/**===========================================

This routine wil decode a received string
  in this format “123.4,567.8\n” into two
  numbers to control a servo
  ===========================================**/
void readData(char ch)
{
  static unsigned int value = 0;

if (ch >= ‘0’ && ch <= ‘9’)
    value = (value * 10) + (ch - ‘0’);

if (ch == ‘,’)
  {
    posXaxis = value;
    value = 0;
  }
 
  if (ch == ‘\n’)
  {
    posYaxis = value;
    value = 0;
  }
}



Tested the code in my sketch true the terminal, first 0 then 90 and 180 degrees.
X=>0/0
Y=>0/0
==Move Servo==
New position deg.=>0/0
New position msec=>1000/1000
X=>900/900
Y=>900/900
==Move Servo==
New position deg.=>900/900
New position msec=>1500/1000
X=>1800/1800
Y=>1800/1800
==Move Servo==
New position deg.=>1800/1800
New position msec=>2000/1000

jt looks that the X goes OK but the Y doenst. Here's my main loop:


void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    readData(Serial.read());
  } else {
    if (valueX == true && valueY == true) {
      Serial.println("==Move Servo==");
      Serial << “New position deg.=>” << posXaxis << “/” << posYaxis << endl;
      Serial << “New position msec=>” << degree2ms(posXaxis / 10) << “/” << degree2ms(newYAxis / 10) << endl;
      servoXaxis.writeMicroseconds(degree2ms(posXaxis));              // tell servo to go to position in variable ‘pos’

valueX = false;
      valueY = false;
    }
  }
}




It must be simple, fore sure.Harry

Most bizarre post ever...
I figured it out. You got your quote tags in the wrong place. Very confusing!

You don't show any code that would set 'valueX' or 'valueY' to true so I'm surprised it receives input more than once.

Tested the code in my sketch true the terminal, first 0 then 90 and 180 degrees.

So "0.0,0.0\n", "90.0,90.0\n" and "180.0,180.0\n"?

Hi,
We need to see ALL your code, from the first line to the last.
What model Arduino are you using?
What are you trying to do with your code?
What hardware is connected?

Can you please post your circuit diagram to help us read your code?

Thanks.. Tom... :slight_smile:

  // send data only when you receive data:
  if (Serial.available() >= 12) {
    posXaxis = Serial.parseInt(SKIP_ALL, '.'); // read an int, skipping any preceding \n, ignoring decimal point
    posYaxis = Serial.parseInt(SKIP_ALL, '.'); // read an int, skipping the comma, ignoring decimal point
  } else {