What is the equivalent of "cin >>" in arduino Serial Monitor

Hello...

i would like to make a program in Arduino IDE that would wait for answer from the Serial Monitor input, just like the "cin >>" in C++, i have used "serial.available" but stil confused, this is my code :

Serial.println("Welcome to Simple Arduino Based Calculator Program\n");
Serial.println("Now you can perform simple calculations");
Serial.println("Some calculations that can be performed are :\n");
Serial.println("1. Operation Multiplication");
Serial.println("2. Division Operation");
Serial.println("3. Addition Operation");
Serial.println("4. Subtract Operation\n");
Serial.println("Please select the type of calculation you want(1-4)\n");

then below that i would want the program to wait answer from the user through Serial Monitor, then store the answer in a variable...how do i do that?

Thank you :smiley:

I guess that would be Serial.read.

In particular check out:

Hi,
This is not your code, it's a piece of your code, which doesn't help us at all to help you.
In the arduino IDE you use serial.available() to check if there is data in the serial, but you need to use specific functions to read this data, such as Serial.read(), parseInt(), readUntil()
and there is another
See the arduino documentation online for a lot of information.
Read the Functions topic.

This is slightly off-topic, but before you go much further, you may want to investigate the F() macro

https://www.arduino.cc/reference/en/language/functions/communication/serial/parseint/

Old, old right-triangle sketch:

/* Right Triangle - User Interactive via Serial Terminal
   M. Ray Burnette 20130125
   Arduino Nano 328P target processor
   Binary sketch size: 5,202 bytes (of a 30,720 byte maximum)
*/

#include "math.h"               // include the Math Library

#define BAUD 9600
#define timeoutPeriod 2147483647    // Long var... about 25 days

float a;
float b;
float h;
char junk = ' ';

void setup()                    // run once, when the sketch starts
{
  Serial.begin(BAUD);           // set up Serial library at 9600 bps
  Serial.setTimeout(timeoutPeriod); // default is 1 second
  while (!Serial) {
    ; // wait for serial port to connect
  }
}

void loop()
{
  
  Serial.println("Let's calculate a hypotenuse, h");
  Serial.println("               /|");
  Serial.println("             /  |");
  Serial.println("           /    |");
  Serial.println("      h  /      |");
  Serial.println("       /       b|");
  Serial.println("     /          |");
  Serial.println("   /            |");
  Serial.println(" /________a_____|");
  Serial.println("");
  Serial.println("");
  
  Serial.println("Enter value for leg 'a', Press ENTER");
  while (Serial.available() == 0) ;  // Wait here until input buffer has a character
  {
      //Side 1
    a = Serial.parseFloat();        // new command in 1.0 forward
    Serial.print("a = "); Serial.println(a, DEC);

    while (Serial.available() > 0)  // .parseFloat() can leave non-numeric characters
    { junk = Serial.read() ; }      // clear the keyboard buffer
  }

  Serial.println("Enter value for leg 'b', Press ENTER");
  while (Serial.available() == 0) ;
  {
      //Side 2
    b = Serial.parseFloat();
    Serial.print("b = "); Serial.println(b, DEC);
    while (Serial.available() > 0)
    { junk = Serial.read() ; }

    h = sqrt ( a*a + b*b );

    Serial.print("hypotenuse = ");
    Serial.println(h, DEC); Serial.println();
  }
}

More:
How to Read User Input from the Arduino Serial Monitor - Circuit Basics

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.