How do I enter three numbers via serial

Hi all, I want to write a sketch that the user enters three numbers via serial monitor.
the first number dist = 0 - 1500
the second stdelay = 0 - 200
the third dur = 2 - 720

should I use byte?
should this be in setup or loop

I was hoping to echo the numbers back to the monitor

I have tried a number examples that are around but they are all about single entry.
I know this should be simple but after a couple of days I am somewhat confused.

Also will the numbers entered need to be converted from the ascii table or can they be applied to math in the pgm as is

my brain hurts
please help

this is what needs the code

Hi all, I want to write a sketch that the user enters three numbers via serial monitor.

The below may be close to what you need. The individual data is delimited with a , comma, and the data packet is ended with an * (change the * to a . period and the typing in of the data will be more keyboard natural).

//zoomkat 11-12-13 String capture and parsing  
//from serial port input (via serial monitor)
//and print result out serial port
//copy test strings and use ctrl/v to paste in
//serial monitor if desired
// * is used as the data string delimiter
// , is used to delimit individual data 

String readString; //main captured String 
String angle; //data String
String fuel;
String speed1;
String altidude;

int ind1; // , locations
int ind2;
int ind3;
int ind4;
 
void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 90,low,15.6,125*
  //or 130,hi,7.2,389*

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '*') {
      //do stuff
      
      Serial.println();
      Serial.print("captured String is : "); 
      Serial.println(readString); //prints string to serial port out
      
      ind1 = readString.indexOf(',');  //finds location of first ,
      angle = readString.substring(0, ind1);   //captures first data String
      ind2 = readString.indexOf(',', ind1+1 );   //finds location of second ,
      fuel = readString.substring(ind1+1, ind2+1);   //captures second data String
      ind3 = readString.indexOf(',', ind2+1 );
      speed1 = readString.substring(ind2+1, ind3+1);
      ind4 = readString.indexOf(',', ind3+1 );
      altidude = readString.substring(ind3+1); //captures remain part of data after last ,

      Serial.print("angle = ");
      Serial.println(angle); 
      Serial.print("fuel = ");
      Serial.println(fuel);
      Serial.print("speed = ");
      Serial.println(speed1);
      Serial.print("altidude = ");
      Serial.println(altidude);
      Serial.println();
      Serial.println();
      
      readString=""; //clears variable for new input
      angle="";
      fuel="";
      speed1="";
      altidude="";
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

thanks for your replys

I only need to enter the values once so I thought it would go in setup

would the following ever work or is it totally the wrong approach?

int dur; // duration 
int dist; // distance
int stdelay; // start delay in minutes
void setup() {                
// Turn the Serial Protocol ON
  Serial.begin(9600);
  Serial.println ("enter the duration in whole minutes");
   /*  check if data has been sent from the computer: */
  if(Serial.available() > 0) {
     dur = Serial.read();
    /*ECHO the value that was read, back to the serial port. */
    Serial.write(dur);
    Serial.println ("enter the distance in millimetres");

    dist = Serial.read();
    Serial.write(dist);
    Serial.println ("enter the start delay in minutes");

    stdelay = Serial.read();
    Serial.write(stdelay);
  }
}

void loop() {}

thanks

Here is an example of a "blocking" function that gets a line of text from the user:

// get a line from serial
//  ignore spaces, tabs etc.
//  forces to upper case
void getline (char * buf, size_t bufsize)
{
byte i;

  // discard any old junk
  while (Serial.available ())
    Serial.read ();

  for (i = 0; i < bufsize - 1; )
    {
    if (Serial.available ())
      {
      int c = Serial.read ();

      if (c == '\n')  // newline terminates
        break;

      if (!isspace (c))  // ignore spaces, carriage-return etc.
        buf [i++] = toupper (c);
      } // end if available
    }  // end of for
  buf [i] = 0;  // terminator
  Serial.println (buf);  // echo what they typed
  }     // end of getline

Have a look at the examples in Serial Input Basics. The parse example illustrates how to assign the values to variables.

...R

I found this code but it does not wait for the 3rd entry

String name;
int age = 0;
float height = 0;

void setup() {

Serial.begin(9600);

}

void loop() {

Serial.println("What is your name?");
while (Serial.available() == 0) {}
name = Serial.readString();
Serial.println("How old are you?");
while (Serial.available() == 0) { }
age = Serial.parseInt();
Serial.println("How tall are you?");
while (Serial.available() == 1) { }
height = Serial.parseFloat();

Serial.print("Hello ");
Serial.print(name);
Serial.print(" !");
Serial.print("You're ");
Serial.print(age);
Serial.println(" years old ");
Serial.print("and You're ");
Serial.print(height);
Serial.println(" feet tall.");
//There is the point that change all! It clear the
//Serial.available().
while (Serial.available())
{
Serial.read();
}
}

jackrat:
I found this code but it does not wait for the 3rd entry

See Reply #5

...R

thanks Robin I had a read, tomorrow I will try some of the samples. So many many ways to skin a cat!

Robin2:
See Reply #5
...R

And reply #1, where it was suggested too.

OldSteve:
And reply #1, where it was suggested too.

Thank you. I had missed that.

...R

Robin2:
Thank you. I had missed that.
...R

I think he needed re-reminding anyway. :smiley:

there is a lot more to teaching than sending someone to the library. whilst every campus has one, its the faculty that makes it worth attending. Are there any teachers here or just library monitors

jackrat:
there is a lot more to teaching than sending someone to the library.

That's why I wrote the tutorial Serial Input Basics

...R

I appreciate that you have spent a lot of time writing the tutorial it looks very good. I have spent many many hours reading and watching tutorials (element14, jeremy blum, instructables, toptechboy, countless youtubes) and it would seem there are countless approaches. I have 1500mm ballscrew set up as a camera motion control which I can control with a very basic arduino sketch. I would like to manipulate the sketch by entering distance to travel (0 -1500), duration(minutes) and a start delay(minutes) and include the appropriate math. Since this only has to be done once, I gather it can be in the setup and not part of the main loop? If I can keep it there that would be good. When I get that going the plan is use a smart phone to enter the numbers, I have used mit app inventor for that. I have built a trailer tug that is controlled by ArdomotiveBT it was really easy to do and works fine, this project however has me a bit confused, more reading and watching I guess....now I am off to work, have a great day or night!

If you really want user input during setup, here's some blocking code which used Serial.parseInt(). It will wait for user input and the numbers will be converted from ascii characters to integers.

int dur; // duration
int dist; // distance
int stdelay; // start delay in minutes

void setup() {
  // Turn the Serial Protocol ON
  Serial.begin(9600);
  Serial.setTimeout(50);//how long will parseInt() wait 

  Serial.println ("enter the duration in whole minutes");
  /*wait for data sent from the computer: */
  while (Serial.available() <= 0) {}
  dur = Serial.parseInt();
  /*ECHO the value that was read, back to the serial port.*/ 
  Serial.println(dur);
  clearInput(); //clear an NL/CR terminators sent with data

  Serial.println ("enter the distance in millimetres");
  while (Serial.available() <= 0) {}
  dist = Serial.parseInt();
  Serial.println(dist);
  clearInput();

  Serial.println ("enter the start delay in minutes");
  while (Serial.available() <= 0) {}
  stdelay = Serial.parseInt();
  Serial.println(stdelay);
  clearInput();

}

void loop() {}


void clearInput()//discard NL/CR
{
  while (Serial.available())
  {
    Serial.read();//throw away any terminating character
    delay(5);//see if there's another
  }
}

jackrat:
there is a lot more to teaching than sending someone to the library. whilst every campus has one, its the faculty that makes it worth attending. Are there any teachers here or just library monitors

I think this is very rude. This is not a school and we are not paid teachers. If you want teachers, do a course and learn electronics/programming properly. If you want a bit of help, post questions on forums.

here is the unit I need the code for, eventually I hope to add a pan and tilt, its a little rough but I am happy for a first hand build

thankyou cattledog will try your suggestion and sorry oldsteve, didnt mean to offend

jackrat:
thankyou cattledog will try your suggestion and sorry oldsteve, didnt mean to offend

No problem. No offense taken. :slight_smile:

jackrat:
and it would seem there are countless approaches

That is certainly a problem and it is part of what makes it difficult to give you specific advice before you produce some code that represents your best attempt. However poor the attempt may be it allows everyone to focus on a single option.

...R