A simple question about entering data via the Serial connection

Greetings,

I've just started learning about the Arduino and I would like to send data from my PC to my Arduino Uno. I would like to write a program that uses the Serial Monitor to prompt the user for information, allows the user to enter some numbers, then stores those numbers in variables to be used later in the program. For example, I would like the screen to prompt "ENTER DISTANCE", after which the user might type "12345" and then hit the ENTER key. An integer variable inside the program would then have the value of 12345.

This seems like it should be simple to do, but it appears to me that the Serial Library works only with ascii data and does not have a way of converting the ascii data to decimal data. It seems to me that using Serial.read() ends up providing me with ascii codes for each individual digit (1, 2, 3, 4, 5) and not for the actual numerical value of 12345. Am I doing something wrong with this? Is there some easy way to transform the entered data back to decimal or am I just being stupid?

Thanks.

Am I doing something wrong with this?

Yes, but since you chose not to post any code, what you've done wrong is a mystery and I can only offer general advice.

Store the input into an array of chars. Then null-terminate it and pass it to atoi().

Arrch:

Am I doing something wrong with this?

Yes, but since you chose not to post any code, what you've done wrong is a mystery and I can only offer general advice.

Store the input into an array of chars. Then null-terminate it and pass it to atoi().

Arrch,

thanks very much for your super-rapid response. Sorry, I did not post any code because I didn't have any code that even came close to working for this. But your general advice has at least opened my eyes to the fact that this sort of data entry operation is not a trivial task. I don't know C, so I had never even heard of atoi as it is not in the Arduino reference. Having googled atoi, I can now see that there are many posted suggestions for doing this sort of data entry. I don't suppose it's included in some sort of library somewhere, is it?

Thanks again. :)

NoEinstein: Sorry, I did not post any code because I didn't have any code that even came close to working for this.

That shouldn't stop you. Posting code gives us an idea of your thought process on how you are approaching this problem. This allows us to correct misconceptions that help you develop the code.

I don't suppose it's included in some sort of library somewhere, is it?

It's included within stdlib, which is included in every sketch automatically by the IDE.

This the example I came up with to explain how to send and receive multidigit integers over serial.

I tried to comment it, so you can understand what is going on.

http://www.cmiyc.com/blog/2012/07/30/arduino-multi-digit-integers/

Awesome! Thank you so much! I’m new to this, so I will study it tonight.

Servo test code that captures and converts an ascii string into a decimal number.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

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

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // 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

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

zoomkat: Servo test code that captures and converts an ascii string into a decimal number. ...

zoomkat, looks interesting. But I'm curious what String readString; might be. Is that a function of some sort called out of a library somewhere, or some kind of C code, or...???

[EDIT] Oops, I guess it's just a String declaration. Sorry, but I didn't see it in the reference until now.

Thanks.

Be careful using Strings as it totally agitates some of the natives. ;)