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
}
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?
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?
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?