# If else statement, could it be done simpler

I have a piece of if else statements to determinate the PWM speed to be used.
The code is working but i was wondering if it could be done simpler and thus saving memory.

``````    // Calculate speed used depending on distance to move
if(abs(rotorMoveAz) >= SPEED_DEG_1) {
_rotationSpeed = confValue._rotationSpeed4;
}
else if(abs(rotorMoveAz) >= SPEED_DEG_2 && abs(rotorMoveAz) < SPEED_DEG_1) {
_rotationSpeed = confValue._rotationSpeed3;
}
else if(abs(rotorMoveAz) >= SPEED_DEG_3 && abs(rotorMoveAz) < SPEED_DEG_2) {
_rotationSpeed = confValue._rotationSpeed2;
}
else {
_rotationSpeed = confValue._rotationSpeed1;
}
``````

Yes. You can optimize the code a bit. It will save you a few bytes of program space. I don’ think it will be a big difference, though.

I’m assuming (from the code) that SPEED_DEG_1 > SPEED_DEG_2 > SPEED_DEG_3. If this is not true then the code below won’t work.
[ It would be nice if you included those values in the code: if they are related you could use a formula
and not need an if statement at all.]

So two things you can do are to eliminate most of the abs() calls, and shorten the if statements. Since the value of rotorMoveAz doesn’t change, you only have to call abs() at the beginning.
Also, if SPEED_DEG_1 > SPEED_DEG_2 then you don’ t need the parts of the if statements after the && - if you reach that statement you already know that a must be < SPEED_DEG_1.

``````    a = abs(rotorMoveAz);
// Calculate speed used depending on distance to move
if(a >= SPEED_DEG_1) {
_rotationSpeed = confValue._rotationSpeed4;
}
else if(a >= SPEED_DEG_2) {
_rotationSpeed = confValue._rotationSpeed3;
}
else if(a >= SPEED_DEG_3) {
_rotationSpeed = confValue._rotationSpeed2;
}
else {
_rotationSpeed = confValue._rotationSpeed1;
}
``````

Don’t forget to declare a of the appropriate type at the beginning of the function.

Can you post the values of the constants? both the ... SPEED_DEG_3 and the _rotationSpeed4...

maybe a formula is possible like

_rotationSpeed = c1 + abs(rotorMoveAz) * c2 ; // where c1 and c2 are to determined constants

robtillaart:
Can you post the values of the constants?
both the … SPEED_DEG_3 and the _rotationSpeed4…

``````#define SUPPLY_VOLTAGE 800      // This is the system supply voltage between 12-24V DC in mV
#define MOTOR_VOLTAGE 600       // Motor normal operating voltage in mV
#define MOTOR_MIN_VOLTAGE 258   // Minimum desriable motor voltage in mV
// Any change in the above values require a version change
// We now can calculate the voltages used to control speed using PWM
// The used formula equals (PWM_resolution/supply_voltage)*desired_voltage = PWM_value
long RotationSpeed4 = ((25500/SUPPLY_VOLTAGE)*MOTOR_VOLTAGE)/100;
long RotationSpeed3 = ((25500/SUPPLY_VOLTAGE)*(MOTOR_VOLTAGE/4)*3)/100;
long RotationSpeed2 = ((25500/SUPPLY_VOLTAGE)*(MOTOR_VOLTAGE/2))/100;
long RotationSpeed1 = ((25500/SUPPLY_VOLTAGE)*MOTOR_MIN_VOLTAGE)/100;

const long SPEED_DEG_1 = 2000;        // These values define the speed trajectory to reach a certain position
const long SPEED_DEG_2 = 800;        // The higher the difference the higher the speed to move the antenna
const long SPEED_DEG_3 = 350;        // Range from 180-30-20-10-2-0
``````

This is where they are calculated and then stored in EEPROM and saved in a struct with these def’s

``````  byte	_rotationSpeed;		// Actual rotation speed
byte	_rotationSpeed4;	// Rotator speed High
byte	_rotationSpeed3;	//
byte	_rotationSpeed2;	//
byte	_rotationSpeed1;	// Rotator speed Low
``````