Storing a multidigit number from the serial monitor

I’m trying to get a program to work which will read a multidigit number from the serial monitor (such as 2365 or 58) and will compare it to an array of accepted numbers to decide whether or not to turn a servo. Problem is, since the arduino is reading one byte at a time, I’m having trouble using the values. I’ll post my code below, but first I’ll explain the problem. So, in my code I have a line which prints out the number that it’s about to add to the total value so that I can see if it’s right or not. It gets the first number right, but for the 2nd digit and beyond I get lines and lines of gibberish. Thank you for any help you can give.

#include <Servo.h>
Servo lock;
static int personnel[4] = {42, 9001, 666, 200};


void setup(){
  Serial.begin(9600);
  Serial.println("Hi");
  lock.attach(9);
}


void loop()
  {
  lock.write(1);
  int vlu = 0;
  int dgt = 0;
  while (!(Serial.available())); //wait till a byte is in register
  delay(20); //wait till all are recieved
  for (int cnt = Serial.available(); Serial.available() > 1; cnt--){
  {
    int dgt = Serial.read()-'0';
    Serial.print (dgt);
    for (int dec = 1; dec < cnt; dec++)
    {
      dgt *= 10;
    }
   vlu += dgt;
  } 
  Serial.print ("\n");
  Serial.print(vlu + "\n");
  for (int i = 0; i < 3; i++){
    if (vlu == personnel[i]){
      lock.write(179);
      delay(1000); 
    }
  }
  lock.write(0);
  }
  while (Serial.available()){
    Serial.read();
  }
}
  for (int cnt = Serial.available(); Serial.available() > 1; cnt--){
  {
    int dgt = Serial.read()-'0';
    Serial.print (dgt);
    for (int dec = 1; dec < cnt; dec++)
    {
      dgt *= 10;
    }
   vlu += dgt;
  }

Nonsense. Serial.available() changes as you read values from the serial port and as values arrive. If you want to read all that are available at some point in time, the time to stop is when cnt is 0, NOT when Serial.available() is 0.

Imagine that you enter “1234”. Imagine that sooner or later, the data arrives at the Arduino. How will you know when all the data has arrived?

Now, imagine that you enter “1234!”. Imagine that sooner or later, the data arrives at the Arduino. Now, can you know when all the data has arrived? Of course you can, because the end of packet marker (’!’) has arrived. There is nothing magical, of course, about ‘!’ as the end of packet marker, and, in fact, the serial monitor can add or for you, so ‘\n’ () might make a better choice.

Store the data in an array, NULL terminated, and use atoi() to convert to a value. Or, use parseInt() if you are lazy.

This problem, or one very like it, comes up every day.

It would be very useful if there was a demo sketch that people could be referred to.

I am quite prepared to write one, but different experts seem to use different approaches and I don’t want to waste time (mine and other people’s) on a piece of code that is just going to draw arguments in favour of each preferred approach.

My own inclination is to save the incoming data into an array (bytes or chars) so that the receiving code could include checksums or other reliability tests. Then the data in the array can be parsed to extract the data values.

…R

I am quite prepared to write one, but different experts seem to use different approaches and I don't want to waste time (mine and other people's) on a piece of code that is just going to draw arguments in favour of each preferred approach.

Start on the Website and Forum part of the forum, suggesting some approach. Let people comment on that approach. When you get consensus on one (or more) approaches, publish on the playground.

Very simple servo test code that captures a string of characters sent from the serial monitor and converts them into a number for servo control.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    myservo.write(n);
    readString="";
  } 
}