View Single Post
  #5   Spotlight this post!  
Unread 06-12-2007, 16:30
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
Re: EasyC Vex Programming help in RC Mode

First of all, instead of just setting 60 in the code, wouldn't it be easier to #define it at the start of the code? That way if you want to change it, you can just change it there and don't have to fish for it in the rest of the code.

Code:
#define Percent_Speed 60 //defines the percent of max speed you want to go
The #define sets the value of Percent_Speed (the percentage of max speed you want). Since in this case it is set to 60, Percent_Speed is equal to 60 throughout the code.

Quote:
Originally Posted by artdutra04 View Post
(Don't declare your variable as an unsigned char, use int here!)
I'm assuming you're saying this to prevent overflow when you multiply the value by 6, and also so that you can go into negative numbers when you subtract 127

Instead of declaring an int and wasting memory, why wouldn't you just declare the variable as an unsigned short and typecast the variable during the operation?

Code:
unsigned short ourVariable;
declare the variable as an unsigned short
Assign it to your input(not shown here).

Code:
ourVariable = (((int)ourVariable - 127) * Percent_Speed / 10) + 127; //calculations
motor = ourVariable; //output ourVariable to motor.
If you change motor to whatever the motor you want to output to, the code will take ourVariable, which you have mapped to your input earlier in the code and typecast it into an integer for that operation only (see below). It will then multiply ourVariable by the percentage of your speed and divide by 10 to reduce the power to the percent you set in #define Percent_Speed, add your 127 back to get the value within the accepted range again, and set it to your motor. And yes, I realize I have a pair of unneeded parenthesis in the line, but I like grouping things that way.

Inserting (int) in front of ourVariable makes the program treat ourVariable as an integer for that operation (to give it room to perform the operation), but once the operation is done, ourVariable will return to a nice little unsigned char that doesn't hog memory.
__________________

Last edited by slavik262 : 06-12-2007 at 18:46. Reason: revisions