|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#16
|
||||
|
||||
|
Re: Why it works with an underscore?
Quote:
Code:
/* Setting_1 - Turn on solenoids 1&3 only */ #define Setting_1 solenoid1 = 1;Solenoid2 = 0; solenoid3 = 1;solenoid4 = 0; solenoid5 = 0; solenoid6 = 0 Some additional advice for you and others reading this thread:
|
|
#17
|
||||
|
||||
|
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)); 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. Last edited by Bongle : 06-01-2007 at 09:05. |
|
#18
|
||||
|
||||
|
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; } ![]() |
|
#19
|
||||
|
||||
|
Re: Why it works with an underscore?
Quote:
(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. |
|
#20
|
||||
|
||||
|
Wonderful! That makes sense, I've been reminded of this before and again have forgotten. Thanks for the help!
![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A poximity sensor that works with IFI gear | Validius | Control System | 10 | 16-03-2005 21:56 |
| A poximity sensor that works with IFI gear | Validius | Electrical | 10 | 16-03-2005 21:56 |
| Why is Dave hanging out with these guys? | JohnBoucher | NASA Discussion | 1 | 17-12-2004 10:38 |
| RoboEmu 1.06 (works with XP and 2k) | rbayer | Programming | 4 | 20-10-2002 21:47 |
| Are you a team with very little money, why/ | mnkysp6353 | General Forum | 6 | 09-10-2001 21:30 |