Hello,
I'm replicating steering with a servo motor and controlling the servo motor angle using the serial monitor.
I'm having trouble setting a min and max the servo will turn. For example I have the code set so typing in 1-5 will give you a position of 60-120 degrees. I want it to not move or only hit the max if a number greater than 5 or less than 0 is entered.
Here is a copy of the code I'm currently working on.
Thanks.
#include <Servo.h>
Servo myservo;
int user_input;
int SPEED = 1000;
void setup() {
Serial.begin(9600);
myservo.attach(9);
myservo.write (90);
pinMode(9,OUTPUT);
delay(1000);
Serial.print ("TYPE USER INPUT 1 TO 5 \r\n");
}
void loop() {
//int value = Serial.parseInt();
//user_input = Serial.parseInt();
if(Serial.available()) {
int value = Serial.parseInt ();
Serial.println(value);
analogWrite(9,HIGH);
user_input = Serial.parseInt();
user_input = map(value,1,5,60,120);
if (value <=(5) || value >=(1)) {
myservo.write(user_input);
}
else {
myservo.write(3);
}
the servo motor will turn to the desired angle but turn back to a default position. But with that added line it will turn and stay at requested angle.
I tried using AND but made turning to an unwanted position (type in 6 to serial monitor) worse. It would turn to 90 degrees from default position whereas using OR made 6 turn to where 6 would be on the scale, if that makes sense.
You should delete these two lines. if you attach pin 9 to 'myservo' , the servo library controls pin 9 completely. You should not do anything with pin 9 by yourself.