Go Down

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

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 for Arduino 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.

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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!

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.

PaulS

Quote
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.

AWOL

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

OP, what observations have you made?
What else have you tried?
"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.

PaulS

Quote
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!

PaulS

Quote
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?

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

}

}

Go Up