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);
}