Computation error converting map() int value into float

Hello, first time posting here and first time working with an Arduino.

I am working on a program that will use a joystick's position to control a servo. I need the 0-1023 map()'d values to correlate with an angle 0-90 angle of a servo.

I created a function to take the 'int' values and convert them to float because I need accurate values fed into the servo.
it returns:
514 (Xvalue at the middle of joystick)
-19276.00 should be multiplying the last value by 90
-18.4 divides correctly by 1023
I cannot pinpoint why this multiplication is going wrong.

I appreciate any help and explanations!

Here is the code:

#include <Servo.h> // include servo library

Servo myservo; //declare a servo variable
int Xvalue; //declare Potentiometer  Xvalue
float servo_angle; // declare servo angle decimal
//float control_ratio = 90 / 1023;

//declare and define function
float pot_to_angle(int Xvalue){
  
  servo_angle = Xvalue * 90;
  Serial.println(servo_angle); // for debug 
  
  servo_angle = servo_angle / 1023;
  Serial.println(servo_angle); // for debug
  
  return servo_angle;
}

void setup() {
   Serial.begin(9600); //set baud rate
   
   myservo.attach(3); //attach servo to pin 3
}


void loop() {
  
  Xvalue = map(analogRead(A0), 0, 1023, 0, 1023); // use map to set range for the voltage
  
  Serial.println(Xvalue); //print pot
  
  servo_angle = (float) pot_to_angle(Xvalue); //get servo_angle from pot pos
  Serial.println(servo_angle); //print servo angle
  
  delay(1000); //wait 1s
  
  myservo.write(servo_angle); // output to servo
  
}

Wah!

That's going to exceed the range of an int.

The Servo object only accepts int values, IIRC

#include <Servo.h> 

Servo myservo;

void setup() 
{
   Serial.begin(9600); 
   myservo.attach(3);
}

void loop() 
{
  int servoAngle = map (analogRead(A0), 0, 1024, 0, 90);
  
  Serial.println(servoAngle);
  
  delay(1000);
  
  myservo.write(servoAngle);
}

thank you for the responses!

hmm, since that operation is exceeding the int type and the servo object will only accept int types, is there some way of mapping those map()'d values to a certain range?

ex:

when
Xvalue = 0 servo_angle = 0
Xvalue = 1023 servo_angle = 90

Otherwise, would it be possible to set the Xvalue as a float, process them and then round them to the nearest int when placing feeding them to the servo?

I added my modified code to my last post - give it a try.
(it is uncompiled, and hence untested, and is offered with zero warranty)

1 Like

It worked!

although I would like to ask clarifying question:

in the map() function


map (analogRead(A0), 0, 1024, 0, 90);

The 0, 1024, is setting the scope of the potentiometer giving it a value depending on the position

while the 0, 90 is setting the range. I thought that this section would set a valid range such that if the joystick was placed at position 91 it would not output a value.

Is it actually setting a range for the original to be mapped?
(mapping 0-1023 to 0-90?)

if that is true would I be able to make a dynamic map function that would allow me to set a min and max angle?

map (analogread(A0) 0, 1023, min_angle, max_angle); ?

When in doubt, there's always the reference

1 Like

Great! thanks for the clarification I really appreciate it!

There is some controversy about how map () is specified/implemented, and you'll probably want to change to this

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.