Help with code that is not working correctly

so I'm trying to have tell the servo to goto a certain position when the arduino receive a letter over the serial port, It kind of works but not really, the servo moves to its position but goes back and doesn't really respond afterward, I have the led go high and low to verify its receiving the letter and attempting to write the servo position, but that's not working either.

#include <Servo.h>

Servo myservo;

const int ledPin = 13;
int incomingByte;

void setup(){
myservo.attach(9);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);

}

void loop()
{

if (Serial.available() > 0) {

incomingByte = Serial.read();

if (incomingByte == 'H') {
  myservo.write(180);
  digitalWrite(ledPin, HIGH);  
  
}

if (incomingByte == 'L') {
  myservo.write(0);
 digitalWrite(ledPin, LOW);
 
}



}
}

Is your baud rate from the IDE same as that in the program? If you force incoming byte to be H in the code, does the servo move? How are you powering the servo?

Try leaving out the If(Serial.Available() > 0) and then maybe add a 5 millisecond delay. Not sure if you need the delay though, try it with the delay and without

Try leaving out the If(Serial.Available() > 0)

No don't.

Change the incoming byte variable to a char.

Have you connected the grounds?

Try giving it more power.

The servo has enough power, I put on one of the sample sketches that sweeps the servo from 0-180 and it works perfectly fine, so power isn't an issue. I'm also using the arduino processing serial monitor, so communication shouldn't be an issue. But how does one go about changing the incoming byte variable to character? thanks for the help!

Are you actually hitting "send"?

But how does one go about changing the incoming byte variable to character? thanks for the help!

Using the text editor, of course. Highlight the incorrect type, and type the correct type.

Sorry, but the return type of "Seria.read" is "int".

OP, what observations have you made?
What else have you tried?

Sorry, but the return type of "Seria.read" is "int".

True, but, if you KNOW that there is data to read, because you have called Serial.available(), you can store the output in a byte sized variable, with no data loss. Often, this is a desirable thing to do.

SO anybody have any idea what is up? the sketch works perfectly fine if i remove the moving of the servo, the LED part I mean.

nevermind everybody fixed it, used previous suggestion of adding a delay, added a 1500ms delay after writing the servo position!

the sketch works perfectly fine

What sketch is that? The one full of errors that we have been pointing out, or something derived from that that fixes the errors?

What does removing the servo moving code have to do with incorrectly reading serial data?

in the code i have a servo move to 180 degrees if it detects that the button is in a HIGH state and it reads an H from the serial port. The individual pieces of the circuit work when being tested by them selves so its not hardware related as far as i can tell, I'm thinking it has something to do with the && operators? Any ideas?

#include <Servo.h>

Servo myservo;

const int buttonPin = 2;
const int ledPin = 13;
int incomingByte;

int buttonState = 0;

void setup(){
myservo.attach(9);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT); 

}

void loop()
{
  buttonState = digitalRead(buttonPin);

if (Serial.available() > 0) {

incomingByte = Serial.read();

}

if (incomingByte == 'H' && buttonState == HIGH) {
  myservo.write(180);
  delay(1500);
  digitalWrite(ledPin, HIGH);  
  
}

if (incomingByte == 'L' && buttonState == LOW) {
  myservo.write(90);
  delay(1500);
 digitalWrite(ledPin, LOW);
 
}

}

The two 'if' statements with && should be inside the first 'if' statement so that they are only tested when a serial character is available.

void loop()
{
  buttonState = digitalRead(buttonPin);

  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 'H' && buttonState == HIGH) {
      myservo.write(180);
      delay(1500);
      digitalWrite(ledPin, HIGH);  

    }
    if (incomingByte == 'L' && buttonState == LOW) {
      myservo.write(90);
      delay(1500);
      digitalWrite(ledPin, LOW);

    }
  }
}

Pete

So you have to type a command AND use a button to move the servo? Are you sure you don't need to use || instead, so you either type a command, or use the button ?

the servo will be used to lock a deadbolt, the switch will detect when the door is closed so the deadbolt only moves when the door is closed, also i ahave already tried it with || with no luck

Try adding some ()s, such as:

if ((incomingByte == 'H') && (buttonState == HIGH)) {

Apparently @swilliams1284 wanted a change of scenery. He's moved the conversation here...
http://arduino.cc/forum/index.php/topic,127428.0.html