map function alternative that allows fractions?

In the code below, I collect analog data from the potentiometer built in the servo motor and feed it back to the Arduino.

A max and min value is recorded.

I then use the map() function to apply the potentiometer values to degrees.

My problem:

I want to read degrees in decimals but The map() function uses integer math so will not generate fractions.

What is an alternative way to do this?

// Include Arduino Servo Library
#include <Servo.h>

// Control and feedback pins
int servoPin = 9;
int feedbackPin = A0;
float ser;

// Value from feedback signal
float feedbackValue;
float feedbackMin, feedbackMax;
//unsigned int
// Create a servo object
Servo myservo;

void setup()
{

Serial.begin(9600);

// Attach myservo object to control pin
myservo.attach(servoPin);

myservo.write(0); //set the servo to 0 position
delay(700); //wait for the servo to reach there
feedbackMin = analogRead(feedbackPin); // Pot value at 0 degrees
delay(700);
Serial.println("Pot value for 0 deg is " + String(feedbackMin)); // Print it!
delay (700);
myservo.write(130 * 1.1); //go to 130 degrees, the 1.1 is an error correction factor
delay(700); //wait for the servo to reach there
feedbackMax = analogRead(feedbackPin); //pot value at 180 deg
Serial.println("Pot value for 130 deg is " + String(feedbackMax));
delay(700); //fancy delay

// Read value from feedback signal
feedbackValue = analogRead(feedbackPin);

// Print to serial monitor when done
Serial.println("Finished!");
float deg = 90 * 1.1;
myservo.write(deg);
delay(1000);

}

void loop()
{
ser = analogRead(feedbackPin);
ser = map(ser, feedbackMin, feedbackMax, 0.0, 130.0);
Serial.println(ser);

delay(100);

}

https://forum.arduino.cc/index.php?topic=3922.0

You could map it to integers between 0 and 35999 and get 2dp as you know where the decimal point should be.

linear interpolation

#define map(a, b, c, d, e) ( { typeof(a) _a=(a); typeof(a) _b=(b); typeof(a) _c=(c); typeof(a) _d=(d); typeof(a) _e=(e); ( _a - _b) * (_e - _d) / (_c - _b) + _d; } )

This take and use the type of the first argument
Then the type and resolution is defined at runtime

As I think I said in your last thread on this subject there is very little point having your values as floats or fractions. The analogRead gives you an integer and servo.write() only takes integers.

So the question is what do you plan to do with your fractional values? If you manage to get an answer that the angle is 87.856 degrees what then?

Steve

The only thing I could think of with using fractional numbers with servo torque values is when using milliseconds.

If using the Servo library with its default range of 0 to 180 degrees is equal to 500 to 2500uSeconds. Which comes out to 11.11uS per degree. Thus one could get a torque values of 5uS, which is about a 1/2 of a degree of torque. I have found metal geared servos will respond, reliably, to a torque value of 7uSeconds, which is less than one degree.

Yes, I use float values to calculate less than one degree torque values. It works.

On an ESP32 torque values of 3uS can be generated but will the servo 3uS move is up in the air.

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