Hi, Im new to Arduino and was just wondering if someone could help me in working out how to reverse the inputs of my servo?
Also if someone could help me in understanding how the numbers work as I've managed to make it work with trial and error but it doesn't make sense to me to be honest.
Thanks!
This is my code:
`#include <Servo.h>
Servo myservo;
int potpin = 0;
int val;
void setup() {
myservo.attach(7);
}
void loop() {
val = analogRead(potpin);
val = map(val, 0, 1023, 82, 115);
myservo.write(val);
delay(15);
}
val is set by the reading of your potentiometer (in the joystick).
That will be 0..1023 depending on the deflection. Print that value and you will see how much of the range gets visited as you go to the extremes.
Next, we transform, or "map" that range 0..1023 into the range 82..115. It's a bit of simple mathematics, 0 ends up giving you 82, and 1023 gives you 115, and in between it does the right thing and gives you something in between.
Lastly, that transformed value is sent out to the srvoe, which goes to a position depending on the number. here it looks like it is meant to sweep from 82 dgrees to 115 dgrees, a fraction of the 0 to 180 it is probably capable of doing.
Lotsa ways to get it to go in reverse. Here's one, it just trnsaforms the 0..1023 raw reading and make it go 1023..0:
val = 1023 - analogRead(potpin);
1023 - 0 is 1023. 1023 - 1023 is 0. And in between the maths do the right thing.
Thank you for this information, yes I manually inputted the 82 and 115 values as they were my limits however I now need to increase them but I have got the reversal sorted so thanks for that guys