Arduino Forum

Topics => Robotics => Topic started by: Railroader on May 16, 2019, 12:27 am

Title: Trouble using Serial.read()
Post by: Railroader on May 16, 2019, 12:27 am
Using Serial monitor I send commands to the UNO.
If I send the string "X123Cr" it works well but if I send "XCr" and then "123Cr" only "1" is received.
 What am I missing?

The code is too large to attach using code tags. I try to attach the central piece.

Start looking at functiion Figures and case "S". If I then send the string X123 Cr it works, I get the numeric of 123, but when I send X Cr and then 123 Cr I only get the number 1 when calling get_num().
What am I missing?




Code: [Select]
long get_num()
{
  long get_num_sum = 0;
  int sign = 1;
  char got_char;
  while (!Serial.available() );
  got_char = Serial.read();
  //  Serial.print("Got char 1 "); Serial.println(got_char);
  if (got_char == '-' )
  {
    sign = -1;
    get_num_sum = 0;
    //    Serial.print("Sum inside sign check - "); Serial.println(get_num_sum);
  }
  else
  {
    get_num_sum = long(got_char - '0');
  }
  while (Serial.available())
  {
    got_char = Serial.read();
    Serial.print("Got char n "); Serial.println(got_char); Serial.print("sum = "); Serial.println(get_num_sum);
    //    if (isAlphaNumeric(got_char)) sum = (sum * 10) + long(got_char - '0');
    get_num_sum = (get_num_sum * 10) + long(got_char - '0');
  }
  Serial.print("Get_num reads "); Serial.println(get_num_sum * sign);
  return (get_num_sum * sign);
}

char get_cmd()
{
  while (!Serial.available() );
  return (Serial.read());
}
void figures()
{
  char cmd, cmd_2;
  long depth = 81;
  int motor = motorX;
  int nr_steps;
  long step_delay2;

  mylcd.clear();
  mylcd.print("C Circle");
  mylcd.setCursor(0, 1);
  mylcd.print("H Hole");
  mylcd.setCursor(0, 2);
  mylcd.print("S Step motor");
  mylcd.setCursor(0, 3);
  mylcd.print("Z Set axle to zero");
  //             01234567890123456789

  cmd = get_cmd();
  switch (cmd)
  {
    case 'C': Serial.print("Figure circle ended "); Serial.println(millis());
    case 'c':
      {
        mylcd.clear();
        mylcd.print("Circl Depth Rad  Ang");
        global_angle_print_flag = true;
        global_depth_print_flag = true;
        //void makecircle(float (angle_start), float (angle_stop), long(xcenter), long(ycenter), long(radius), float(astep))
        digitalWrite( 8, LOW);//Enable motorpower
        Serial.print("Figure circle started "); Serial.println(millis());
        for (int i = 40; i < depth; i += 40)
        {
          goxyz(motorZ, zpos - 40, zwork_delay);
          mylcd.setCursor(8, 1); mylcd.print("    "); mylcd.setCursor(8, 1); mylcd.print(zpos);
          makecircle(0, 2 * pi, 4000, 4000, 800, 10.0 / 360.0 * pi); //2 degrees stepping angle
        }
        Serial.print("Figure circle ended "); Serial.println(millis());
        global_angle_print_flag = false;
        global_depth_print_flag = false;
        goxyz(motorZ, 0, ztransport_delay);
        goxyz(motorX, 0, xtransport_delay);
        goxyz(motorY, 0, ytransport_delay);
        digitalWrite( 8, HIGH);//Disable motorpower
        break;
      }
    case 'H':
    case 'h':
      {
        mylcd.clear();
        //                   01234567890123456789
        mylcd.print("H Hole De   Rad  Ang");
        global_angle_print_flag = true;
        global_radious_print_flag = true;
        // makehole(float (angle_start), float (angle_stop), long(xcenter), long(ycenter), long(radius), float(astep))
        digitalWrite( 8, LOW);//Enable motorpower
        Serial.print("Figure hole started "); Serial.println(millis());
        for (int i = 40; i < depth; i += 40)
        {
          goxyz(motorZ, zpos - 40, zwork_delay);
          mylcd.setCursor(8, 1); mylcd.print("    "); mylcd.setCursor(8, 1); mylcd.print(zpos);
          makehole(0.0, 2 * pi, 4000, 4000, 1200, 10.0 * pi / 360.0); //1 degree stepping angle
        }

        goxyz(motorX, 0, xtransport_delay);
        goxyz(motorY, 0, ytransport_delay);
        goxyz(motorZ, 0, ztransport_delay);
        global_angle_print_flag = false;
        global_radious_print_flag = false;
        Serial.print("Figure hole ended "); Serial.println(millis());
        digitalWrite( 8, HIGH);//Disable motorpower
        break;
      }
    case 'S'://Step motor
    case 's':
      {
        mylcd.setCursor(0, 2);
        mylcd.print("S Step motor");

        mylcd.clear();
        mylcd.print("Step motor ");
        mylcd.setCursor(0, 1); mylcd.print("Motor. X, Y, Z ");

        //                                 01234567890123456789
//        global_radious_print_flag = true;
        digitalWrite( 8, LOW);//Enable motorpower
        Serial.print("Stepping motor ");
        cmd_2 = get_cmd();

        switch (cmd_2)
        {
          case 'X':
          case 'x':
            {
              motor = motorX;
              step_delay2 = xtransport_delay;
              mylcd.print("X");
              Serial.println("X");

              nr_steps = get_num();
              mylcd.setCursor(0, 2); mylcd.print("Steps "); mylcd.print(nr_steps);
              goxyz(motorX, xpos + nr_steps, xtransport_delay);
              break;
            }
          case 'Y':
          case 'y':
            {
              motor = motorY;
              step_delay2 = ytransport_delay;
              mylcd.print("Y");
              Serial.println("Y");
              nr_steps = get_num();
              mylcd.setCursor(0, 2); mylcd.print("Steps "); mylcd.print(nr_steps);
              goxyz(motorY, ypos + nr_steps, ytransport_delay);
              break;
            }        //                          01234567890123456789

          case 'Z':
          case 'z':
            {
              motor = motorZ;
              step_delay2 = ztransport_delay;
              mylcd.print("Z");
              Serial.println("Z");
              nr_steps = get_num();
              mylcd.setCursor(0, 2); mylcd.print("Steps "); mylcd.print(nr_steps);
              goxyz(motorZ, zpos + nr_steps, ztransport_delay);
              break;
            }
        }//End of case step motor

        mylcd.clear();
        digitalWrite( 8, HIGH);//Disable motorpower
        pos_print(xpos, ypos, xpos);
        mylcd.setCursor(0, 0); mylcd.print("Send a char to skip");
        cmd = get_cmd();//In order yo leave set zero
        //        break;
      }//End of switch step motor
    case 'Z'://Set axle to zero
    case 'z':
      {
        mylcd.clear();
        pos_print(xpos, ypos, xpos);
        cmd_2 = get_cmd();

        switch (cmd_2)
        {
          case 'X':
          case 'x':
            {
              xpos = 0;
              break;
            }
          case 'Y':
          case 'y':
            {
              ypos = 0;
              break;
            }
          case 'Z':
          case 'z':
            {
              zpos = 0;
              break;
            }
        }//End switch cmd set zero
        mylcd.clear();
        pos_print(xpos, ypos, xpos);
      }//End of Zero axle stepping
      mylcd.clear();
      pos_print(xpos, ypos, xpos);
      mylcd.setCursor(0, 0); mylcd.print("Send a char to skip");
      cmd = get_cmd();//In order yo leave set zero
  }//end of switch cmd
  digitalWrite( 8, HIGH);//Disable motorpower
}//End of figures
Title: Re: Trouble using Serial.read()
Post by: PaulS on May 16, 2019, 03:42 pm
Code: [Select]
  char got_char;

  got_char = Serial.read();

    get_num_sum = long(got_char - '0');

The value of got_char - '0' will be in the range 0 to 9. Why are you forcing that to be a long?

Code: [Select]
    case 'C': Serial.print("Figure circle ended "); Serial.println(millis());
Missing a break statement.

Create a minimal test sketch that illustrates the problem, or post ALL of your code. There is no reason to include a sh*tload of code to use the data that you can't receive and parse data correctly.
Title: Re: Trouble using Serial.read()
Post by: Railroader on May 16, 2019, 04:40 pm
Thanks PaulS
How do I do to attach an .INO file at this moment? The sketch is too large to be put into Code tags.

The case 'C' and Serial.print was an accidental Ctrl V at some time and it is corrected now. It doesn't change anything


Making a stepper motor of 400 steps per millimeter move some 200 mm needs 400*200 == 80 000 steps. A little too much for Integers....

The two ways of sending the string are X123 Carriage Return, or X Carriage Return, 123 Carriage return. The first way works, the second onle gives the value 1.
Isn't The Cr dropped in Serial.read, treated as a delimiter?

I'll try to create a test sketch later today. Cutting away code without loosing neccessary lines is never an easy task
Title: Re: Trouble using Serial.read()
Post by: Railroader on May 16, 2019, 05:24 pm
@PaulS
I just got the sketch working. Some break were missing here and there. Improved the debug info sent in Serial.print.
Thanks for Your support!