Go Down

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

#### gharryh

##### Apr 04, 2013, 08:17 pm
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_valuefloat 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

#1
##### Apr 04, 2013, 08:27 pm
is it a float or a long - can't be both.

#### gharryh

#2
##### Apr 04, 2013, 08:32 pmLast 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

#3
##### Apr 04, 2013, 08:42 pm
What does your revised code look like?

#### gharryh

#4
##### Apr 04, 2013, 08:53 pm
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_valuelong 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

#5
##### Apr 04, 2013, 09:19 pm
There is no = in a #define. At least, not in this case.

#### robtillaart

#6
##### Apr 04, 2013, 09:53 pm
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.0long 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 mathvoid setup(){  Serial.begin(9600);  Serial.println(l);  Serial.println(f,3);  // 3 decimals  Serial.println(l2);}void loop(){}`