Servo serial control problems

Just got the arduino starterkit for xmas so I am really new to this.
Been playing around a bit and really enjoyed the projects book.

trying to do something on my own I wanted to be able to control a servo angle with the serial monitor and also display the angle on the screen.
Heres what i got so far:
#include<LiquidCrystal.h>
#include<Servo.h>
LiquidCrystal lcd(12,11,5,4,3,2);
Servo servo;
int angle=90;
void setup(){
Serial.begin(9600);
servo.attach(6);
lcd.begin(16,2);
}
void loop () {
if (Serial.read()>0){
angle= Serial.read();
servo.write(angle);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Angle:");
lcd.setCursor(0,1);
lcd.print(angle);
}
The screen seems to work and the servo sets itself to 90deg.
But when i try and enter i value in the serial monitor I get weird values for angle, most often it will be -1 or 48.
Any ideas on what I am doing wrong?

I think that there is problem with transfering ASCII character from serial monitor to non-ASCII number for servo. It is quite difficult to solve this problem so if you do not need it, try something else :slight_smile:

Zoomkat will be along shortly.

Basic serial servo test code that you can try.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 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(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo-test-22-dual-input"); // 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

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }
    Serial.print("Last servo command position: ");    
    Serial.println(myservo.read());
    readString=""; //empty for next input
  } 
}

manek100:
I think that there is problem with transfering ASCII character from serial monitor to non-ASCII number for servo. It is quite difficult to solve this problem so if you do not need it, try something else :slight_smile:

There is no problem sending characters from the serial monitor and having them interpreted as numbers by the Arduino. You just need to think carefully about what is happening. The Arduino is not nearly as clever as a person and must be instructed to deal with every little thing.

If you look at Zoomkat's sketch you will see how it first reads all of the characters from the PC and then converts the whole thing (e.g. 4, 44 or 444) into an integer.

...R

while (Serial.available()) {

This makes sure when you read the serial, there is something to read. If not, a read returns -1.

Thank you zoomkat this works perfectly!
Seems like the fairly big step that i missed was telling the arduino how to interpret the values from the pc.

Thanks again for the quick reply s!