Jump to content

Hi,

 

In Arduino how do you get an 'if' statement to recheck the original value between each of the following 'else' statements?

 

So, for example,

 

if (distance =10) {

 

do task 'a';

 

}

 

else{

 

complete task 'b';

complete task 'c';

complete task 'd';

 

}

 

How would I get it to recheck the distance between each of the tasks in the 'else' section?

 

Thanks

Link to comment
https://linustechtips.com/topic/903463-arduino/
Share on other sites

Link to post
Share on other sites

13 minutes ago, MrDrWho13 said:

You would need an if statement every time you want to check it.

 

Also remember to put "==" not "=" since they mean different things.

Putting all the ifs is a lot of repeated code and probably deserves some more design.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/903463-arduino/#findComment-11115712
Share on other sites

Link to post
Share on other sites

Just now, vorticalbox said:

Putting all the ifs is a lot of repeated code and probably deserves some more design.

That's true

@JWKelly99 Why do you want to check at each point? Are you putting delays in your code? Can't you just run them all and check next time?

 

I edit my posts a lot.

Link to comment
https://linustechtips.com/topic/903463-arduino/#findComment-11115717
Share on other sites

Link to post
Share on other sites

13 hours ago, JWKelly99 said:

Hi,

 

In Arduino how do you get an 'if' statement to recheck the original value between each of the following 'else' statements?

 

So, for example,

 

if (distance =10) {

 

do task 'a';

 

}

 

else{

 

complete task 'b';

complete task 'c';

complete task 'd';

 

}

 

How would I get it to recheck the distance between each of the tasks in the 'else' section?

 

Thanks

The most obvious way would be with a switch statement:

switch (distance)
{
  case 10:  /* Handle distance == 10 */ break;
  case 20:  /* Handle distance == 20 */ break;
  //Etc...
  default:  /* Handle anything else */  break; 
}

 

Link to comment
https://linustechtips.com/topic/903463-arduino/#findComment-11117805
Share on other sites

Link to post
Share on other sites

19 hours ago, JWKelly99 said:

Want it to recheck the distance between each of the tasks in the 'else'

I'm struggling to understand what you want to do.

Do you want to execute one, and only one of your statements?
Do you want to execute possibly many of your statements?
Do you want to execute either the if, or the else, and just need to update the value of distance between the else statements?

For the first case, I'm not sure a switch statement works. Case (switch) statements only work if your variable should only be one of a few values. In the first case you should do:

if (condition)
{
	//Do stuff
}

else if (other condition)
{
  // Do stuff
}

else if (yet another condition)
{
  // Do stuff
}

 

For the second statement:

if (condition)
{
  // Do stuff
}

if (other condition)
{
  // Do stuff
}

if (yet another condition)
{
  // Do stuff
}

 

For the third statement (I think this is what you want??):

if (condition)
{
  // Do stuff
}

else
{
  // Do stuff
  update variable;
  // do stuff
  update variable;
  // do stuff
}


That would be the simplest thing to do with the least code delta. The best thing to do would be to make the variable a parameter of whatever method you are calling, and then keep the variable local to your Loop() method. Then you have:

void Loop()
{
  if (condition)
  {
    Something(variable);
  }
  
  else
  {
    Something(update variable);
    Something(update variable);
    Something(update variable);
  }
}

void Something(variable)
{
  // do stuff here.
}


Of course, I can't see why you want to do this. All of this is logically invalid. If the value of variable needs to change before you act on it, then you have to rerun the entire decision block (because the value of the variable can now be whatever satisfies the if condition). This smells of bad design.

So my question is, what specifically and exactly are you trying to do?

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/903463-arduino/#findComment-11118515
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×