How to control servo motor via user serial monitor analog input?

Could someone link me a good write up or tutorial on controlling a servo motor and making it move to a certain degree based on user input on the serial monitor? For example, if I wanted it to move to 180 degrees, I would type in 180 and the servo motor would move to 180 degrees.

Wait a few minutes, and zoomkat will be along to post some code. :stuck_out_tongue_closed_eyes:
Or you could save time and search for it.

First time in a couple of days!

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}

zoomkat:
First time in a couple of days!

// zoomkat 10-4-10 serial servo test

// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray);
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  }
}

I put this code inside my already exisiting code and when I enter in a number, ie. 0, it automatically goes to 180 degrees. It does this for any number I input. And also, it only reads an input once, I can't enter another number in and make it move again.

I put this code inside my already exisiting code and when I enter in a number, ie. 0, it automatically goes to 180 degrees. It does this for any number I input. And also, it only reads an input once, I can't enter another number in and make it move again.

Then I would suspect something incompatable with your existing code.

Here is part of my code so far.

How do I make it so that when the "System deactived" pops up on the serial monitor after hitting my push button, it deletes the "System active Enter degree(s) 0-180." line and just displays "System deactived". Right now it's showing:
System active
Enter degree(s) 0-180.
System deactivated

#include <Servo.h>                          

int buttonPin = 2;              // pushbutton switch is connected to pin 2
int ButtonMode = 0;
int pushbutton;                        // variable for reading the pin status
int doublecheck;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state; initial

Servo servomotor;

int Hundreds;
int Tens;
int Ones;
int Good_value;
int Degrees =0;
int lol=0;

int var = 0;

int outputValue = 0;



void setup() {
  pinMode(buttonPin, INPUT);    // Declare pushbutton as input


 servomotor.attach(9); // servo is on pin 9 
 

  
  Serial.begin(9600);           
  buttonState = digitalRead(buttonPin);   // read the initial state
}

void loop(){
  
  pushbutton = digitalRead(buttonPin);      // read input value and store it in pushbutton
  delay(10);                                // 10 milliseconds
  doublecheck = digitalRead(buttonPin);     // read the input again to check for bounces
  if (pushbutton == doublecheck) {          // make sure we got 2 consistant readings
    if (pushbutton != buttonState) {        // the button state has changed
      if (pushbutton == LOW) {             // check if the button is pressed
        if (ButtonMode == 0) {               // if its off
          ButtonMode = 1;           // First time push
          Serial.println("System active");
  Serial.println("Enter degree(s) 0-180.");
      
    } else {
          if (ButtonMode == 1) {             // After first push
            ButtonMode = 0; // Go onto the 2nd case
            Serial.println("System deactived");
            
          } 
        }
      }
    }
    buttonState = pushbutton;                 // save the new state in our variable
  }




  // Different motor modes for each push of the button
  if (ButtonMode == 0) {                // initial mode
  
  servomotor.write(90); // set servo to 90 degree position
  }

How do I make it so that when the "System deactived" pops up on the serial monitor after hitting my push button, it deletes the "System active Enter degree(s) 0-180." line and just displays "System deactived".

To do this, you need to use something other than the Serial Monitor that comes with the Arduino. What you use needs to understand something like VT100, and you need to include appropriate VT100 codes in what you send to make it clear the screen.

In a former job, I knew all about how to make VT100 screens do all kinds of cool stuff, but that was 25 years ago.