Servo will eventully crash board - potential problem with my code being unstable

Hi guys

What I’m trying to do is pass 3 characters through the serial and have these interpreted as an angle. This angle is then passed to the servo to move it.

Communicate */

#include <Servo.h> 

Servo myservo;  // create servo object to control my servo 
char byteread; //Char to hold the incoming byte from serial
int angle; //int to hold servo angle
int i = 0; //int to hold a counter variable
void setup() 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600); //begins serial communication

void loop() 
  if (Serial.available()){

    i=i++; // counter to determine which character is incoming from serial (looks for 3 in total)
    byteread = - '0'; // read current character
    // if statements to determine the byte
    if (i == 3){ //final counter variable the angle 
    angle = angle + byteread; //integer = byte * 1 
    angle = constrain(angle,0,180); // constrain movement angle to prevent inputs out of range for servo
    Serial.println("Servo about to move");
    myservo.write(angle); // move servo
    Serial.print("Servo Moved");
    angle = 0; // reset angle for next 3 character input
    i = 0; // reset counter for next 3 character input
    Serial.print(" : Loop Finished : ");
    else if (i == 2){
    angle = angle + (byteread * 10); // integer = byte x 10 ( 2nd character)
    else if (i == 1){
    angle = angle + (byteread * 100); // integer = byte x 100 (1st character)

If i comment out the servo code and upload it the angle return works fine (again and again), however if i move the servo it seems to cause instability. It works but eventually crashes with the following error. Input/output error in writeArray
	at Method)
	at javax.swing.JTextField.fireActionPerformed(
	at javax.swing.JTextField.postActionEvent(
	at javax.swing.JTextField$NotifyAction.actionPerformed(
	at javax.swing.SwingUtilities.notifyAction(
	at javax.swing.JComponent.processKeyBinding(
	at javax.swing.JComponent.processKeyBindings(
	at javax.swing.JComponent.processKeyEvent(
	at java.awt.Component.processEvent(
	at java.awt.Container.processEvent(
	at java.awt.Component.dispatchEventImpl(
	at java.awt.Container.dispatchEventImpl(
	at java.awt.Component.dispatchEvent(
	at java.awt.KeyboardFocusManager.redispatchEvent(
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(
	at java.awt.Component.dispatchEventImpl(
	at java.awt.Container.dispatchEventImpl(
	at java.awt.Window.dispatchEventImpl(
	at java.awt.Component.dispatchEvent(
	at java.awt.EventQueue.dispatchEvent(
	at java.awt.EventDispatchThread.pumpOneEventForFilters(
	at java.awt.EventDispatchThread.pumpEventsForFilter(
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(
	at java.awt.EventDispatchThread.pumpEvents(
	at java.awt.EventDispatchThread.pumpEvents(

I’m scratching my head to this one as i cant see why its unstable, however i am fairly amateurish at this. Any help or guidance that you guys could provide would be very welcome. That may be a more robust approach or improving the above code.



If you are trying to power the servo from the Arduino, don't. Use a separate power supply capable of supplying 1 ampere per straining servo.

Here is an easier way to get a number from Serial.

The problem is nothing to do with your sketch - that is a Java stack trace from an exception occurring within a Processing application running on the PC. What application is that - is it the Arduino IDE, or something else?

Simple servo testing code for use with the serial monitor.

// 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() {
  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 =;  //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.print("writing Angle: ");

    readString=""; //empty for next input
 if (i == 3){ //final counter variable the angle 
    angle = angle + byteread; //integer = byte * 1

This part of your program interprets only the last byte read as ("some character's ascii value" - "the ascii value of the character 0") and adds it to the variable angle. However, the comments suggest that you intended all 3 ascii bytes to be interpreted as a number.


Real big thank-you for your replies. It gave me somewhere to start. To let you know what i have done;

I tried the servo test code you provided zoomkat and retrieved the same error.

I wrote an application in visual basic studio ( to interact with the serial com port. This works fine without any errors so perhaps its the Arduino IDE? (thanks PeterH)

Would re installing the Arduino software potentially fix this?

I know as well not to power the servo from the board with some load!