Servo motor min and max

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

Are you expecting 2 ints? as you are reading twice?

Also.. your if should probably be using AND (&&) not OR (||) ?

I found that without

user_input = Serial.parseInt();

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.

The 2nd serial.parseint() timeouts after (probably 2 seconds) and then returns zero.

Your if statement always returns true... because all numbers are GE 1.

Also - turn off line ending in the serial monitor.. you are not handling them properly. You're getting and extra character which which equate to 0.

Try this version... I'v added a few more print statement so you can see what is happening.

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

void loop()
{
  if (Serial.available())
  {
    int value = Serial.parseInt ();

    Serial.println(value);

    analogWrite(9, HIGH);

    user_input = map(value, 1, 5, 60, 120);

    Serial.print(user_input);
    
    if (value >= 1 && value <= 5)
    {
      Serial.println("true");
      myservo.write(user_input);
    }
    else
    {
      Serial.println("false");
      myservo.write(3);
    }
  }
}

Thanks for your help!

This is my updated version and now is doing what I would like it to do.

#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.println ("TYPE USER INPUT 1 TO 5");
}

void loop()
{
  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 >= 1 && value <= 5)
    {
      myservo.write(user_input);
    }
    else
    {
      Serial.println("ERROR: INVALID INPUT");
      myservo.write(myservo.read());
    }
  }
}

Also, I'm still super new to programming. What do you mean by this

Nice one. You don't need the 2nd parseInt... that is just removing the LF probably.. just turn line ending off in the monitor, or change to just CR.

GE
Greater than or equal
">="

there's a subtle difference between guarding against invalid input values and setting min and max stepper motor positions.

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.