reading the serial port

hello! i am kind of new so bear with me.

I am doing a project and i need to have it so i can insert 3-digit numbers into the serial port. I need this answer ASAP. thank you

P.S. give me a simple code

A demand for code with a vague description isn't going to get an answer (beyond this one).

well you don't have to be mean.......

i am trying to put 3 digit numbers (i.e. 102) into the serial port/serial monitor.
i need it to say things like "hi!" every time you enter a number like 304.

please (again) answer ASAP
(i hope this clear up any confusion')

tyguy2:
well you don't have to be mean.......

Trying to deduce my state of mind from that single sentence is impossible. Please stop. I promise you will always fail.

(i hope this clear up any confusion')

Not even close.

i am trying to put 3 digit numbers (i.e. 102) into the serial port/serial monitor.

"Put"? As in, write to the serial port from my Arduino? As in, enter from the keyboard using my hands?

i need it to say things like "hi!" every time you enter a number like 304.

"It"? As in, my Arduino? As in, my baby brother?

1.) again, this is a public place, don't act mean to people. it isn't nice, it is condescending.

2.)I am trying to type in 3-digit numbers into the serial monitor. When I do this, I want my arduino to
say a phrase (example: if I enter the number 102 into the serial monitor, i want the arduino to output ("hi!")

3.)I need an example code

4.)please reply quickly

5.)I hope THIS post helps clear up any confusion.

tyguy2:
1.) again, this is a public place, don't act mean to people. it isn't nice, it is condescending.

If you feel I'm being mean to you then there is nothing left except for me to bow out. I wish you the best of luck with your homework assignment.

this is not a, quote on quote, "homework assignment". This is a person looking for help, not hurtful comments

What have you tried?

Start here

Specifically section 6 on how to properly post code. It's also considered polite to properly format the code before you post. This can be done in the Arduino IDE with Tools > Auto Format.

sorry about that. I am new to this. Anyway.....

void setup() {                
  
  Serial.begin(9600);
  
  
}


void loop() {
  
  while (Serial.available() == 0);
 
  char val =  Serial.read() - '0';
  
  
if (val == 1 && val == 0 && val == 2) Serial.println ("hi");
}

every time i am at the serial monitor nothing happens
please help

tyguy2:
if (val == 1 && val == 0 && val == 2) Serial.println ("hi");

val can't be 1, 0 and 2 at the same time. It can only have one value. When you send the Arduino "102" through the serial monitor, it receives it as three separate characters: '1', '0', and '2'. In addition, your loop() function will likely run thousands of time in between receiving the characters, so you can't just assume that the instant you hit send, all three bytes will be available. What you need is some sort of character that signifies the end of the packet. The Arduino serial monitor can append a '\n' or '\r', which could be used in this circumstance. You also need a state machine to keep track of the characters you receive and a buffer to store them in.

I posted an example of receiving a number through the Serial monitor in another thread probably less than an hour ago; it shouldn't be hard for you to find.

Can i please have a short example code?

looking for Serial.available() == 0 is not a good idea.

You want to do something when a serial input character IS available.

tyguy2:
Can i please have a short example code?

You really can't be bothered to look at my post history or scan the first page for a similar thread that I posted in?

This is a technical forum, so questions need to be in a technical format. That being said, below is some very simple code that might get you started.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;


void setup() {
	Serial.begin(9600);
        pinMode(13, OUTPUT);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(2);  
    	char c = Serial.read();
        readString += c; 
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
     
    if (readString == "304") {
	digitalWrite(13, HIGH);
        Serial.println("Led On");
    }
   if (readString == "303") {
	digitalWrite(13, LOW);
        Serial.println("Led Off");
    }
        readString="";
   } 
}

looking for Serial.available() == 0 is not a good idea.

@michinyon: Read the code again.

  while (Serial.available() == 0);

OP, this question comes up here in different forms probably a couple of times a month.

Do some of your own legwork.

[rant]

Mortified!

This is indicative of the selfish, instant gratification, vague, rude, posts that seem to be forever increasing across the net.
Well done you guys that spoke out!

We all love to help, but, I at least, like to feel that the person receiving help will somehow meet me half way, do some research, and improve their lot and maybe pass on some of that new knowledge. Not, as I feel will happen here, take what they can, use it until bored and then demand more ways to fulfil their ill conceived plans.

Many people on this forum are keen to give up their time and freely spout code until the cows come home, personally I like to point in the right direction, try to get them to understand what it is that they are doing and why. That way they get more fulfilment.

I suppose that the Arduino is it's own worst enemy, it appears to de-skill to such a degree that looks like putting Lego together - which is cool - but belays the need for at least a meagre understanding.

But what really annoys me is the rudeness of these people. This guy should be ashamed of himself - he is basically mugging people and spitting in their face while he does it!

Everyone should take a stand against this kind of behaviour.

[/rant]

, like to feel that the person receiving help will somehow meet me half way

Amen.

We’re not supposed to do people’s homework either.

I don’t mind helping someone learn as long as they do learn. Otherwise it’s unpaid work since yes I consider a person who has really learned makes my world a bit better.

Tyguy: serial data you get from the serial monitor is ASCII Encoded Text. The character ‘0’ has a value of 48 but you can use ‘0’ in code.

You can make this into a new sketch and change serInput as you need until it makes sense. Leave loop empty.

void setup( void )
{
  Serial.begin( 9600 );

  char serInput = '5';
  byte value;

  if (( serInput >= '0' ) && ( serInput <= '9' ))
  { 
    value = serInput - '0';
    Serial.println( "Value is " );
    Serial.println( value, DEC );
  }
  else
  {
    Serial.println( "Input is not a digit." );
  }
}

And for your own sake, learn about C string arrays and avoid depending on C++ Strings when coding on limited RAM machines like the UNO. String variables are wasteful and unnecessarily hide what they do and how they do it. The ‘advantages’ they give in some respects are matched by the disadvantages that come with learning to depend on every black box.