Go Down

Topic: a invalid long problem (Read 353 times) previous topic - next topic

gharryh

I want to calculate some values depending on some defines.
But i get a error:
ArduKMoto:40: error: 'long' invalid for 'RotationSpeed4'
ArduKMoto:40: error: expected primary-expression before '=' token
ArduKMoto:40: error: expected primary-expression before '=' token

code
Code: [Select]
#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
float long RotationSpeed4 = ((25500/SUPPLY_VOLTAGE)*MOTOR_VOLTAGE)/100;
float long RotationSpeed3 = ((25500/SUPPLY_VOLTAGE)*MOTOR_VOLTAGE)/100;
float long RotationSpeed2 = ((25500/SUPPLY_VOLTAGE)*(MOTOR_VOLTAGE/2))/100;
float long RotationSpeed1 = ((25500/SUPPLY_VOLTAGE)*MOTOR_MIN_VOLTAGE)/100;

Why cant it be a long? and what should it then be?

wildbill

is it a float or a long - can't be both.

gharryh

#2
Apr 04, 2013, 08:32 pm Last Edit: Apr 04, 2013, 08:34 pm by gharryh Reason: 1
Making it either a float or long gives (almost) the same error:
ArduKMoto:40: error: expected primary-expression before '=' token
ArduKMoto:40: error: expected primary-expression before '=' token

wildbill

What does your revised code look like?

gharryh

Code: [Select]
#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)/100;
//long RotationSpeed2 = ((25500/SUPPLY_VOLTAGE)*(MOTOR_VOLTAGE/2))/100;
//long RotationSpeed1 = ((25500/SUPPLY_VOLTAGE)*MOTOR_MIN_VOLTAGE)/100;

I commented the last 3 out, they produce the same ero\ro

wildbill

There is no = in a #define. At least, not in this case.

robtillaart

when you use longs you will have truncating errors, check the diffs below

Code: [Select]

#define SUPPLY_VOLTAGE  800     
#define MOTOR_VOLTAGE  600

#define SUPPLY_VOLTAGE_F  800.0     
#define MOTOR_VOLTAGE_F  600.0

long l = ((25500/SUPPLY_VOLTAGE)*MOTOR_VOLTAGE)/100;
float f = ((25500/SUPPLY_VOLTAGE_F)*MOTOR_VOLTAGE_F)/100;

// rewritten the math!
long l2 = 25500L * MOTOR_VOLTAGE /SUPPLY_VOLTAGE /100;  // you need to add the L to the values to enforce long math

void setup()
{
  Serial.begin(9600);
  Serial.println(l);
  Serial.println(f,3);  // 3 decimals
  Serial.println(l2);
}
void loop(){}

==> about  2.5%  difference
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up