Understanding servo.write

Hello, I understand this is a very basic question, but I looked around and couldn’t find an answer anywhere.

Basically, why doesn’t this work? I want to have a position (0-180) input read by serial.read, and then for a servo to move to this position.

#include <Servo.h>
Servo myServo;
int pos =0;

void setup() {
     Serial.begin(9600);
     myServo.attach(9);
}

void loop(){
     if(Serial.available()){
          int pos = Serial.read();
          servo.write(pos);
}
}

Have a look at this Serial Input Basics thread.

You have to remember that a number like 180 is going to come in as three ascii characters unless you are sending it as a raw byte. You’ll have to read all three characters and convert them from ascii back to a real number. I think that thread has a specific example for just what you want to do.

Serial.read() returns a single ASCII character. So if you typed "A" on the keyboard, pos would be assigned the decimal value 65. Here is an ASCII table. I'm sure that's not what you want, letter A to represent 65 degrees. Get it?

Yes, thank you! that is very helpful

Very simple servo test code.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// 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.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer
    myservo.write(n);
    readString="";
  } 
}

Very simple servo test code that avoids the use of the String class, and will work correctly with different baud rates and key-by-key entry on a terminal emulator (just remember to hit the “return” key when you’ve entered your number), using 2/3rds the program memory of Zoomkat’s example, and much less RAM.

(compiled, but not thoroughly tested)

#include <Servo.h>
Servo myservo;
const byte servoPin = 9;

void setup() 
{
  Serial.begin(115200);
  myservo.attach(servoPin); 
  Serial.println(F("servo-test"));
}

void loop() 
{
  static int angle;
  
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n')
    {
      Serial.println(angle);
      myservo.write (angle);
      angle = 0;
    }
    else
    {
      if (isdigit (c))
      {
        angle *= 10;
        angle += c - '0';  
      }
    }
  }
}

Very simple servo test code that avoids the use of the String class, and will work correctly with different baud rates and key-by-key entry on a terminal emulator (just remember to hit the "return" key when you've entered your number), using 2/3rds the program memory of Zoomkat's example, and much less RAM

My code works with the default serial monitor settings and yours doesn't. Maybe yours isn't so simple. 8)

Very simple servo test code that avoids the use of the String class, and will work correctly with the default settings of the Serial Monitor and also avoids the use of the tedious thumb-twiddling delay () function, using 70% of the program memory of Zoomkat’s example, and much less RAM.

#include <Servo.h>
Servo myservo;
const byte servoPin = 9;
const unsigned long inputTimeout = 2000;

void setup()
{
  Serial.begin(9600);
  myservo.attach(servoPin);
  Serial.println(F("servo-test"));
}

void loop()
{
  static int angle;
  static unsigned long lastCharAt;
  unsigned long nowTime = micros ();
  
  while (Serial.available()) {
    char c = Serial.read();
    if (isdigit (c))
    {
      lastCharAt = nowTime;
      angle *= 10;
      angle += c - '0'; 
    }
  }

  if (lastCharAt && (nowTime - lastCharAt >= inputTimeout))
  {
    Serial.println(angle);
    myservo.write (angle);
    angle = 0;
    lastCharAt = 0;
  }
}

This seems a lot simpler and it works, whats wrong with this?
Because it uses the blocking function parseInt does this make it undesirable?

#include <Servo.h>
Servo servo;
int pos;

void setup() {
     Serial.begin(9600);
     servo.attach(9);
}

void loop(){
     if(Serial.available() > 0){
          int pos = Serial.parseInt();
          if (Serial.read() == '\n') {
          servo.write(pos);
        }
    }
}

Have a look at the examples in Serial Input Basics - simple, reliable and non-blocking. There is also a parse example.

...R