if command question

Hi all;

I am trying to give a value to a variable with an if command. And I want to activate if command with a variable's value. And I am not sure about I am writing the code rightly.

for example;

if (x1v = 0){
  digitalRead(exs);
  if (exs == LOW){
      x1v = 1;
      x2v = 1;
  }
}

if (x1v = 0){
 sensorval =  analogRead(sensor)/20;

  if (sensorval < 25){
      x1v = 1;
      x2v = 2;
  }
}

There are no complete code. I just wanna know is this code group has written right way. Or is there an any example to do this.

I am sorry about my bad english. Thank you so much for your helps and patience.

In an if statement you should always use two equals signs ==

I am trying to give a value to a variable with an if command.

That's certainly a most unusual way to do it. And, usually, as in your code, wrong.

Using an if statement to determine which value to assign is a different story.

I just wanna know is this code group has written right way.

Look at your first three lines. See if you can answer your one question (with Mike's hint).

Grumpy_Mike:
In an if statement you should always use two equals signs ==

So is it the right way?

if (x1v = 0){
  digitalRead(exs);
  if (exs == LOW){
      x1v == 1;
      x2v == 1;
  }
}

if (x1v = 0){
 sensorval =  analogRead(sensor)/20;

  if (sensorval < 25){
      x1v == 1;
      x2v == 2;
  }
}

So is it the right way?

What do you think?

if (x1v = 0){

How many equal signs do you see? Feel free to take your shoes off if you need to.

PaulS good to see you here :slight_smile:

But come on, keep calm don't get angry please...

Should I use double equal everywhere? or some of equals should be one and some equals should double?

Should I use double equal everywhere?

No.

= is the assignment operator.
== is the equality operator.

They are not interchangeable.

PaulS:

Should I use double equal everywhere?

No.

= is the assignment operator.
== is the equality operator.

They are not interchangeable.

So if I want to change the value should I use one equal, right?

for example

if (x1v == 0){
  digitalRead(exs);
  if (exs == LOW){
      x1v = 1;
      x2v = 1;
  }
}

if (x1v == 0){
 sensorval =  analogRead(sensor)/20;

  if (sensorval < 25){
      x1v = 1;
      x2v = 2;
  }
}

May you can let me to see the problem, after you fix this code and post here if there are any wrong thing in this code.

if there are any wrong thing in this code.

Syntactically and logically the code looks OK. Whether it does what you want, or not, only you can tell.

PaulS thats ok. Working perfectly. One more project is done.

So much time happened after my last code, I forget something but it is ok now. Project is done.

Thank you so much for your help :slight_smile:

And thanks all of you for helps.

Grumpy_Mike:
In an if statement you should always use two equals signs ==

well generally perhaps but certainly not always :wink:

yaafm:

Grumpy_Mike:
In an if statement you should always use two equals signs ==

well generally perhaps but certainly not always :wink:

I think for anyone posting on this board always. :slight_smile:

First rule of technicality club:
Don't talk about technicality club!

KeithRB:
First rule of technicality club:
Don't talk about technicality club!
Saturday Morning Breakfast Cereal - 2014-09-02

Why would I imagine you think && vs & (amongst other examples) is a similar "technicality". C is a rich language - enjoying and making good use of that richness is surely not a "technicality" - is it? :wink:

No, but we should simplify things for newbies. Using the result of an assignment in an if statement is a perfectly valid C construct, but can be confusing for the beginner.

digitalRead(exs);

This isn't going to work. If you want to read the state of a digital input pin, you need something more like

exs = digitalRead( pin_number );
if ( exs == 1 )
{   
    // assign something else here
}

Working perfectly. One more project is done.

I very much doubt it.