Serial Communication Question

I was wondering what code I could use to read in and store an input from the Serial monitor? This input would be a simple integer value.

I've fiddled with Serial.read but what I get is that it is stored as a BYTE value?

And with Serial.println, what declaration would I use to print out the same integer value?

Thanks for the help

This input would be a simple integer value.

Where is this data coming from? Is the int being sent as two bytes, or is the value being converted to an array of characters, and sent one character at a time?

If its an array of characters, how will you know when all the characters have been received?

I was hoping to directly input the number in the Serial monitor.

I'm sorry but I just don't really know how it works

The Serial Monitor has no concept of integers. It has a text field, where you type characters. When you press the send key, those characters are sent, one at a time, to the serial port, for the Arduino to read.

It is up to you, then, to supply an end-of packet marker, so you know when to quit reading and storing data, and start interpreting that data.

There are plenty of examples of reading data and storing it in a character array. Once the end-of-packet marker is received, you can convert the array to an integer, using atoi().

Serial.print() (and println()) has several overloads, one which is specifically for printing integers, so you simply need to call Serial.print() with the integer to be printed.

Below is some servo code I use that takes a string number from the serial monitor, converts it to a number for use to control a servo.

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 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(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}