problem with serial communication

Hi …

I want to make a program :

  • If I type a number on keyboard in serial window and this number is equals to a pot value … serial print “YES” and else types “NO”

my code :

int pot = A0;
int value;

void setup() {
  // put your setup code here, to run once:
pinMode(pot,INPUT);
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
value = analogRead(pot);
Serial.println(value);
if (Serial.available()){
  int data = Serial.read();
  if(data = value)
  {Serial.println("YES");}else{Serial.println("NO");}
}
delay(1000);
}

Problem … it types yes if I type any number or even a character !!!

Thanks ,

    if (data = value)

How do you test for equality in C ?

I don't get it bro , I don't understand u ..

= for assignment
== to test equality

There are other problems but fix this one first

Thanks for ur help ..

but now it types "NO" only even if there is equality ... :confused: :confused: :frowning:

Please show the current code.

Remember to use the autoformat function (CTRL-T) of the Arduino IDE first.

By the way, I don't think that Serial.read() does what you expect it to do in your code.

Good Luck!

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

malaa_borg:
Thanks for ur help ..

but now it types "NO" only even if there is equality ... :confused: :confused: :frowning:

I did warn you that there were other problems !

Have you tried printing the value and data variables to see exactly what is being compared ? You may be surprised at what is being received from the Serial input.

pinMode(pot,INPUT);

This probably does NOT do what you think it does, for an pin that you are using as an analog pin. Analog pins are input-only, so there is no need to tell an analog pin that is is an input pin.

Doing this affects the nature of the digital pin that shares the same space. There are side affects for analog pins. In general, if you don't know what the side affects are, and need those side affects, do NOT call pinMode() on a pin that you are using as an analog pin.

Thanx guys for helping ....

the best method :

Using Serial.parseInt() instead of Serial.read() ...

It works well

Serial.parseInt() is blocking for a time, Serial.read() is not. For many people, blocking is bad. However, if Serial.parseInt() works well for you, then great!