Go Down

Topic: How can I simplify this simple code? (Read 1 time) previous topic - next topic

liudr


ZacharyBruno


Assuming current(dir) returns the numerical angle, you can do this:

absRollAngle=(current(dir)+360)%360;

http://arduino.cc/en/Reference/Modulo


Since I first started developing this program, I've learned about Strings and how bad they are (Although they make your code so readable).

I really like this method... when I try compiling, though, it says:  invalid operands of types 'float' and 'int' to binary 'operator%'
RadarProject - Arduino Autopilot for RC http://radarproject.wordpress.com/

liudr



Assuming current(dir) returns the numerical angle, you can do this:

absRollAngle=(current(dir)+360)%360;

http://arduino.cc/en/Reference/Modulo


Since I first started developing this program, I've learned about Strings and how bad they are (Although they make your code so readable).

I really like this method... when I try compiling, though, it says:  invalid operands of types 'float' and 'int' to binary 'operator%'


Since you didn't say what current(dir) does, I assumed it returns integer.


absRollAngle=(int(current(dir))+360)%360;

AWOL

Quote
I think the modulo is easy enough.

Easy, yes, but insufficient - if the initial value is more negative than -360, it won't work.

e.g. (-540 + 360) % 360 = -180.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ZacharyBruno

AWOL, current(dir) will never be more than 180 or -180.

Ok... too bad modulo doesn't work with float. What about this piece of code? I have a tolerance level. Basically, I want it to return true if the amount of roll is out of the desired roll with a specific tolerance. Is there a better way to write this?

Code: [Select]

boolean Plane::planeDisplaced(String dir, float input) {
/////// Checking To See if Plane is Within Bounderies ///////
     if (current(dir) < (input - tolerance) || current(dir) > (input + tolerance)) {
       return true;
     }
     else {
       return false;
     }
}


Thanks in advance,
Zachary
RadarProject - Arduino Autopilot for RC http://radarproject.wordpress.com/

Go Up