Hello guys, i was working on my RC car and using servo to turn left or right but there is a question that i want to ask that how to limited the range of servo from 60 to 120 degrees? because right now i can only controlling servo from 0 to 180 degrees. I'm just a 14 years old boy so go easy on me and also i use a joystick to control the servo.
This is my code. I managed to get the servo not going through the limit of 60 degrees but can't make it not going through the limit of 120.
#include <Servo.h>
Servo servo;
int servoValue;
int Xval;
int Yval;
int Sval;
void setup() {
// put your setup code here, to run once:
pinMode(6, INPUT);
Serial.begin(9600);
servo.attach(7);
}
void loop() {
// put your main code here, to run repeatedly:
Xval=analogRead(A0);
Yval=analogRead(A2);
Sval=digitalRead(6);
Serial.print(Xval);
Serial.print(";");
Serial.print(Yval);
Serial.print(";");
Serial.println(Sval);
delay(200);
servoValue=180./1023.*Xval;
if(Xval>700){
servo.write(120);
}
if(Xval<341){servo.write(60);
}
else{
servo.write(servoValue);
}
}
Pls help me.
I just only need Xval so never mind about the Yval and Sval.
Why do you check Xval, not servoValue?
The outcome of your first if(Xval<341) is overridden by the outcome of the second if. Use if...else if...else...
Here is the new code but it is still not working. Pls tell me what is wrong and i'm pretty sure that there is nothing going wrong with my wire hook up. Also i use a servo SG90 Tower Pro.
#include <Servo.h>
Servo servo;
int servoValue;
int Xval;
int Yval;
int Sval;
void setup() {
// put your setup code here, to run once:
pinMode(6, INPUT);
Serial.begin(9600);
servo.attach(7);
}
void loop() {
// put your main code here, to run repeatedly:
Xval=analogRead(A0);
Yval=analogRead(A2);
Sval=digitalRead(6);
Serial.print(Xval);
Serial.print(";");
Serial.print(Yval);
Serial.print(";");
Serial.println(Sval);
delay(200);
servoValue=180./1023.*Xval;
if(Xval>682){
servo.write(120);
}
else{servo.write(servoValue);}
if(Xval<341){servo.write(60);
}
else{
servo.write(servoValue);
}
}
Have you tried looking at the Knob example for servo in the IDE examples.
It uses a map function.
Thanks a lot bluejets. It worked. I will post the example code in here to make an example for anyone have the same issue like me.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 60, 120); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
One more time thanks a lot.
if(Xval>682){
servo.write(120);
}
else{servo.write(servoValue);}
if(Xval<341){servo.write(60);
}
else{servo.write(servoValue);}
This is a case where doing a "unit test" of your code would help. What happens when you run a value INSIDE of the range through? What about above? What about below?
xval < 341 (say we use 300 for this example)
- Through the first IF, it evaluates FALSE, and it writes the servo to servoValue
- Through the second if statement it evaluates TRUE and writes the servo to 60 (Desired output)
- It writes the servo value TWICE, and it appears to give the correct output, but that's only because the second if statement is what governs the output.
xval > 682
- Through the first IF, it evaluates TRUE, and it writes 120 to the servo value. (Desired output)
- Through the second IF, it evaluates FALSE, and then writes the servoValue. (Not desired output)
- It also writes the servo value TWICE, and only the second sticks, appearing to give us the wrong result.
Good luck!