[SOLVED]Get int variables value through Serial Monitor?

is it possible to do something like in C language "scanf" in arduino language? I need to ask to the user to insert some numbers through the Serial Monitor... i know that there is Serial.read() but i only get -1 and 0
here's an example of what i need:

Insert the actual time: _

it wait for the value and in the code i have -> variable = Serial.read();

SOLUTION by "waterlubber "

Serial.print("Enter the text for the user");
while(!Serial.available()){
  //Do Nothing unless there is a reply!
}
variable=Serial.parseInt();

The Arduino IDE uses C++ so you can use scanf() if you want to.

Something is wrong if you only get -1 and 0 from Serial.read().
Post your code that exhibits this behaviour.

Serial.read() returns -1 if there is nothing available to read, so if you get this it suggests that you aren't using Serial.available() correctly.

If you want to receive a decimal number and know that it's available (for example because Serial.available() shows that there are bytes available to read, and you expect the client to have sent a decimal number) then you can use Serial.parseInt() to read it and return it as a number (rather than a sequence of ascii characters).

so if i do this

Serial.println("Insert the actual time (H): ");
hour=Serial.parseInt();
Serial.println("Insert the actual time (M):");
minutes=Serial.parseInt()
Serial.println("Insert the actual time (S): ");
seconds=Serial.parseInt()

the user can set the time that he want and the variables contain the decimal number that the uset set?

No.
You need to check that there is something to read before you read it otherwise you will get -1 as explained in an earlier post

Sean_97:
so if i do this

Conceptually yes, but after sending the message prompting the user to enter a number, you need to wait until they have actually done that before you try to read it. Otherwise the Arduino will time out after a second, return nothing and carry on to send the second prompt and so on, and go through that whole sequence before the user has actually got around to responding to any of the prompts.

If you don't need your sketch to do anything else while it is waiting, and if you're happy for the sketch to wait as long as necessary for the user to enter something, you could just insert a loop that calls Serial.available() repeatedly until it returns a positive result after each prompt.

The Arduino IDE uses C++ so you can use scanf() if you want to.

sp. "sscanf"

i need to get variables values only one time so i thought to put it on the setup

Serial.print("insert actual time: ");
  Serial.available();
  seth=Serial.parseInt();

i need that seth contains the value that the user put on the serial monitor is it possible? 'cause this doesn't work

'cause this doesn't work

Not "doesn't" but "can't"

Have a look at the reference for Serial.available()

can you please give me a solution? i'm messing around for about a week :~

Serial.print("Enter the hour! OR ELSE!");
while(!Serial.available()){
  //Do Nothing unless there is a reply!
}
seth=Serial.parseInt();

Very simple servo test code where a servo control value is sent from the serial monitor to the arduino, the character string value is captured into a readString String, then the captured characters in the readString String are converted into an integer using the readString.toInt() function.

// zoomkat 10-14-11 serial servo test
// use a microseconds value like 1500 in serial monitor
// 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"); // 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);  // allow buffer to fill with next character
    }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    myservo.writeMicroseconds(readString.toInt()); //convert readString to number for servo
    readString=""; //empty for next input
  } 
}

Thank you man! Now works perfectly :wink:

You should be aware that Zoomkat's code only works with the serial monitor, which sends all characters when you hit "send", and may not work at all with other line speeds.
It won't work with a proper terminal emulator, which sends each character as the key is pressed.

i just needed a sort of menu and @waterlubber gave me the best solution