|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
I have a code written up for my own custom encoder involving optical sensors, yes its a optical encoder, using quadrature.
My question is why the compiler will find a syntax error if I didn't use an underscore in my variables. For instance, I have this before: ------------------------------------------------------------- /*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/ unsigned char Sen1; unsigned char Sen2; unsigned char Sen3; unsigned char Sen4; unsigned char Sen5; ------------------------------------------------------------- along with ------------------------------------------------------------- void Process_Data_From_Local_IO(void) { Sen1 = rc_dig_in05; Sen2 = rc_dig_in06; Sen3 = rc_dig_in07; Sen4 = rc_dig_in08; Sen5 = rc_dig_in09; after I changed it to: ------------------------------------------------------------- /*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/ unsigned char Sen_1; unsigned char Sen_2; unsigned char Sen_3; unsigned char Sen_4; unsigned char Sen_5; ------------------------------------------------------------- void Process_Data_From_Local_IO(void) { Sen_1 = rc_dig_in05; Sen_2 = rc_dig_in06; Sen_3 = rc_dig_in07; Sen_4 = rc_dig_in08; Sen_5 = rc_dig_in09; ------------------------------------------------------------- Im new at programming so im open to harassment. ************************************************** *************** Another question is how do you set a value under certan conditions in programming? For instance, I have 5 sensors like the above. When all 5 sensors reads a certain encoding for example all 0's how do I set this reading to a specific digit, so I can later call up on again. This makes it easier for changing later due to so many sensor reading combinations. The idea im getting at is something like: if (rc_dig_in01 = 0,rc_dig_in02 = 0,rc_dig_in03 = 0,rc_dig_in04 = 0,rc_dig_in05 = 0) == 10 if (rc_dig_in01 = 0,rc_dig_in02 = 0,rc_dig_in03 = 0,rc_dig_in14 = 0,rc_dig_in15 = 1) == 20 and after all these are set, I can have another routine that can use those numbers: if (20) turn on solenoid 3 etc etc. Last edited by Generalx5 : 05-01-2007 at 01:37. Reason: Added the second part under the * line |
|
#2
|
|||||
|
|||||
|
Re: Why it works with an underscore?
Welcome to the quirkiness of the MCC18 compiler!
The compiler is known to choke for no apparent reason. Personally, I think it's a catch-all. Something goes wrong and it tosses a syntax error. |
|
#3
|
||||
|
||||
|
Re: Why it works with an underscore?
#define is your best friend in MPLAB . . or at least your cool kinda crazy uncle
as in #define optical1 rc_dig_in05 as for your if statements commas are wrong you want ANDs(&&) so its if(blah == 0 && other == 0 && batman == 0) ohh also remember that = is assignment and == is compare Last edited by Stuart : 05-01-2007 at 02:50. |
|
#4
|
||||
|
||||
|
Re: Why it works with an underscore?
Remember this or you will have an infinite number of problems everytime you load code to your robot.
Nothing hurts more than to discover that a single = in an if statement actual assigns that value to that variable, rather than comparing it. I don't know why the underscore thing would be an issue; I know MPLAB tends to blow up on variables with spaces, but single words have always worked fine for me. It might have something to do with the fact you're using numbers in the name; you might need so many letters in your variable before you can use a number. |
|
#5
|
||||
|
||||
|
Re: Why it works with an underscore?
Quote:
Quote:
if ((rc_dig_in01 == 0) && (rc_dig_in02 == 0) && (rc_dig_in03 == 0) && (rc_dig_in04 == 0) && (rc_dig_in05 == 0)) { outputvariable = 10; } This follows the C syntax rule: if (expression) statement; (SINGLE statement) if (expression) { statement; statement; ... } (BLOCK of statements) There are other ways of writing this as well, but unless you are very experienced with C and the rules of operator precedence, liberal use of round brackets around each sub-expression will guarantee the multiple statements are evaluated in the manner and the order you expect. As Stuart said, it is a very common error to use if (rc_dig_01 = 0) rather than (rc_dig_01 == 0). The first form ASSIGNS the value 0 to rc_dig_01, the second COMPARES rc_dig_01 with 0. |
|
#6
|
||||
|
||||
|
Ok, I see where my mistakes are, thanks a lot for all the help =D. I'll go edit my code right now.
|
|
#7
|
||||
|
||||
|
Do I #define a name that I want to use and then in the routines use them there?
Can I: #define *Sensor_variable read* results in turning on solenoid 1 and 4 and 5 then later if this happens: if ((Sen_1 == 1) && (Sen_2 == 1) && (Sen_3 == 1) && (Sen_4 == 1) && (Sen_5 == 1)) { Sensor_variable read; } This way I can activate certain solenoids when my 5 sensors reads all 1's its soooo confusing...still trying to make the encoder work. |
|
#8
|
||||
|
||||
|
Re: Why it works with an underscore?
#defines are very simple.
At their simplest (you can do a lot of wierd stuff with them, but that's for another day), you have: #define <text to replace> <text to replace with> So if you did #define *Sensor_variable read* Then anytime you typed "*Sensor_variable", it would replace it with "read*". Unless you didn't mean the asterisks to be there, then it would do the same thing without the asterisks. This means you can do things like #define test pwm01; The preprocessor (thing that processes everything that starts with #) doesn't care WHAT you put after the first argument to a define, it will go to the end of the line and replace all instances of "test" with "pwm01;". Note that the semicolon is in there. That means that if you wrote test = 5; Then after the preprocessor was done, it'd look like pwm01; = 5; // compile error woo-hoo because it replaced "test" with "pwm01;". So be careful of what you put as the second argument to #define, or else it might not make sense. For a good example of #defines, take a look at the given code in ifi_aliases.h. The entire file is IFI just re-defining big ugly expressions as easy-to-use things like pwm01, pwm02, rc_dig_in01, etc. You just need to do the same thing. Last edited by Bongle : 05-01-2007 at 20:23. |
|
#9
|
||||
|
||||
|
Is it possible to define something and have a set of commands built into it?
Something like this? #define Setting_1 ((solenoid1 = 1) && (Solenoid2 = 0) && (solenoid3 = 1) && (solenoid4 = 0) && (solenoid5 = 0) && (solenoid6 = 0)); will this allow me to do.... if (Sensors_are_online); { Setting_1 } basically if the sensor receives a signal, it would turn on setting_1 which is to turn solenoid 1 and solenoid 3 on. |
|
#10
|
||||
|
||||
|
Re: Why it works with an underscore?
Quote:
By the way, you don't need a semicolon after the if (Sensors_are_online) statement. In general, any sort of construct that uses curly braces does not have a semicolon for itself. Rather, the individual statements within the braces have semicolons at the end of them. |
|
#11
|
||||
|
||||
|
Re: Why it works with an underscore?
NVM that last one I got it figured out, it was just a simple ";" mistake, I added an extra ; at the end of the text to replace.
Alright heres the new problem... I have a bunch of if statements and they rely on a bunch of sensors, when these sensors read a certain set of digital values it would turn on certain solenoids, correct, but! if these digital values from the sensors changed (knowing this from printf), the solenoids stay the same. They do not change with the input so I'm guessing that I'm doing something wrong with the if statements. I have a lot of if's and in the very bottom on all the if statements, there would be an else if statement to eliminate any other undefined sensor readings. any suggestions to what I'm doing wrong? ************************************************** ************ Ah! yes, I found the mistakes there, I've been reading the powerpoint tutorials from IFI and it got me confused. I start putting ; and brackets all over the place....I'll remember it for next time. Last edited by Generalx5 : 06-01-2007 at 00:48. Reason: Added part under the * line. |
|
#12
|
||||
|
||||
|
Re: Why it works with an underscore?
You can also use #define in your header files
#ifndef header_h__ #define header_h_ stuff stuff stuff #endif Then if you put #include "header.h" more then once it will say hey! header_h__ is already defined stupid! you can't define it again!! and just exit........ ![]() |
|
#13
|
||||
|
||||
|
Re: Why it works with an underscore?
Quote:
You could use a switch loop. **Somebody please correct me if I am wrong, I don't remember is switch is only C++ or c too. |
|
#14
|
|||||
|
|||||
|
Re: Why it works with an underscore?
Quote:
But don't do it with a #define. What you're probably looking for is a function that will do what you want. Code:
void Setting_1(void)
{
solenoid1 = 1;
solenoid2 = 0;
solenoid3 = 1;
solenoid4 = 0;
solenoid5 = 0;
solenoid6 = 0;
}
if (Sensors_are_online)
{
Setting_1();
}
|
|
#15
|
||||
|
||||
|
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:
|
![]() |
| 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 |