Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Why it works with an underscore? (http://www.chiefdelphi.com/forums/showthread.php?t=50954)

Mark Pierce 06-01-2007 07:32

Re: Why it works with an underscore?
 
Quote:

Originally Posted by Generalx5 (Post 548652)
Something like this?

#define Setting_1 ((solenoid1 = 1) && (Solenoid2 = 0) && (solenoid3 = 1) && (solenoid4 = 0) && (solenoid5 = 0) && (solenoid6 = 0));

I don't think this is what you want to do. If you want Setting_1 to turn on Solenoid1 and solenoid3, with the others off, you would write the following:

Code:

/* Setting_1 - Turn on solenoids 1&3 only */
#define Setting_1 solenoid1 = 1;Solenoid2 = 0; solenoid3 = 1;solenoid4 = 0; solenoid5 = 0; solenoid6 = 0

This makes it clear you are doing assignments. You could put braces around the assignment statements to make the command look like a single statement to the compiler. This is not needed if you always use braces for the executable statements in if(), while() and for() statements.

Some additional advice for you and others reading this thread:
  • Writing code with comments that explain what is intended is one of the best ways to prevent and debug mistakes. Including these comments of what you intend to do when you ask for advice really helps us help you.
  • Routine names like Setting_1 are great for examples to illustrate a point, but in your robot code, use names like "Extend_L_R_arms_only"
  • In programming there are often dozens of ways to accomplish the task. Often the choice should be made by which will be easiest to understand, debug, and modify as the program develops.

Bongle 06-01-2007 09:00

Re: Why it works with an underscore?
 
I'll also point out that #defines, although pretty useful in some cases, can make code very hard to read, which is important if you ever have someone helping you with it.

For example, your Setting_1 thing affects a LOT of variables, but someone coming along and reading the code would have no idea. They'd see "Setting_1;". It wouldn't look like a function and it wouldn't have any apparent assignments, but because of the #define it actually affects 6 different variables. At the very least, you should have the #define named such that it is apparent it changes something.

One of my coworkers at Sony really likes making complicated #defines. They make code REALLY hard to read until he explains what each one does.

Code:

((solenoid1 = 1) && (Solenoid2 = 0) && (solenoid3 = 1) && (solenoid4 = 0) && (solenoid5 = 0) && (solenoid6 = 0));
Although this actually may* do what you want it to, it does it in a pretty nonsensical way. As posted above, you want each of those assignment statements to have a semicolon after them, rather than a logical and. That way, each of them is a seperate statement that is guaranteed to execute, rather than a wierd condition statement that may stop halfway through (See below).
What follows below is completely tangential and is only for added knowledge
*Depending on the compiler, it may short-circuit the condition statement if one of the arguments in an && block is false. Since ALL conditions must be true for your condition to work out (because they are all anded together), then as soon as it encounters one that returns zero (Solenoid2 = 0) it may exit the condition and return zero.

Basically, when your program is run, it will do this:
Assigns solenoid1 = 1 (result: 1)
Assigns solenoid2 = 0 (result: 0)
**ANDS the first two expressions together (result: 0 && 1 is 0)
Assigns solenoid3 = 1 (result: 1)
ANDS the third expression with result from first 2 (result: 0 && 1 is 0)
Assigns solenoid4 = 0 (result: 0)
ANDS the fourth expression with result from first 3 (result: 0 && 0 is 0)
Assigns solenoid5 = 0 (result: 0)
ANDS the fifth expression with result from first 4 (result: 0 && 0 is 0)
Assigns solenoid6 = 0 (result: 0)
ANDS the sixth expression with result from first 5 (result: 0 && 0 is 0)

At **, the program KNOWS that the final result is going to be zero because one condition in a series of logical ands resulted in zero, so depending on the compiler, it might just save time and skip doing the rest.

Generalx5 06-01-2007 15:24

Re: Why it works with an underscore?
 
It compiles now, but its still not responding to the changes in the sensors. If one of the sensors changed signals and this setting is defined as setting_2 below, it does not change the settings for the solenoids.

This is how im using the codes:


#define Setting_1 solenoid1 = 1;solenoid2 = 0; solenoid3 = 1;solenoid4 = 0; solenoid5 = 0; solenoid6 = 1
#define Setting_2 solenoid1 = 0;solenoid2 = 1; solenoid3 = 0;solenoid4 = 1; solenoid5 = 1; solenoid6 = 0



if ((Senor1 = 1) && (Sensor2 = 1) && (Sensor3 = 1) && (Sensor4 = 1) && (Sensor5 = 1))
{
Setting_1;
}
if ((Senor1 = 1) && (Sensor2 = 1) && (Sensor3 = 1) && (Sensor4 = 1) && (Sensor5 = 0))
{
Setting_2;
}

:confused:

Noah Kleinberg 06-01-2007 15:57

Re: Why it works with an underscore?
 
Quote:

Originally Posted by Generalx5 (Post 549007)
It compiles now, but its still not responding to the changes in the sensors. If one of the sensors changed signals and this setting is defined as setting_2 below, it does not change the settings for the solenoids.

This is how im using the codes:


#define Setting_1 solenoid1 = 1;solenoid2 = 0; solenoid3 = 1;solenoid4 = 0; solenoid5 = 0; solenoid6 = 1
#define Setting_2 solenoid1 = 0;solenoid2 = 1; solenoid3 = 0;solenoid4 = 1; solenoid5 = 1; solenoid6 = 0



if ((Senor1 = 1) && (Sensor2 = 1) && (Sensor3 = 1) && (Sensor4 = 1) && (Sensor5 = 1))
{
Setting_1;
}
if ((Senor1 = 1) && (Sensor2 = 1) && (Sensor3 = 1) && (Sensor4 = 1) && (Sensor5 = 0))
{
Setting_2;
}

:confused:

Your problem here is that you're using "=" instead of "==" inside your if statement. "=" sets a variable's value, while "==" tests for equality.

(x==y) returns a true or false value based on the equality of x and y, but (x=y), like you have here, returns whatever y is equal to, which in this case is 1 (or 0 for one of them) (0 represents false, and anything besides 0 represents true). Therefore you're always entering the first if statement because all of the conditions always return "1", whereas the second one is never reached because "sensor5=0" always returns 0.

Hope this was helpful.

Generalx5 06-01-2007 18:14

Re: Why it works with an underscore?
 
Wonderful! That makes sense, I've been reminded of this before and again have forgotten. Thanks for the help!:D


All times are GMT -5. The time now is 20:47.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi