Toggling Servo output

I’ve hit a road block and I’m sure it’s a cake walk for most of you.
Problem is after I send a 1 to the Arduino and it only twitches the servo and I want it to stay enabled until I send 0 which should tell it to stop.

That’s the objective at least.

#include <Servo.h>

int SerPin = 8;

void setup()
{
    pinMode (SerPin, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
  while (Serial.available() == 0);
  int val = Serial.read() - '0';
if (val == 1)
  {
  digitalWrite(SerPin, HIGH);
  }
else if (val == 0);
  {
  digitalWrite(SerPin, LOW);
  }
  Serial.flush();
}

There's a very useful servo library for driving R/C servos correctly.

Do you mean the Software Servo?

No, the standard Servo library.
Simply toggling a control line will not drive a servo, unless your timing is correct.

see the servo reference for documentation

  while (Serial.available() == 0);
  int val = Serial.read() - '0';
if (val == 1)
  {
  digitalWrite(SerPin, HIGH);
  }
else if (val == 0);
  {
  digitalWrite(SerPin, LOW);
  }
  Serial.flush();

Do nothing until a byte arrives in the serial buffer. As soon as one arrives, read it and set a digital pin.

Then, dump everything else that has arrived while setting the digital pin. Why?

Simple code for testing a hobby servo.

// 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="";
  } 
}

Hi zoomkat, I wonder why you start the servo at 2000 rather than 1500?

for users that prefer to work in degrees, here is a test sketch inspired by zoomkats:

// serial servo tester for Arduino 0022
// servo on pin 9
// send angle as digits between 0 and 180
// terminates on the first non-digit character such as newline

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
String readString;

void setup() {
  Serial.begin(9600);
  myservo.write(90); //set initial servo position if desired
  myservo.attach(9);  //the pin for the servo control 
  Serial.println("servo-test-22");
}

void loop() {

  for(;;) { 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      if(c >= '0' && c <= '9')
        readString += c; //makes the string readString
      else
        break;  
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();
    myservo.write(n); 
    readString="";
  } 
}

Hi zoomkat, I wonder why you start the servo at 2000 rather than 1500?

The 2000 has no significance other demonstrating that a servo can be prepositioned if desired prior to the arduino receiving any position commands.

No, the standard Servo library.
Simply toggling a control line will not drive a servo, unless your timing is correct.

How do I get any timing and for that the correct timing?
Also how do I get the servo to hold at a specific point?

The servo library handles the timing for you. It continuously sends properly timed pulses to all attached servos.