Map function and servo motor

This is my map function. y= (x,0,74,72,144). The figures between 72 and 144 are used to move a servo motor. I am using the servo motor library which gives 0-180.
I plotted the figures and when x reached 46, y was 116 from the print out on the serial monitor. Then the servo motor stopped moving. Can anybody see an error in the map? The servo should have gone on to 144. The servo only appeared to move about 90 degrees.
I know I can get nearly 180 degrees movement from this servo as tested with the arduino pot example. The servo is FT5330M. It is possible that on further testing I may get the full 180 degrees. I have made several changes to the program since the test.
Again can anybody see a potential error in the map? x is a variable which kept on rising to a limit at 74. Y was increasing on the serial monitor but the servo had stopped.

Please post your test sketch

What is the source of the value of x ?

x is 46. y is 116. Why should the servo go to 144?

a7

  • Please explain what you think is happening here. :thinking:

The "map" is (x+72) with the range being (exactly 0+72 to about 74+72 [really, 74+70]) until about half way (108) when the map is a little less (36 + 71).

Your observation of 46 + 72 = 118 with an output of y = 116 shows the non-linearity of resistors. Sounds like it works perfectly as can be expected.

do you want the map output to be "closer" to 0-180?

y= map(x,0,74,  0,180)

(180 * 46/74 = 112)
(74 * 144/180 = 59)

False. Read all the comments and reply. When "x = 46" and "y = 116" this is close to the calculated 112.

If the servo stops moving when "x" increases to 74 and "y" increases to 144, (1) the servo is broken or (2) your microcontroller is broken, or (3) you are not interpreting information correctly.

because x in my program kept rising towards 74 and so y should have kept rising to 144 according to the map. The serial monitor was showing the increasing Y but the servo had stopped moving.

values of x between 0-74 are being mapped to values between 72 and 144 which are sent to the servo to move it. I am thinking that an error may be in the value of x which is not an integer. The map function claims that entries are integers.

no it has to be 144

yes but x was still rising on the serial monitor towards 74 and y was also increasing on the serial monitor. when y reached 116 however there was no more movement on the servo. y on the serial monitor kept rising towards 144 and reached it but the servo had stopped much earlier.
I am leaning toward the problem being that x is not an integer in my program. The map function claims that the numbers are integers.

By your responses it looks like you have not read a single reply. Use a float in servo.write() and you will see the answer.

The program with this?

Probably does not work.

Hi,
PLEASE post ALL your code, in code tags.

Have you checked your servo by running the SWEEP code in the Servo Library's example code.

How often do you increment x?
Your problem may be that you are incrementing TOO fast.

Tom.. :smiley: :+1: :coffee: :australia:

This will take your 0 to 74 input and map it to 72 to 144 pwm output for a 180-degree servo. You will need a potentiometer.

#include <Servo.h>
Servo servo;
int servoPin = 3;

void setup() {
  servo.attach(servoPin);
}

void loop() {
  servo.write(map(map(analogRead(A0), 0, 1023, 0, 74), 0, 74, 72, 144));
}

then the x value should be 74

don't you see that an x of 0 results in a y of 72 and an x of 74 a y of 144?

Well, since you won't post your code, or a test, here you go:

Tell us how this does not meet your needs.
x is a float, as you finally revealed, and y is an int.
If you take x out of bounds, of course, y will be out of bounds; use constrain() if that's a problem. However, you seem to think the function limits incorrectly within the ranges stated for x and y, which it does not.
Ball's in your court. I have you muted, because of history, and because with 102 topics opened but none ever marked solved, I expect you'll just walk away from this one, too. I may or may not check back.

mapfn data graphed...
image