Go Down

Topic: Help with code that is not working correctly (Read 1 time) previous topic - next topic

swilliams1284

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.

Code: [Select]
#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);

}



}
}

marco_c

#1
Oct 13, 2012, 05:18 am Last Edit: Oct 13, 2012, 05:20 am by marco_c Reason: 1
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?
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Sasha831

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

Grumpy_Mike

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

No don't.

Change the incoming byte variable to a char.

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up